[备忘]记录下内存分配相关的一些文章资料

这两天线上的一个服务出现了内存问题,表现在使用top查看进程的RES会间断性的突然上升,而且从不下降。仔细review了线上的代码,没有发现内存泄漏,怀疑和glibc的内存分配机制有关,glibc并没有及时将内存释放给操作系统。

可以自行使用如下的测试代码进行下验证,会发现使用默认的glibc和google提供的tc_malloc,map吃掉的内存在离开自己的scope后并没有吐给操作系统,使用jemalloc没有如上问题。线上的代码已经重新用jemalloc编译推动上线了,还处在观察阶段。

#include <malloc.h>
#include <map>
#include <iostream>
#include <stdlib.h>
//#include "google/malloc_extension.h"

void testmap() {
  std::cout << "*************1 malloc_stats****************" << std::endl;
  malloc_stats();
  std::cout << std::endl;
  
  std::map<int, int> testmap;
  
  for(int i = 0; i != 10000000; i++) {
    testmap[i] = i;
  }
  std::cout << "*************2 malloc_stats****************" << std::endl;
  malloc_stats();
  std::cout << std::endl;
  
  testmap.clear();

  std::cout << "*************3 malloc_stats****************" << std::endl;
  malloc_stats();
  std::cout << std::endl;
}

int main() {
  //static const int DEFAULT_MMAP_THRESHOLD = 0;
  //::mallopt(M_MMAP_THRESHOLD, DEFAULT_MMAP_THRESHOLD); 
  
  testmap();
  //MallocExtension::instance()->ReleaseFreeMemory();
  sleep(20);
  
  std::cout << "*************4 malloc_stats****************" << std::endl;
  malloc_stats();
  std::cout << std::endl;
}

如下,记录下在网上查到的一些资料:

jemalloc

jemalloc:another option

更好的内存管理-jemalloc   (^_^给程序员最后的免费的午餐)

tcmalloc

TCMalloc : Thread-Caching Malloc

tcmalloc, a big surpise

TCMalloc: 线程缓存的Malloc

glibc

GLIBC内存分配机制引发的“内存泄露”

glibc内存泄露以及TCmalloc 简单分析

glibc内存管理ptmalloc2源代码分析  (大杀器,慎入,一份130页的pdf文档)

STL

有感于STL的内存管理

STL和内存管理技术

STL默认的内存分配的机制

[百度分享]频繁分配释放内存导致的性能问题的分析

实际应用

TFS Dataserver内存问题分析

Will malloc implementations return free-ed memory back to the system?

文章最后,对贵淘宝等的无私分享精神表示感谢!!

原文地址:https://www.cnblogs.com/liuhao/p/3040125.html