Android Development Small Problem Summary

Source: Internet
Author: User

Introduction to usein the process of writing programs, inevitably encounter memory leaks, this time if manually find memory leaks, do not say the method is not universal, is really to own manual search is also very time and energy. Admittedly, we can use some tools, and we're surprised to find that these tools are useful (such as Intel's Memory leak Detection tool), but because often these tools are cumbersome to install, and we write programs that are not very large, we may be able to find a smaller approach. Microsoft provides such a method, we only need to add a few lines of code in the program, we can find the problem of memory leaks, and then we can locate the memory leaks (it can be achieved with a few lines of code, magic!). )。 so how to implement, the main functions are listed first:_CrtDumpMemoryLeaks,_CrtMemCheckpoint,_crtmemdifference
  • Crtdumpmemoryleaks : All currently not destroyed objects (no delete and free), output to the debug window by default
  • _crtmemcheckpoint: Saves the current state of all objects that are not destroyed
  • _crtmemdifference : Comparison of two _crtmemcheckpoint saved state, return differential value
simply use _CrtDumpMemoryLeaks to detect objects that are not currently released, but if the program is larger, you need to determine if there is a problem with a certain program, you need the next two parameters. _CrtMemCheckpoint Save is the result of _CrtDumpMemoryLeaks , if we save a state before and after a program, So by comparing these two states we can tell if there is a memory leak in this program.
Using the example
    • Enable memory leak debugging support
       #define _crtdbg_map_alloc#include <stdlib.h> #include <crtdbg.h 
      where the # define statement maps the underlying version of the CRT heap function to the corresponding debug version. If you omit #define statement, the memory leak dump will be simplified.
After you use these statements to enable the debug heap function, you can set a call to _CrtDumpMemoryLeaks before an application exit point to display a memory leak report when the application exits:
_CrtDumpMemoryLeaks ();
If you want to detect a memory leak when you add an exit point for your program, you can set the debug option by setting the call to add the function _CrtDumpMemoryLeaks yourself at each exit point:
_CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_leak_check_df);

By default, the debug information for the output will be in the Debug window, and of course you can customize the output location by _CrtSetReportMode .
    • Compare Memory leak status
Another technique for locating a memory leak involves taking a snapshot of the memory state of the application at a critical point. To take a snapshot of the memory state given to a point in the application, create a _crtmemstate structure and pass it to the _CrtMemCheckpoint function:
_CrtMemState S1;_crtmemcheckpoint (&S1);
_CrtMemCheckpoint will populate the structure with the current memory state.
If you want to see the contents of the output _crtmemstate structure, you can use the _ Crtmemdumpstatistics function:
_CrtMemDumpStatistics (&S1);//_ crtmemdumpstatistics output memory state dump as follows://0 bytes in 0 free blocks.//0 bytes in 0 Normal  blocks.//3071 bytes in + CRT blocks.//0 bytes in 0 Ignore blocks.//0 bytes in 0 Client blocks.//largest number used: 3071 bytes.//Total allocations:3764 bytes.

To determine if a memory leak has occurred in a section of code, you can take a snapshot of the memory state before and after this section, and then use _ Crtmemdifference to compare two states:

_CrtMemCheckpoint (&S1);//memory allocations take place Here_crtmemcheckpoint (&S2); if (_CrtMemDifference (&am P;S3, &S1, &s2)) {_crtmemdumpstatistics (&S3);}
_CrtMemDifference compares the memory states S1 and S2, returning results in (S3), that is, the difference between S1 and S2.
One way to look for a memory leak is to first place the _CrtMemCheckpoint call at the beginning and end of the application, and then use _CrtMemDifference to compare the two results. If _CrtMemDifference shows a memory leak, add more _crtmemcheckpoint to use binary search to divide the program until a leak source is found.

    • Post a result diagram of your own use

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.