IOS Foundation-----Memory Management

Source: Internet
Author: User

Objective-c memory Management method has a reference counting mechanism, garbage collection mechanism, the automatic release pool. When there is alloc, there is release. iOS should be crash (flash back), 90% is due to memory problems. In the possession of a number of what are the hundreds of classes?, to find memory problems is extremely difficult, learn memory management, can help us reduce the error rate. Memory problems are reflected in two?? : Memory overflow, wild pointer exception.

Reference counter

  In Xcode4.2 and later versions, because of the introduction of the arc (Automatic Reference counting) mechanism, Xcode can automatically add the memory release code to your code when the program is compiled, and if you write a manual release code, Xcode will error, So in today's content if you're using a post-Xcode4.2 version (believe that most of your friends now have a higher version than this), you have to manually turn off arc, which will help you understand the OBJC memory recovery mechanism. The memory management mechanism in OBJC is just as important as the content of pointers in C, and it is not difficult to develop a program, but good programs weigh more heavily on memory management, which often consumes less memory and runs more smoothly. Although the new version of Xcode introduces Arc, many times it does not completely solve your problem. Turn off arc in Xcode: Project Properties-build settings--Search "garbage" to find objective-c Automatic Reference counting set to No.

The reference count also points to the arc (auto Reference counting) Auto reference count and the MRC (Manual Reference counting) manual reference count.

Auto Reference count: The compiler feature of IOS 5.0, which allows the user to open space only, not to release space. It's not garbage collection! It is still the MRC, but the compiler helps the programmer to add the freed code by default.

The opening and releasing of memory is controlled by the program code. Relative to garbage collection, the control of the memory more flexible, can be released in time when the need to release, more requirements for programmers, programmers should be familiar with the mechanism of memory management.

garbage collection mechanism  

Programmers only need to open up memory space, do not need to be released in the form of code, the system to determine which space is no longer being made, and reclaim these memory space in order to allocate again. The entire recycling process does not need to write any code, and the system completes the garbage collection. Ja─a in the development of the direct is the garbage recycling technology.

OC Draw the counting mechanism to manage the memory, each object has a lead? Counter, to record the number of times the current object is cited. When a new citation is directed to an object, the citation counter is added 1, and when the citation is removed, the count is reduced by 1. When the citation is counted to 0 o'clock, the object's space is reclaimed by the system. Retaincount gets the citation count of the object.

For example, the toilet into a person, the reference count will be added 1, out of a person will be reduced by 1, and so on people all come out and become 0, equivalent to release, the system is recycled.

Methods that affect reference counting

1, + Alloc open up memory space, let the open memory space of the citation count from 0 to 1.

2,-retain to Count plus 1, if the object before the count is 1,retain to 2, if the citation count is 5,retain after the change to 6.

3.-Copy copies the contents of a certain object, copies the new object, the original object's citation count is unchanged, and the new object's citation count becomes 1.

Freeing memory

iOS5 before, make? NSAutoreleasePool. The release pool class creates an object.

After the iOS5 , no longer recommended to make? NSAutoreleasePool class, make? @autoreleasepool {} override.

Out curly braces, and automatically frees Chicai to send release messages to individual objects.

IOS Foundation-----Memory Management

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.