Memory leaks on IOS face questions

Source: Internet
Author: User

What is wrong with this code, how to modify for (int i = 0; i < Somelargenumber; i++) {NSString *string = @ "ABC"; string = [String lowercasestring];st ring = [string stringbyappendingstring:@ "XYZ"]; NSLog (@ "%@", string);}

====== ========= ========= ========= ========= ========= = = =

Honestly, when I saw this code, I didn't see any problems.

If Somelargenumber tends to infinity, it has to produce an infinite string of nsstring types.

Let's talk about the difference between stacks:

(1) Management: For the stack, is automatically managed by the compiler, without our manual control, for the heap, the release of work by the programmer control, easy to produce memory leak. (2) Application size: The space can be obtained from the stack is small, the heap is the data structure to the high address extension, is a discontinuous memory area. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large. (3) Fragmentation problem: For the heap, frequent new/delete is bound to cause memory space discontinuity, resulting in a large number of fragments, so that program efficiency is reduced. For the stack, there is no problem, because the stack is advanced out of the queue, they are so one by one correspondence, so that there will never be a memory block from the middle of the stack (4) Allocation method: The heap is dynamically allocated, there is no statically allocated heap. Stacks are allocated in 2 ways: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is assigned by the ALLOCA function, but the dynamic allocation of the stack is different from the heap, and his dynamic allocation is released by the compiler without our manual implementation. (5) Allocation efficiency: The stack is the data structure provided by the machine system, the computer will support the stack at the bottom: allocate the address of the special register storage stack, the stack stack has a special instruction execution, which determines the efficiency of the stack is high. The heap is provided by the C + + function library, and its mechanism is very complex.

Thus, the frequent opening up of memory space, and freeing up memory that will inevitably lead to memory leaks.

Let's take a look at the revised code.

for (int i = 0; i<1000;i++) {NSAutoreleasePool * pool1 = [[NSAutoreleasePool alloc] init]; NSString *string = @ "ABC"; string = [String lowercasestring];string = [string stringbyappendingstring:@ "XYZ"]; NSLog (@ "%@", string);//release pool [Pool1 drain];}

In OC, we talked about nsautoreleasepool this thing, the elements in NSAutoreleasePool are actually turned into autorelease by default, better let the memory be released.


Memory leaks on IOS face questions

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.