https://github.com/gperftools/gperftools
使用VS编译项目,将 libtcmalloc_minimal.lib 放到项目的lib目录下
添加库目录:
$(ProjectDir)lib
附加依赖项:
libtcmalloc_minimal.lib
强制符号引用:
__tcmalloc
测试代码
#include <Windows.h>
#include <iostream>
//#include <tbb/tbbmalloc_proxy.h> //这是tbb包含的头文件, win32系统默认malloc是只要注释该头文件引用
using namespace std;
// 执行多少轮
#define MAX_SCALE (4000)
//每轮生成 2 4 8 16 32 64 126 256 512 1024 2048 4096 。。。。共16种规格的内存
#define MAX_SIZE 16
DWORD WINAPI worker(LPVOID lpThreadParameter){
int iterations = MAX_SCALE;
void* ps[MAX_SCALE][MAX_SIZE];
while(iterations-- ) {
for (int i = 0; i < MAX_SIZE; i ++){
int size = (int)pow(2.0, i);
void * p = malloc(size);
memset(p,0,size);
ps[iterations][i] = p;
}
}
iterations = MAX_SCALE;
while(iterations-- ) {
for (int i = 0; i < MAX_SIZE; i ++){
free(ps[iterations][i]);
}
}
return NULL;
}
void main(){
int scale = 4;
cout << "start " << endl;
HANDLE* handles = new HANDLE[scale];
cout << "HANDLE newed" << endl;
DWORD start = ::GetTickCount();
for (int i = 0; i < scale; i ++){
DWORD threadID;
handles[i] = CreateThread(0, 0x1000000000, worker, NULL, NULL, &threadID);
}
WaitForMultipleObjects(scale, handles, true, INFINITE);
DWORD end = ::GetTickCount();
cout << end-start << endl;
}
测试结果(毫秒)
tcmalloc 4 thread
1155
905
1045
952
msvcrt 4 thread
1997
2714
2777
2496
tbb 4 thread
905
889
920
858
tcmalloc 1 thread
796
780
843
858
msvcrt 1 thread
686
686
702
686
tbb 1 thread
734
718
686
702
tbb 4t 8000loop (crash!!!)
1732
1825
1794
1841
ms 4t 8000loop
16660
23462
21855
11013
tc 4t 8000loop
2200
2262
2200
2059