iOS Memory leak Detection Tool Pleaksniffer

Source: Internet
Author: User

Http://www.cocoachina.com/ios/20160706/16951.html

This document is licensed for transfer from Mrpeak technology share (public number: Mrpeaktech)

New OBJECTIVE-C Memory Leak Detection Tool Pleaksniffer,github address.

Background

Read the Weread team a few days ago to share a memory leak detection tool mleaksfinder, vaguely think of earlier I have also had the idea of writing such a gadget, do not know for what reason to forget it. After carefully read the Mleaksfinder source, understand the implementation of ideas, and found that their original ideas are not the same, and finally last weekend beat procrastination to put the idea before the code, also born this feature similar memory leak Detection tool pleaksniffer. Readers are advised to read the following Mleaksfinder this blog in detail first.

Why do you want to rebuild wheels

I actually tried the mleaksfinder in the company's project and investigated 2 leaks??。 According to the date in the Mleaksfinder code file, this project has at least six months to start, and in the study has been verified in practice, in terms of functionality and stability should have a good performance.

After writing the Pleaksniffer, found the same memory leak with Mleaksfinder, the different ideas of the code arrived at the same end, the fun to write code. New ideas may also be a little bit of a contribution to the iOS development community if you inspire more ideas.

Mleaksfinder at this stage can investigate the leakage of Uiviewcontroller and UIView, my earlier ideas can also be recursive to find out the uiviewcontroller of all property leaks, And in the Pleaksniffer and the company's project has been a preliminary validation, which is a small supplement to the Mleaksfinder function.

The meaning of this kind of tool

Before we discuss the meaning of such tools, let's be clear:

Without the use of the leak Detection tool in instrument, there is no easy 100% accurate memory leak detection method.

But this kind of tool still has its existence value, the harm of memory leak does not need to repeat, if a tool can detect a possible memory leak in 80% scene, and this kind of detection does not bring any side effect (do not affect production environment code), why not use it.

Most people underestimate the likelihood that they will cause accidental memory leaks when they write code. Retain Cycle,block Strong reference, Nstimer release improper, these common errors are still very easy to appear in our code, instrument each use to spend a bit of energy, suitable for regular large-size troubleshooting. It's usually better to use mleaksfinder,pleaksniffer tools like this to do real-time monitoring and provide free advice.

Pleaksniffer Realization Idea

Most of the time we are writing uiviewcontroller,uiviewcontroller like a root node, holding and managing a lot of child node objects, the life cycle of these child nodes depend on the Controller,controller release, They are also released. Simply describe their relationship with a picture:

Depending on the design pattern used by each application (MVC,MVP,MVVM, etc.), the controller holds different property. Here we use the MVP as an example, the controller contains objects that include various view objects, and Presenter,model objects. Of course, it is possible for each object to hold more sub-objects.

Pleaksniffer is based on such a hypothesis:

If the controller is released, but the child objects that it once held are still present, then these sub-objects are the suspect targets of the leak.

Of course, this hypothesis is not a 100% applicable truth, different engineers write code in a way that differs greatly in style, some will make some uiviewcontroller into a single case (personally think this is not a good idea.) Some will cache some of the view (even if the controller has been released), and there will be other scenarios that are not considered. But in more than 80% scenarios, we release the resources they hold after the controller ends its life cycle. This time Pleaksniffer can play a useful role and give you some free advice on leaks.

So how is it that after the controller is released, he knows that the object it holds is not released?

A small trick can accomplish this: a sub-object (such as view) creates a weak reference to the controller, and if the controller is released, the weak reference is also set to nil. How do we know that the sub-object was not released? Send a PING notification to ping this sub-object every short period of time with a single object, and a pong notification if the child object is alive. So the conclusion is that if the controller of the child object does not already exist, but can still respond to the ping notification, then this object is a suspect leak object. A complete structure can be represented by:

Notification removal takes an opportunity, where we use the associated object mechanism to regenerate each sub-object into a proxy object and remove the notification from the dealloc of the proxy object.

Of course, when to judge the life cycle of an object begins and when it is judged to end, a selective mechanism is needed. View,controller,property are not the same.

Pleaksniffer Adopt A conservative strategy, through the runtime mechanism of objective C, recursively will be a controller all the strong reference property to find, and install proxy monitoring ping notification. Under my test, I can basically find out the scene of the property leaking.

Pleaksniffer the use of a simple answer, after the installation through the pod, the following code activation can be.

1234 #if MY_DEBUG_ENV[[PLeakSniffer sharedInstance] installLeakSniffer];[[PLeakSniffer sharedInstance] addIgnoreList:@[@"MySingletonController"]];#endif

Addignorelist can add a few special ignore lists, such as a singleton that does not correctly predict a leaking object. Remember to use debug macros to wrap the code above, and do not bring these leak detection code into the online environment.

If a suspected leak is detected, Pleaksniffer prints a log in the console:

Controller leak: Detect Possible controller Leak:%@

Other object leaks: Detect Possible Leak:%@

For more details please refer to the code: GitHub address.

iOS Memory leak Detection Tool Pleaksniffer

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.