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

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

 

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

posted @   刘浩de技术博客  阅读(2092)  评论(3编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示