[備忘]記錄下內存分配相關的一些文章資料


這兩天線上的一個服務出現了內存問題,表現在使用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?

 

文章最后,對貴淘寶等的無私分享精神表示感謝!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM