linux下tcmalloc的使用

  • TCMALLOC优点很多,比glibcmalloc快,自带的堆栈工具可以轻松找出内存瓶颈和内存泄露
//CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ‐std=c++11 ‐g ‐pthread")

SET(SRC_INCLUDE
"/home/share/opensource/dest/linux2/gcc/x64/RelWithDebInfo/include")

INCLUDE_DIRECTORIES(${SRC_INCLUDE})

SET(SRC_LIB
"/home/share/opensource/dest/linux2/gcc/x64/RelWithDebInfo/lib")

LINK_DIRECTORIES(${SRC_LIB})

ADD_EXECUTABLE(main main.cc)

TARGET_LINK_LIBRARIES(main
libtcmalloc.a) //链接libtcmalloc要放在后边,如果不需要heap profiler和checker的话可以链接
libtcmalloc_minimal

//main.cc
#include <iostream>
	int main() {
	int* p = new int();
	return 0;
}
  • 内存申请使用完毕free 后是不会马上返回给系统的, malloc 和tcmalloc 都是一样的。在tcmalloc 中控制
    把未使用的内存返回给系统,主要有两种方法

    修改环境变量TCMALLOC_RELEASE_RATE ,这个值代表把未使用的内存还会给系统的速度取值0-10,值越高
    返还的速率越高,值为0表示不返还,默认是1.0

    可以在程序中调用MallocExtension::instance()‐>ReleaseFreeMemory();

内存泄露

  • 全局内存泄露
承上
//main.cc
#include <iostream>
int main() {
	int* p = new int();
	return 0;
}
[root@localhost gperftools]# export
PPROF_PATH=/home/share/opensource/dest/linux2/gcc/x64/RelWithDebInfo/bin/pprof
[root@localhost gperftools]# make
// HEAPCHECK有多个选项minimal normal strict draconian
[root@localhost gperftools]# env HEAPCHECK=normal ./main
WARNING: Perftools heap leak checker is active ‐‐ Performance may suffer
Have memory regions w/o callers: might report false leaks
Leak check _main_ detected leaks of 4 bytes in 1 objects
The 1 largest leaks:
Using local file ./main.
Leak of 4 bytes in 1 objects allocated from:
@ 433362 constructLeak
@ 433377 main
@ 7fd7e2669b15 __libc_start_main
@ 433289 _start
If the preceding stack traces are not enough to find the leaks, try running THIS shell
command:
pprof ./main "/tmp/main.16906.main‐end.heap" ‐‐inuse_objects ‐‐lines ‐‐heapcheck ‐‐
edgefraction=1e‐10 ‐‐nodefraction=1e‐10 ‐‐gv
If you are still puzzled about why the leaks are there, try rerunning this program with
HEAP_CHECK_TEST_POINTER_ALIGNMENT=1 and/or with HEAP_CHECK_MAX_POINTER_OFFSET=‐1
If the leak report occurs in a small fraction of runs, try running with
TCMALLOC_MAX_FREE_QUEUE_SIZE of few hundred MB or with TCMALLOC_RECLAIM_MEMORY=false, it
might help find leaks more repeatably
Exiting with error code (instead of crashing) because of whole‐program memory leaks
  • 对特定代码块进行内存泄露检查
// main.cc
#include <iostream>
#include <assert.h>
#include "gperftools/heap‐checker.h"
void constructLeak() {
	int* p = new int();
}
int main() {
	HeapLeakChecker heap_checker("test_foo");
	{
		constructLeak();
	}
	if (!heap_checker.NoLeaks()) {
		assert(NULL == "heap memory leak");
	}
	return 0;
}
[root@localhost gperftools]# env HEAPCHECK=normal ./main
WARNING: Perftools heap leak checker is active ‐‐ Performance may suffer
Have memory regions w/o callers: might report false leaks
Leak check test_foo detected leaks of 4 bytes in 1 objects
The 1 largest leaks:
Leak of 4 bytes in 1 objects allocated from:
@ 433392
@ 4333bd
@ 7f129bf36b15
@ 4332b9
If the preceding stack traces are not enough to find the leaks, try running THIS shell
command:
pprof ./main "/tmp/main.17300.test_foo‐end.heap" ‐‐inuse_objects ‐‐lines ‐‐heapcheck ‐‐
edgefraction=1e‐10 ‐‐nodefraction=1e‐10 ‐‐gv
If you are still puzzled about why the leaks are there, try rerunning this program with
HEAP_CHECK_TEST_POINTER_ALIGNMENT=1 and/or with HEAP_CHECK_MAX_POINTER_OFFSET=‐1
If the leak report occurs in a small fraction of runs, try running with
TCMALLOC_MAX_FREE_QUEUE_SIZE of few hundred MB or with TCMALLOC_RECLAIM_MEMORY=false, it
might help find leaks more repeatab
main: /home/share/api/gperftools/main.cc:17: int main(): Assertion `__null == "heap memory
leak"' failed.
Aborted (core dumped)
[root@localhost gperftools]#

计算内存消耗情况

  • notes: 最好链接libtcmalloc.so ,而不是链接静态库,不然会出现全局分析没有结果的情况

  • 对特定代码块进行分析

#include <iostream>
#include "gperftools/heap‐profiler.h"
void test1() {
	int i = 100;
	while (i‐‐) {
		char *buf = new char[1024 * 1024];
	}
}
void test2() {
	int i = 100;
	while (i‐‐) {
		char *buf = new char[1024 * 1024];
	}
}
int main() {
	HeapProfilerStart("./test.log");
	int i = 100;
	while (i‐‐) {
		char *buf = new char[1024 * 1024];
	}
	test1();
	test2();
	HeapProfilerStop();
	return 0;
}
  • 对整个程序统计内存使用情况
[root@localhost gperftools]# env HEAPPROFILE="./tmp" $(pwd)/main
#include <iostream>
#include "gperftools/heap‐profiler.h"
void test1() {
	int i = 100;
	while (i‐‐) {
		char *buf = new char[1024 * 1024];
	}
}
void test2() {
	int i = 100;
	while (i‐‐) {
		char *buf = new char[1024 * 1024];
	}
}
int main() {
	//HeapProfilerStart("./test.log");
	int i = 100;
	while (i‐‐) {
		char *buf = new char[1024 * 1024];
	}
	test1();
	test2();
	//HeapProfilerStop();
	std::cout << "over" << std::endl;
	return 0;
}
[root@localhost gperftools]# ls
CMakeCache.txt cmake_install.cmake main Makefile test.log.0002.heap
tmp.0002.heap
CMakeFiles CMakeLists.txt main.cc test.log.0001.heap tmp.0001.heap
tmp.0003.heap
[root@localhost gperftools]# pprof ‐‐text /bin/ls tmp.0001.heap
Using local file /bin/ls.
Using local file tmp.0001.heap.
Total: 101.0 MB
100.0 99.0% 99.0% 100.0 99.0% 0000000000400be1
1.0 1.0% 100.0% 1.0 1.0% 0000000000400b7b
0.0 0.0% 100.0% 1.0 1.0% 0000000000400bfb
0.0 0.0% 100.0% 101.0 100.0% __libc_start_main
0.0 0.0% 100.0% 101.0 100.0% _init
0.0 0.0% 100.0% 101.0 100.0% _init (inline)
[root@localhost gperftools]#
  • 一些环境变量的设置
HEAP_PROFILE_ALLOCATION_INTERVAL:程序内存每增长这一数值后就生成新的profile文件,改变量用于控制这
个数量,单位是字节,默认是1Gb
HEAP_PROFILE_INUSE_INTERVAL:程序如果一次性分配内存超过这个数值就生成新的profile文件,默认是100Mb
HEAP_PROFILE_MMAP:有时候程序申请内存的方式是通过mmap,sbrk系统调用而不是malloc free,如果想profile
这些内存,可以开启这个变量,默认是false。我们工程代码中就有些调用了mmap申请大块内存的地方,开启这个
环境变量,能更准确跟踪内存走向。
HEAP_PROFILE_MMAP_ONLY:如其名,只profile mmap,sbrk申请的内存
HEAP_PROFILE_TIME_INTERVAL:每隔多少s输出profile文件
[root@localhost gperftools]# env HEAPPROFILE=./kkk HEAP_PROFILE_ALLOCATION_INTERVAL=81920000
./main
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值