Objective-C questions related to memory management

Source: Internet
Author: User

Objective-C questions related to memory management
Crash occurs in iOS applications. More than 90% of the causes are memory problems. In a project with dozens or even hundreds of classes, it is extremely difficult to find out memory problems. Understanding common memory problems can reduce the chance of errors. Memory problems are reflected in three aspects: memory overflow, abnormal wild pointers, and memory transition and release.
During the interview, interviewers often ask questions about memory. Here, I will share with you two simple questions.
Interview Question 1:

    @autoreleasepool {        for (long i = 0; i < largeNumber; i++) {                Person *per = [[Person alloc] init];                [per autorelease];        }    }
LargeNumber is a large number. Is there a memory problem with this question? If yes, please solve the memory problem without modifying the written code. We all know the principle of Memory Management: If you use alloc, copy, and retain for an object, you must use the corresponding release or autorelease. At first glance, this question includes alloc and autorelease, which correspond to each other. No problem. But let's give it a closer look. Although autorelease reduces the reference count by one, it does not immediately subtract one. Its essential function is to place the object in the automatically released pool closest to him, when the automatic release pool is destroyed, the release message is sent to every object in the automatic release pool. The question is here autorelease. Because largeNUmber is a large number and autorelease cannot reduce the reference count by one immediately, memory overflow may occur before the loop ends. The solution is as follows:
    @autoreleasepool {        for (long i = 0; i < largeNumber; i++) {            @autoreleasepool {                Person *per = [[Person alloc] init];                [per autorelease];            }        }    }

Add an automatic release pool inside the loop to ensure that each object is released in time.
Interview Question 2:
    @autoreleasepool {        NSString *per = [[NSString alloc] init];        [per retain];          [per retain];          per = @"aa";           [per release];        [per release];        [per release];    }
This problem also has a memory problem. The following problems exist: 1. memory overflow problem. 2. Objects pointing to the constant area do not need to be release. Let's analyze this question. It seems that alloc, retain, and release correspond to each other. It seems that there is no problem. But let's analyze the statement per = @ "aa. The pointer variable per originally points to a new heap zone space, but after a new value is assigned to per, the point of per changes from the original point to the heap zone space to the constant zone. After pointing to the constant area, send the release message to the per. It is to release the constant area, and the constant area does not need to be released at all. As a result, the space in the original heap area is not released, memory overflow may occur.


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.