Introduction and use of tcmalloc in gperftools

Source: Internet
Author: User
Tcmalloc (thread-cachingmalloc) is a memory management library in the Google-perftools tool. Compared with malloc in the standard glibc library, tcmalloc is much more efficient and fast in memory allocation, it can improve the performance in high concurrency and reduce the system load.
Google-perftools. When tcmalloc is used only, the other three components can not be compiled.
Note: using the thread memory pool method, small objects are allocated in the memory pool, and a large amount of memory space is allocated to optimize the allocation time.
For implementation principles, see http://goog-perftools.sourceforge.net/doc/tcmalloc.html.
Introduction
Tcmalloc is a memory management library developed by Google, which is faster than glibc's malloc. In general, ptmalloc2 can execute a malloc and free pair in NS, while tcmalloc can execute a malloc and free pair in 50 ns.
Tcmalloc can reduce lock contention among multi-threaded programs and achieve zero contention on small objects.
Tcmalloc allocates a local cache for each thread. A small number of addresses are allocated directly from the cache and garbage collection is performed regularly, return the idle memory in the local cache of the thread to the Global Control Heap.
Tcmalloc considers smaller than (<=) 32 K as a small object, and large objects are allocated in units of pages (4 K) from the Global Control Stack. That is to say, large objects are always page aligned.
A page in tcmalloc can store small objects of the same size. Small objects are allocated from the local memory linked list, and large objects are allocated from the central memory heap.
Install
The Installation Process of tcmalloc in Linux is as follows:
1) download the source code package from the Google source code website. The latest version is 2.0;
2) decompress the source code package
# Unzip gperftools-2.0.zip or
# tar zxvf gperftools-2.0.tar.gz
3) Compile the dynamic library
# cd gperftools-2.0
# ./ configure  --disable-cpu-profiler --disable-heap-profiler--disable-heap-checker 
--disable-debugalloc--enable-minimal
The preceding parameters are added to generate only the tcmalloc_minimal dynamic library. If you need all the components, run the following command:
# ./configure
#./Configure-H is used to view compilation options.
Compilation and installation:
# make&& make install
Copy the dynamic library of tcmalloc_minimal to the system directory during minimum installation:
# cplib/tcmalloc_minimal.so.0.0.0 /usr/local/lib
Create a soft connection pointing to tcmalloc:
# ls –s /usr/local/lib/libtcmalloc_minimal.so.0.0.0/usr/local/lib/libtcmalloc.so
Before starting the program, preload the environment variable settings of the tcmalloc dynamic library:
# exportLD_PRELOAD=”/usr/local/lib/libtcmalloc.so
Use losf to check whether the program has loaded the tcmalloc Library:
# lsof -n | greptcmalloc
The installation of tcmalloc in Linux is complete. In Windows, run vs (Version 2003 or later) to open the project gperftools. sln for compilation.
Use
Link libtcmalloc. So/libtcmalloc. A to the program, or set ld_preload = libtcmalloc. So. In this way, you can replace the malloc, free, realloc, and strdup memory management functions of the operating system with the functions in the tcmalloc library. You can set the environment variables as follows:
Tcmalloc_debug = <level> debugging level; value range: 1-2
Mallocstats = <level> indicates the memory usage status. The value ranges from 1 to 2.
Heapprofile = <PRE> specifies the data export file for memory leak check
Heapcheck = <type> heap check type, type = normal/strict/draconian
The tcmalloc library can also check for Memory leakage. There are two ways to use this function:
1) link the tcmalloc Library to the program. Note that the tcmalloc library should be finally linked to the program;
2) Set ld_preload = "libtcmalloc. So"/heapcheck = normal, so you do not need to recompile the program.
Enable the check function. There are two ways to enable the leak check function:
1) use environment variables to check the entire program: heapcheck = normal/bin/ls
2) Insert a checkpoint in the source code to check only some parts of the program. The Code is as follows:
Heapprofileleakcheckerchecker ("foo"); // start check
Foo (); // the part to be checked
Assert (checker. noleaks (); // end the check
Call Checker to create a memory snapshot and call checker. noleaks creates another snapshot and compares it. If the memory size increases or changes randomly, the noleaks function returns false and outputs a message to show you how to use the pprof tool to analyze specific memory leaks.
Run the memory check:
         #LD_PRELOAD=libtcmalloc.so HEAPCHECK=strict HEAPPROFILE=memtm ./a.out
After the execution is complete, the results of the check will be output. If there is any leakage, pprof will output how many bytes are leaked and how many times are allocated, it will also output a detailed list of the locations where and how many times of allocation.
Compare two snapshots:
# Pprof -- base = profile.0001.heap program name profile.0002.heap
Code for disabling the memory leak check when the memory leak is known:
void *mark =HeapLeakChecker::GetDisableChecksStart();
<Leaky code> // do not check for leakage
HeapLeakChecker::DisableChecksToHereFrom(mark);
Note: In some libc programs, you may have to close the check to work properly.
Note: The Memory leakage caused by array deletion cannot be checked, for example, char * STR = new char [100]; Delete STR ;.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.