IPhone Instruments tool_detect Memory leakage

Source: Internet
Author: User

I recently used the Instruments tool and found it very helpful for tracking memory leaks in the game. Since I discovered that Instruments are so useful, I think writing an article about how to use it to track memory leaks can also help others.
What is memory leakage? Why do I need to care about memory leakage?
... This section is omitted...
Visit Wikipedia to obtain more information about memory leakage.
How do I know that the memory has been leaked?
Some memory leaks can be easily discovered by reading the code, and others are difficult. This is why Instruments is needed. Instruments has a "Leaks" tool that accurately tells you where memory Leaks have occurred so that you can locate and fix Leaks.
Example Program
I wrote an example program, which may leak memory in two places, one in Objective-C View Controller and the other in C ++ class. The routine can be obtained from here. The following code is extracted from the routine and contains the code for tracking memory leaks.
// Leaky excerpts-see GitHub for complete source
-(Void) viewDidLoad {
[Super viewDidLoad];
LeakyClass * myLeakyInstance = new LeakyClass ();
Delete myLeakyInstance;
MMyLeakyString = [[NSString alloc] initwithuf8string: "I'm a leaky string."];
[Self doSomethingNow];
}
-(Void) doSomethingNow
{
MMyLeakyString = [[NSString alloc] initwithuf8string:
"Look, another alloc, but no release for first one!"];
}
// Leaky excerpts-see GitHub for complete source
LeakyClass: LeakyClass ()
{
MLeakedObject = new LeakedObject ();
}
LeakyClass ::~ LeakyClass ()
{
}
I will first compile InstrumentsTest in Debug mode and run it on the iPhone. After completing this step, I will start Instruments.

When you start Instruments, you can select what you need from a bunch of Instruments tools. Select the iPhone on the left side and double-click "Leaks" in the icon on the right side:

Then you will see the following window:

Make sure that your iPhone is connected to your computer. in the upper left corner of the window, you will see a drop-down menu that says "Launch Executable ". Click it and make sure that your iPhone (not your computer) is selected as the active device. Move to "Launch Executable" and you will see a list of all installed iPhone programs. Find the program that you want to use the "Leaks" tool (in this example, InstrumentsTest) and click it.

You are ready. Click the red "Record" button to start the program and start recording each memory allocation operation in the program. It automatically detects memory leaks every 10 seconds.

You can change the time for automatic detection. You can also perform manual detection. (when detecting memory leaks, the program will pause for about 3-5 seconds. If you want to perform a test and a memory check, this pause will disturb you ). I usually set it to manual control and click the "Check for leaks" button when I need it (for example, Check it after loading the new game mode, when you exit the game and return MM, check ). Click "Leaks" and use the View-> Detail button in the upper right corner to set and View the option value. In this example, I set it to auto.

After the program runs for a period of time, the automatic memory detection will detect two memory leaks. Great! What should I do now?

Extended Detail View
Instruments is very lazy and does not clearly point out what to do next. Note the row of buttons at the bottom of the window. Have you seen the button consisting of two rectangles? When you move your mouse over it, the system prompts "Extended Detail View ".

Click this button, and a window will pop up on the right, which provides a variety of details about memory leakage. Click a memory leak. The Extended Detail View displays the complete call stack of the leaked Memory code. In the above example, click the first memory leak prompt, which occurs in [NSString initwithuf8string]. If you select the highlighted step in the call stack, you will see that the last call of the program is [InstrumentsTestViewController viewDidLoad].
Double-click a line in the Extend Detail View. It opens the XCode window and displays the problematic code. This is a great feature.

In this example, the first NSString allocation is leaked, and you need to do some processing. This is a very simple example, but it is more difficult to find out why a leak occurs. Let's take a closer look at the example. In viewDidLoad, we allocate the string to the memory, as shown below:
MMyLeakyString = [[NSString alloc] initwithuf8string: "I'm a leaky string."];
In dealloc, we use the following method to release
[MMyLeakyString release];
Your intuition may be that this will not leak, but the search code is useful to the location of mMyLeakyString. In doSomethingNow, it is used like this:
MMyLeakyString = [[NSString alloc] initwithuf8string:
"Look, another alloc, but no release for first one!"];
Note that we declare a new string and point the mMyLeakyString to it. The problem here is that we are not changing the memory that mMyLeakyString refers to releasing the memory that it originally points. So the original string is still in the heap, and we cannot release this part of memory. The release operation in dealloc actually releases the memory occupied by the string we declare in doSomethingNow, because this is what the pointer refers.
To fix this problem, we can change doSomethingNow to the following code:
-(Void) doSomethingNow
{
[MMyLeakyString release];
MMyLeakyString = [[NSString alloc] initwithuf8string:
"Look, another alloc, but released first one!"];
}
In this Code, the memory occupied by the first string is released before mMyLeakyString is specified to the new string. Re-compile and run the program, and you will see only one memory leak. Of course, there may be a better way to handle NSString in the project, but if you do so, you can fix this leakage problem.
Let's take a look at the second leak. Click the leak prompt to see what causes memory leakage. This leak was discovered by the LeakyClass: LeakyClass () constructor:

Double-click it in the call stack, and the problematic code will appear again in XCode.

We can see that a new LeakedObject object is declared in the constructor, but the Destructor is not deleted, which is not good. Each new operation requires a corresponding delete operation. So we change the destructor to the following:
LeakyClass ::~ LeakyClass ()
{
If (mLeakedObject! = NULL)
{
Delete mLeakedObject;
MLeakedObject = NULL;
}
}
Re-compile and run without memory leakage!
These two examples are simple, but they show that Instruments can be used to track memory leaks in Object-C and C ++.
Fix your memory leakage. Remember, a program without memory leakage is a good one.
Source http://www.mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/

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.