内存问题

这两天线上的一个服务出现了内存问题,表现在使用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?

 

from:http://www.cnblogs.com/liuhao/archive/2013/04/24/3040125.html

=========================================

http://www.2cto.com/os/201212/180499.html(Linux的进程与内存管理)

http://blog.csdn.net/aero_boy/article/details/6624263(用TCMalloc监测程序内存使用情况)
http://shiningray.cn/tcmalloc-thread-caching-malloc.html(TCMalloc)
http://blog.chinaunix.net/uid-9543173-id-3571436.html(linux下内存的统计和内存泄露类问题的定位 )
http://blog.csdn.net/wuwangyingzhong/article/details/8281767(GLIBC内存分配机制引发的“内存泄露”)
http://blog.csdn.net/guomsh/article/details/6536915(Linux Out-of-Memory(OOM) Killer)
http://www.360doc.com/content/11/1119/18/7492958_165786637.shtml(Linux下OOM Killer机制详解)
http://www.cnblogs.com/itfriend/archive/2011/12/14/2287160.html(Linux内存高,触发oom-killer问题解决)
posted @ 2014-06-23 15:16  dkcndk  阅读(2403)  评论(0编辑  收藏  举报