一.安装
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()函数来实现调用构造函数和析构函数.

Logo

更多推荐