How memory is managed in OC

Source: Internet
Author: User

Memory management in OC mainly consists of three ways arc (automatic memory count), manual memory count, memory pool;

1) ARC: This way is similar to Java, in the execution of your program, there is always an expert in your back to help you clean up the garbage, you do not have to consider when it began to work, how to work, you just understand, I applied for a memory space, when I no longer use thereby this memory becomes garbage, I will completely forget it, anyway, that man will help us to clean up the garbage, but unfortunately, that man needs to consume certain resources, in carrying equipment, resources are tight goods, so the iphone does not support this feature.

Resolution: Created by Alloc-inital, after the creation of reference counter +1, and thereafter every retain reference counter +1, then do the corresponding number of times in the program release is good.

2) Manual memory Count: That is, after a period of memory is applied, there is a variable to save the number of times used, we temporarily call it a counter. When the counter becomes 0, then it is time to release the memory, for example, after a period of memory in program A is successfully requested, then this counter will change from 0 to 1 (we call this process alloc), then program B also needs this memory, Then the counter changes from 1 to 2 (we call this process retain) and then program a no longer needs this memory, then this counter will be reduced by 1 (we called this process to release), program B does not need this memory, So also subtract 1 of the counter (this process is also called release) when the system (i.e. foundation) discovers that the counter member becomes 0, then the memory recycle program is called to recycle this memory (we call this process Dealloc), by the way, If there is no foundation, then the maintenance counters, freeing memory and so on will need you to do it manually.

Workaround: Usually created by static methods of the class, the function name does not appear in the Alloc or Init typeface, such as [NSString string] and [Nsarray Arraywithobject:], after the creation of the reference count +0, after the function out of the stack, This is equivalent to a local variable on a stack, and of course the lifetime of the object can be extended by retain.

3) Memory Pool: You can control the timing of memory requests and recoveries by creating and freeing memory pools.

Workaround: The autorelease is counted into the system memory pool, the memory pool can be nested, each memory pool needs to have a create release object, as written in the main function, the use is very simple, if [[[[NSString alloc]initwithformant:@] Hey you! "] Autorelease], a NSString object is added to the most internal system memory pool, and when we release the memory pool, the objects are freed.

How memory is managed in OC

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.