20

Firstly, I want to know how to install TCmalloc in Ubuntu. Then I need a program uses TCmalloc. Then I need a small program to show that TCmalloc is working better than PTmalloc.

Chen Li
  • 4,824
  • 3
  • 28
  • 55
sudo
  • 548
  • 1
  • 4
  • 16
  • 1
    What do you mean by "working better"? Smaller memory fragmentation? Faster performance? If it's performance, then it should be relatively easy to show that tcmalloc is better than ptmalloc, but if it's memory fragmentation it may be a bit harder. – juhist Mar 23 '15 at 08:23
  • In faster performance. – sudo Mar 23 '15 at 08:49
  • Related: https://stackoverflow.com/questions/6261201/how-to-find-memory-leak-in-a-c-code-project – Ciro Santilli OurBigBook.com Sep 10 '19 at 19:47

5 Answers5

11

I'll provide another answer since there's an easier way to install it than in the other answer:

Ubuntu already has a package for google perf tools: http://packages.ubuntu.com/search?keywords=google-perftools

By installing libgoogle-perftools-dev you should get all that is required for developing tcmalloc applications. As for how to actually use tcmalloc, see the other answer.

juhist
  • 4,210
  • 16
  • 33
11

To install TCMalloc:

sudo apt-get install google-perftools

To replace allocators in system-wide manner I edit /etc/environment (or export from /etc/profile, /etc/profile.d/*.sh):

echo "LD_PRELOAD=/usr/lib/libtcmalloc.so.4" | tee -a /etc/environment

To do the same in more narrow scope you can edit ~/.profile, ~/.bashrc, /etc/bashrc, etc.

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
  • 1
    My experience said, that only the `firefox` crashed, when *TCMalloc* is installed in system-wide manner. All other programms on Ubuntu 16.04 are totally happy with it. – Tomilov Anatoliy Jan 11 '18 at 13:37
  • To get the compile tools as well, install: ```sudo apt-get install google-perftools libgoogle-perftools-dev``` – AndrewStone Oct 09 '19 at 14:26
6

Install:

sudo apt-get install google-perftools

Create an application in eclipse or any other code composer

#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 << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!

  vector<BigNumber*> v;

  for(int i=0; i< 100; i++)
  {
    v.push_back(new BigNumber(i));
  }

  return 0;
}

This code will help you see how memory is leaking

Then add the library to your makefile

-ltcmalloc

when running the application, you want to create a heap file, so you need to add an environment variable HEAPPROFILE=/home/myuser/prefix and files prefix.0001.heap will be created in the /home/myuser path

Run the application and heap files will be created Examine heap files

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

Easy to see which objects leaked and where were they allocated.

Nic Cottrell
  • 9,401
  • 7
  • 53
  • 76
user3696279
  • 99
  • 1
  • 5
  • Some notes: (1) use `export` to set the variable. (2) if it's set properly the program will print "Starting tracking the heap" to stdout when it's started. – user202729 Jun 09 '18 at 11:58
3

If you would like to use tcmalloc only just for allocated memory optimization, not for analysis, you can do like this:

sudo apt -y install libgoogle-perftools-dev

cc -O3 -ltcmalloc_minimal -fno-builtin-malloc \
 -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -o main main.c
Arun
  • 19,750
  • 10
  • 51
  • 60
Dennis V
  • 574
  • 7
  • 19
1
  1. tcmalloc is in the google perf tool, installation guide could be found here.
  2. The example is included in the google perf tool
  3. see here, section Performance Notes
superK
  • 3,932
  • 6
  • 30
  • 54