IOS Performance Tuning series: use Zombies to dynamically analyze zombie objects in the memory and optimize zombies

Source: Internet
Author: User

IOS Performance Tuning series: use Zombies to dynamically analyze zombie objects in the memory and optimize zombies

Hardware wide: The fourth article in the IOS Performance Tuning series, which is expected to be updated continuously over 20 articles.

The first two articles, IOS Performance Tuning series: Analyze static analysis and IOS Performance Tuning series: using Instruments to dynamically Analyze memory leaks, focus on memory leaks. This article is the opposite, the focus is on overreleased objects in the memory ).

The title of this article has been entangled for a long time, whether to write EXC_BAD_ACCESS error debugging, or write Analysis of zombie objects in the memory, and finally select a Duang ~ Duang ~ .

I saw a post on the forum today. What I encountered was the problem to be analyzed in this article. I just used it to explain the Bug scenario:

I believe that many people have encountered the EXC_BAD_ACCESS error before using ARC. This error can be understood as accessing released objects, which Apple calls botnets.

For example, if ARC is not enabled, the following code is used:

NSString* hello = [NSString stringWithFormat:@"Hello"]; NSLog(@"What you say is %@",hello);  [hello release]; 

The hello object is not manually assigned, but added to the automatic release pool. The release pool is responsible for release. Therefore, the EXC_BAD_ACCESS error will occur when the third row calls release.

After enabling ARC, the EXC_BAD_ACCESS error can be avoided to a large extent, but it is also possible. For example, C ++ code is used in IOS, C ++ objects are not managed by ARC.

The EXC_BAD_ACCESS error is not as easy to locate as the access NULL pointer. It is often difficult to find the error point when an error is reported. Therefore, XCode provides a separate Zombies tool in Instruments to analyze such errors.

Principles of Zombies Analysis

 

Like other tools using Instruments, click the Product menu Profile of XCode to start Instruments:

You can see the introduction below the Zombies tool to find zombie objects that have been excessively released.

The Zombies tool works in the same way as setting the NSZombieEnabled environment variable. After Zombies is started, NSZombieEnabled is set to True internally.

If NSZombieEnabled is enabled, it will replace the default dealloc implementation with a zombie, that is, when the reference count is reduced to 0, the zombie implementation will convert the object to a zombie object. When you send a message to a zombie object, it does not send an error message to the previous Crash or produce an incomprehensible behavior, it will display a piece of log and automatically jump into the debugger, so we can find the specific or probably which object is released by mistake.

Steps for Zombies Analysis

 

1. Start Instruments and select Zombies;

2. Run the test case that previously generated EXC_BAD_ACCESS again until the program crashes. If an EXC_BAD_ACCESS error occurs, the following interface will appear:

3. Move the arrow to view the error details. For example, you can view the memory operation process of the object, such as malloc, autorelease, retain, and release;

4. Check the detailed history at the bottom. Select the corresponding line to locate the corresponding code and find the code that produces the error:

It is easy to find out the error code line by checking the information provided by Zombies. Zombies is only useful when an EXC_BAD_ACCESS error is generated.

Manually set the NSZombieEnabled environment variable:

 

XCode also provides the method to manually set the NSZombieEnabled environment variable. However, setting NSZombieEnabled to True will result in increased memory usage and affect Leaks tool debugging, this is because NSZombieEnabled replaces released objects with zombie objects.

Click Edit Scheme in the Product menu to open the page, and then select the Enable Zombie Objects check box:

It is generally not recommended to perform manual settings, but should use Zombies tool for debugging.

 

Record, for better yourself! Reprinted please indicate the source!

Related Article

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.