Implementing the IOS Memory Detection Tool

Source: Internet
Author: User

Memory leaks are a common problem in iOS development, where common memory detection methods are sorted out. a static detection method

Using the Xcode analysis feature, Product->analyze

Using static detection, you can check out some obvious memory that is not freed, including memory leaks at the beginning of nsobject and CF.

Disadvantage: Unable to detect block-caused memory leak problem


two dynamic detection method

Using instruments



three dealloc re-test

Rewrite the Dealloc method, when the interface returns or the object is destroyed, to determine whether to call the


Four third-party detection methods

Mlleaksfinder

Mainly examines the UI aspect the leakage, the integration is simple, the merit is as follows:
1. Different from the instrument analysis function, need manual observation, this tool automatic detection, found that there is leakage can be real-time prompt, although mainly for the UI, but for general engineering, memory leaks in the scene or the majority of the UI, so can solve a large part of the problem.
2. Memory leaks can be easily detected during the development of new features and the process of resolving bugs.

The principle is as follows: (1) detect whether the Viewcontroller has been released after the pop
(2) Set the flag bit to No in viewwillappear
(3) Set the flag bit to Yes after navgation pop
(4) To judge the mark position in the Viewdiddisappear


Based on this principle, we simply implement a simple memory detection tool of our own:

(1) exchanging the viewwillappear and viewdiddisappear of Viewcontroller with runtime technology;

(2) in Viewwillappear to set the flag to NO, the code is as follows:

-(void) Sw_viewwillappear: (BOOL) animated{
    [self sw_viewwillappear:animated];
    
    Objc_setassociatedobject (self, &kleakmemory, @ (NO), objc_association_retain_nonatomic);
}
(3) Exchange Navigationcontroller popviewcontrolleranimated, and in which the flag bit is set to Yes
-(Nullable Uiviewcontroller *) sw_popviewcontrolleranimated: (BOOL) animated{
    uiviewcontroller *VC = [Self sw_ Popviewcontrolleranimated:animated];
    
    extern const char *kleakmemory;
    Objc_setassociatedobject (self, &kleakmemory, @ (YES), objc_association_retain_nonatomic);
    
    return VC;
}

(4) To judge the mark bit in the viewdiddisappear of Viewcontroller, and to detect whether Dealloc calls

-(void) Sw_viewdiddisappear: (BOOL) animated{[self sw_viewdiddisappear:animated];
    if (Objc_getassociatedobject (self, &kleakmemory)) {[Self willdealloc]; }}-(BOOL) Willdealloc {if (![
    Super Willdealloc]) {return NO;
return YES; }
/**super willdealloc*/
-(BOOL) willdealloc{
    ///Here Note that using weakself prevents circular references
    __weak typeof (self) weakself =  self;
    Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
        __ Strong typeof (Weakself) strongself = weakself;
        [Strongself Showmsg:nsstringfromclass ([Strongself class])];
    Return YES
}

-(void) ShowMsg: (NSString *) msg{
    Uialertview *alertviewtemp = [[Uialertview alloc] initwithtitle:@ "Leak"
                                                            Message:msg
                                                           delegate:nil
                                                  cancelbuttontitle:@ "OK"
                                                  Otherbuttontitles:nil];
    [Alertviewtemp show];
}

Demo

If you have questions, you are welcome to point out.

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.