google tcmalloc 内存池(不定长内存池)使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wushuangge/article/details/80926505

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

猜你喜欢

转载自blog.csdn.net/wushuangge/article/details/80926505
今日推荐