Introduction to Tcmalloc and use of Windows under installation

Source: Internet
Author: User
Tags perl script

The following is transferred from http://www.cnblogs.com/xsln/p/Introduction_TcMalloc.html

Please look directly at the back of the questions that need attention.

Introduced:

Tcmalloc (Thread-cachingmalloc) is a memory management library in the Google-perftools tool, and tcmalloc is much more efficient and faster in memory allocation than malloc in the standard glibc library. It can improve performance in high concurrency situations and reduce the load on the system.

Tcmalloc is more efficient than GLIBC's malloc. In the case of a PC with a 2.84GH clock, the glibc malloc needs 300ns of time to perform the malloc/free pair, and tcmalloc only needs 50ns to do the same (data comes from official documents).

Tcmalloc uses the method of thread memory pooling, where small objects (<=32k) are allocated in a memory pool, use more allocated memory space to optimize allocation times, and periodically perform garbage collection operations. Large objects (>32k) are allocated directly in the global control heap. Tcmalloc can effectively reduce the problem of locking contention between multiple threads, and even 0 contention can be achieved for small objects.

Under Windows, install using:

  Gperftools can be run in VC + + 7.1 (Visual Studio 2003) or later versions.

First download and unzip gperftools on the official website, for: http://code.google.com/p/gperftools/downloads/list The author downloads is: Gperftools-2.1.zip

This package contains the Readme_windows.txt documentation, which includes a detailed tutorial on how to use it.

Open and compile the Gperftools.sln in the gperftools-2.1 directory, the compilation will generate multiple EXE files for testing, and you can manually perform these files to detect if all of them are passed on your machine. It'll also create binaries, nm-pdb and addr2line-pdb, which you should install in the same directory you install the ' Pprof ' perl script. (Description of the contents of the document, the author did not touch these two files)

After compiling, the Libtcmalloc_minimal[-debug].dll and corresponding LIB files are generated in the Release/debug directory. You can use these two files to replace malloc/new in other projects.

The author uses the environment is VS2005, to use this DLL, you need to add the following line to the project: "Libtcmalloc_minimal.lib"/include: "__tcmalloc", settings such as:

intuitive but not comprehensive testing:   
#include <Windows.h> #include <iostream> #define COUNT 1000*1000void func () {    size_t j = 0; C4/>for (size_t i = 0; i < COUNT; + +i)    {        if (J > 1001)        {            j = 0;} int * pInt = (int*) m Alloc (i * sizeof (int)); free (pInt);}} void Main () {DWORD tstart, tEnd; tstart = timegettime (); func (); tEnd = timegettime (); printf ("%lu\n", tEnd - Tstart);}          

For the above code, the run time takes more than 10,000 milliseconds (measured 14846) when the Tcmalloc is not used (the __TCMALLOC flag is removed), and the runtime takes only 100 milliseconds (measured 151) after using Tcmalloc.

Notice:In addition, for Windows, the author in the gperftools-2.1 directory in the install file found below need to pay attention to the text, if required, please go to the install file to find and read in detail:

* * Windows (MSVC, Cygwin, and MinGW):

Work on Windows is rather preliminary: we haven ' t found a good the
To get the stack traces in release mode on Windows (that's, when FPO
IS-enabled), so the heap profiling may isn't reliable in that
Case. Also, heap-checking and CPU profiling do not yet
All . But as on other ports, the basic Tcmalloc library
functionality, overriding malloc and new and such (and even
Windows-specific functions like _aligned_malloc!), is working fine,
At least with VC + + 7.1 (Visual Studio 2003) through VC + + 10.0,
In both debug and release modes. See Readme.windows for
Instructions on the install on Windows using Visual Studio.

Tcmalloc's related documents address: http://goog-perftools.sourceforge.net/doc/tcmalloc.html

Http://dirlt.com/tcmalloc.html

Attention:

Note that it is best to download the latest version of Gperftools to http://code.google.com/p/gperftools/instead of using the 2.1 version mentioned by the authors below.

Currently the latest version is:

Version 2.1 may not be compatible with new VS (e.g. 2012). (to the official address above see the Gperftools update log appears to be confirmed)

Although the author used a simple program in the VS2012 Environment gperftools-2.1 (corresponding to compile version 2012) can be the malloc, free, new, delete management, but I use it to the large program will appear in the case of death, the reason is unknown, But in google has been searched netizens said may be due to delete/delete[] caused.

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.