首先,我想知道如何在Ubuntu中安装TCmalloc。那么我需要一个使用TCmalloc的程序。然后,我需要一个小程序来证明TCmalloc比PTmalloc工作得更好。
发布于 2015-03-23 08:53:08
我将提供另一个答案,因为安装它的方法比在另一个答案中更容易:
Ubuntu已经为google工具提供了一个软件包:http://packages.ubuntu.com/search?keywords=google-perftools
通过安装libgoogle-perftools dev,您应该可以获得开发tcmalloc应用程序所需的所有内容。至于如何实际使用tcmalloc,请参见另一个答案。
发布于 2018-03-13 11:28:10
安装:
sudo apt-get install google-perftools在eclipse或任何其他代码编写器中创建应用程序
#include <iostream>
#include <unistd.h>
#include <vector>
#include <string>
using namespace std;
class BigNumber
{
public:
BigNumber(int i)
{
cout << "BigNumber(" << i << ")" << endl;
digits = new char[100000];
}
~BigNumber()
{
if (digits != NULL)
delete[] digits;
}
private:
char* digits = NULL;
};
int main() {
cout << "!World!" << endl; // prints !World!
vector<BigNumber*> v;
for(int i=0; i< 100; i++)
{
v.push_back(new BigNumber(i));
}
return 0;
}这段代码将帮助您了解内存是如何泄漏的。
然后将库添加到makefile中
-ltcmalloc运行应用程序时,需要创建堆文件,因此需要添加环境变量HEAPPROFILE= /home/myuser /前缀和文件前缀.0001。堆将在/home/myuser路径中创建。
运行应用程序,将创建堆文件,检查堆文件。
pprof helloworld helloworld.0001.heap --text
Using local file helloworld.
Using local file helloworld.0001.heap.
Total: 9.5 MB
9.5 100.0% 100.0% 9.5 100.0% BigNumber::BigNumber
0.0 0.0% 100.0% 0.0 0.0% __GI__IO_file_doallocate容易看出哪些对象泄漏以及它们被分配到哪里。
发布于 2017-10-11 19:16:51
安装TCMalloc
sudo apt-get install google-perftools为了在系统范围内替换分配器,我编辑/etc/environment (或从/etc/profile,/etc/profile.d/*.sh导出):
echo "LD_PRELOAD=/usr/lib/libtcmalloc.so.4" | tee -a /etc/environment要在更窄的范围内这样做,您可以编辑~/.profile、~/.bashrc、/etc/bashrc等。
https://stackoverflow.com/questions/29205141
复制相似问题