How to debug Memory leakage

Source: Internet
Author: User

Http://www.cocoachina.com/ B? P = 113

 

Since leopard, Apple has added a powerful performance debugging tool in the xcode series, called instruments. Instruments is a very innovative debugging tool. If you have used garageband, a very easy-to-use music creation tool in ilife, you will find that the interfaces of the two are very similar. The UI of the development tool can be used as an imaging music creation software, and only Apple can think of it.

Many functions are provided in instrument.ProgramOptimization and debugging. For beginners, although the interface of this software is extremely friendly, it is totally different from any development and debugging tools in the past, if there is a simple tutorial, it is easier to use instruments for debugging through an example.

 

This articleArticleA simple example will show you how to use the instruments tool to solve the memory leakage problem in the application, which is especially suitable for beginners to read. Before reading this article, if you are not familiar with objective-C memory management, we recommend that you read the previous two articles on this site:

 

Before objective-C 2.0, you need to know about the obj-C memory management rules.

And

About objective-C 2.0 garbage collection

 

In this way, we can better understand how memory leakage occurs, and better understand how to solve this problem quickly through this article.

 

This is the latest article by Matt long. The original Article is in English and has been translated into Chinese on this site.

 

Original article address:

Http://www.cimgf.com/2008/04/02/cocoa-tutorial-fixing-memory-leaks-with-instruments

 

When I write the programCodeIn the hope of releasing the new version as soon as possible, annoying changes to memory leakage, code optimization, and bug removal are my main tasks recently. The interesting part of creating new functions and solving new problems in program development has passed. Now, it takes a lot of time and effort to do things.

 

I know it is worthwhile to spend some time optimizing these aspects, because the program will be more stable and colorful, but it is really a very hard job to optimize and solve these problems. Fortunately, with the release of leopard, Apple has provided some new tools. For such debugging, we can use a tool called instruments to make it easier.

 

As usual, I wrote a demo program to show you how debugging works. You can download it here. In this tutorial, I will only tell you how to use instruments to detect memory leaks. This tool is not difficult to use, but you may not be familiar with some details. After reading this tutorial, you will get used to using this powerful tool.

 

Run instruments in xcode

 

You can directly run instruments and open your project, but the simpler method is to call instruments directly in xcode. The specific operation method is to open the demo project in xcode, or your own project (you need to compile it first), and then select Run-> start with performance tool-> leaks in the menu.

 

While studying this project, Marcus and I (another author of cimgf) listed some things you need to remember when using instruments.

 

-The breakpoint will not be interrupted.

 

Instruments uses the debug information in your debug project, but it will not be automatically interrupted in the breakpoint you set. This is because when you use the menu to read programs from instruments, instruments uses the current execution file path as the startup path and calls it from outside xcode. If you want to use a breakpoint, you can turn off instruments and execute the program.

-Nslog content is not displayed in the debugger window.

If you want to see the content in nslog, You need to manually enable the terminal program.

 

-Memory leakage

 

We expect that if we allocate memory for a new nsstring * object, we can directly see the memory leakage. This is our purpose, but it is not what we think. The following code does not cause any memory leakage in our tests.

 

 

Nsstring*String= [[Nsstring Alloc]Initwithstring:@ "Leaker"];

 

The reason is,NsstringIt cannot be modified.@After the operator allocates the string memory, the compiler helps us improve program efficiency. The compiler automatically assigns the same address to strings with the same content.

 

(Cocoachina explanation: that isStringAnd@ "Leaker"The address is the same after compilation by the compiler. If you have read the previous article, you will know"Leaker"This string will never be released during the program running. As a result,StringYou don't need to release it, so there is no memory leakage problem. However, this is only the optimization of the compiler and cannot be ensured in any case .)

 

in this example, nsstring is never leaked, so this example is useless. It does not mean that nsstring will never be leaked, therefore, when you do not need nsstring , call the -release method to release them. However, in our test, nsstring Memory leakage has never been detected. When we replace nsstring with nsmutablestring , as expected, memory leakage begins to appear.

 

Detect memory leakage

 

After instruments reads your program, you will see that the objectalloc track begins to fill the content with time changes. To detect leaks in the project, click the leaks track and observe the leak status category. This classification changes over time to detect memory leakage. Similarly, you can also click "check for leaks now" under the check manually category to manually detect memory leaks.

 

When the Memory Leak Detection starts, you will see leaked objects in the list. If you want to see details about memory leaks, click the objects in the list, click the extended detail button () at the bottom of the screen ().

Click it and you will see the stack trace list on the right side of instruments. Check the code similar to this in the list. The colors in your instruments list may be different from those in the example.

 

(You Need To Find A list item similar to this type. The top is the method. The following is the prompt: program name, number of rows .)

 

When you find the location where the memory is leaked, double-click instruments to automatically switch the number of leaked programs to xcode.

 

To modify the memory leakage, make sure you have released the object. For details about how to release the instance, refer to the article on this site:

 

Before objective-C 2.0, you need to know about the obj-C memory management rules.

 

Conclusion

 

Discovering and modifying problems is not the most interesting part of development, but when you do this, the results are very beneficial. Instruments provides Linear Tracking for your program to help you track any part of the program you are concerned about. If you want to learn more about instruments, click the library button on the toolbar at the top of the instruments menu to see which debugging functions are provided.

 

 

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.