Use TCMalloc stack check and tcmalloc stack check

Source: Internet
Author: User

Use TCMalloc stack check and tcmalloc stack check

In the previous article TCMalloc: Malloc of thread buffer, the working principle and characteristics of TCMalloc are explained in detail. Today, we translate heap-checking using tcmalloc to learn more about Tcmalloc stack check.

1. Automatic Memory leakage check

This technical document describes how to use the C ++ program to check the stack. This capability is very useful for Automatic Memory Leak Detection.

1.1 link stack checker

You can perform a stack check on any program linked to tcmalloc without re-compiling.

To catch all the memory leaks, tcmalloc must be linked to your executable program. The stack checker may misunderstand some memory of the Linked Library listed behind it. For example, it may falsely report the memory of these databases as memory leaks, but it does not.

Link tcmalloc to your program. In real time, you don't want to use the stack Checker to check whether it is safe. Your program will not run slowly because you have not used any stack check features.

You can run the stack check in programs that are not compiled by using LD_PRELOAD.

1 $ LD_PRELOAD="/usr/lib/libtcmalloc.so" HEAPCHECK=normal 

This is not recommended.

1.2 Enable stack check

There are two methods to enable memory check for executable programs:

  • For the stack check of the entire program, define the environment variable HEAPCHECK as the type of the stack check you need: Common, strict, or demanding. For example, check the memory of/bin/ls:
1 $ HEAPCHECK=normal /bin/ls2 % setenv HEAPCHECK normal; /bin/ls   # csh
  • Perform a stack check on some code of the program. You need to modify your code. Use the HeapLeakChecker object to package the code to be checked.Check. NoLeaks (). This only checks the memory of the included code. Set the local environment variable HEAPCHECK to enable the stack check.

The following is an example of the second usage. If Foo leaks any memory, the following code will terminate.

1 Foo();2 assert(checker.NoLeaks());

When an object is allocated, the scanner creates a stack data file. When checker. NoLeaks () ends, it creates another stack data file and then compares it to generate a new data file. If the new data file shows memory growth, NoLeaks () will return false and terminate the program. The prompt message about how to use the pprof command to obtain a detailed memory leak analysis is printed.

Note: pthread processing is incomplete. If the phtread thread is active during the construction or memory check, the stack check mistakenly treats it as a memory leak, one way to check the global stack is to ensure that all the sites except the main thread are launched at the end of the program.

1.3 disable known memory leakage

Sometimes your program has some memory leaks that you know and accept. You want the stack Checker to ignore them when you check the program, you can achieve this through the appropriate stack check object

1 #include 2  ...3 void *mark = HeapLeakChecker::GetDisableChecksStart();4 <leaky code>5 HeapLeakChecker::DisableChecksToHereFrom(mark);
 
Link: http://blog.csdn.net/chen19870707/article/details/40116379

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.