google tcmalloc 記憶體池(不定長記憶體池)使用
阿新 • • 發佈:2019-02-15
一.安裝
tcmalloc (google-perftools) 是用於優化C++寫的多執行緒應用
tcmalloc在gperftools之中,故想要使用tcmalloc,就得先安裝gperftools。在linux下,其安裝步驟如下:
1 tar xzvf gperftools-2.7.tar.gz
2 cd gperftools-2.7
3 ./configure –enable-frame-pointers
4 make
5 make install
二.使用
cmalloc的使用
#include <iostream>
#include <gperftools/tcmalloc.h>
int main()
{
char *p = (char *)tc_malloc(2 * sizeof(char));
tc_free(p);
p = nullptr;
return 0;
}
但是要注意一點:對於類物件進行申請記憶體和釋放記憶體時,不會呼叫建構函式和解構函式.可以在類中寫一個reset()函式來實現呼叫建構函式和解構函式.