With tcmalloc, Google also provides powerful tools for system resource profiling.
Here, let me explain how to use tcmalloc and some interesting experiments.
Here, my test code used in this page:
https://github.com/sshtel/practical_gperftools
1. What is tcmalloc?
Thread-caching malloc(tcmalloc) uses memory pool to manage memory.
In a word, although programmers allocate or release memory frequently, system call for memory allocation or release is not actually run.
Instead, tcmalloc merely search and return pointer of proper memory space which is already allocated for the process.
If only tcmalloc fail to search memory from the memory pool, it asks memory allocation to the OS.
Introduction to the Thread-Caching Malloc:
http://gperftools.googlecode.com/svn/trunk/doc/tcmalloc.html
2. How to use?
You can just download source code here.
gperftools project: https://code.google.com/p/gperftools/
If you build gperftools, you will get static library file, named libtcmalloc.a (if you use Linux)
According to manual, you are surely recommended to use static library, not dynamic library.
And build your application.
$ g++ your_program.cpp libtcmalloc.a -o your_program
This is my sample Makefile:
https://github.com/sshtel/practical_gperftools/blob/master/sample/test001/Makefile
**NOTE: When compiling with programs with gcc, that you plan to link
with libtcmalloc, it's safest to pass in the flags
-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free
http://google-perftools.googlecode.com/svn/trunk/README
3. Process memory usage when you use tcmalloc
When you first use tcmalloc, you will experience interesting phenomenon.
First of all, you will see a little bit increase of memory usage of your program.
Secondly, you might think that your memory is not actually returned to the system after asking memory release such as delete and free.
Here, let me show you a sample code and change of memory usage.
Using a sample code below, I recorded change of a process memory for both of two test cases, using tcmalloc and not.
https://github.com/sshtel/practical_gperftools/tree/master/sample/test002/
This graph shows change of actual physical memory usage of a process. (I recorded VmRSS field of /proc/pid/status of Linux system)
This graph explains how tcmalloc holds memory even though you try to release memory of the process.
4. What I learned from practical use of tcmalloc
When you use tcmalloc, you have to consider a total memory usage of the process.
For most time of running your process, your process will keep maximum usage of memory with tcmalloc.
This does not happen forever actually, but I will explain it later.
However, if there are other processes and you design your program not considering this, you would get some tragic problem like Out Of Memory.
In other words, when you use tcmalloc, your process will keep memory for longer time than before.
In conclusion, you need to pare down memory usage or optimization is necessary.
And build your application.
$ g++ your_program.cpp libtcmalloc.a -o your_program
This is my sample Makefile:
https://github.com/sshtel/practical_gperftools/blob/master/sample/test001/Makefile
**NOTE: When compiling with programs with gcc, that you plan to link
with libtcmalloc, it's safest to pass in the flags
-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free
http://google-perftools.googlecode.com/svn/trunk/README
3. Process memory usage when you use tcmalloc
When you first use tcmalloc, you will experience interesting phenomenon.
First of all, you will see a little bit increase of memory usage of your program.
Secondly, you might think that your memory is not actually returned to the system after asking memory release such as delete and free.
Here, let me show you a sample code and change of memory usage.
Using a sample code below, I recorded change of a process memory for both of two test cases, using tcmalloc and not.
https://github.com/sshtel/practical_gperftools/tree/master/sample/test002/
This graph shows change of actual physical memory usage of a process. (I recorded VmRSS field of /proc/pid/status of Linux system)
This graph explains how tcmalloc holds memory even though you try to release memory of the process.
4. What I learned from practical use of tcmalloc
When you use tcmalloc, you have to consider a total memory usage of the process.
For most time of running your process, your process will keep maximum usage of memory with tcmalloc.
This does not happen forever actually, but I will explain it later.
However, if there are other processes and you design your program not considering this, you would get some tragic problem like Out Of Memory.
In other words, when you use tcmalloc, your process will keep memory for longer time than before.
In conclusion, you need to pare down memory usage or optimization is necessary.
No comments:
Post a Comment