This paper describes several methods of memory management.
1. What is memory management?
Memory management is the optimization of memory resources.
2. Three ways to manage memory?
OBJECTIVE-C's memory management has three main ways of Arc (automatic memory Count), MRC (manual memory Count), memory pool.
What is 3.MRC? What are the principles of management? Existing problems?
MRC, full name mannulreference counting, Manual memory management (manual reference counting) requires the programmer to manually create an object to request memory and then manually release it.
MRC manages the principles of memory: Who creates, who releases.
In other words, in the process of use, who retain, who release, or who Alloc, who release.
When using MRC, when the reference count is 0 o'clock, it must be reclaimed, the reference count is not 0, it is not recycled, and if the memory count is 0, it will cause a memory leak if it is not recycled. If you want to use an object that has already been created (created by someone else), you cannot use it directly, you need to retain (let Count + 1), then release (count-1) after use, otherwise it will cause the wild pointer.
PS: The wild pointer (not knowing where the pointer is pointing), it points to a piece of memory that does not belong to it, it may be someone else's memory, may have been his own, but was released by others.
The way to avoid the wild pointer is to release the pointer equal to nil, this time is repeated release, will not prompt the error.
Nil: aka Empty, is the memory that the operating system holds, and no one can take away the memory.
What is 4.ARC? Existing problems?
ARC is a new feature of iOS 5, called Automatic Reference counting, which is the automatic reference count. Simply put, it is the code that automatically joins the Retain/release, and the code that originally needed to manually add a reference count to handle memory management can be automatically completed by the compiler.
With arc, if an object is pointed to by any of the strong pointers, it will not be destroyed. If the object is not pointed to by any strong pointer, it will be destroyed.
After using arc, methods such as Release,retain, Retaincount, and so on are not allowed.
Allows overriding of dealloc, but does not allow calls to [super Dealloc], which is called by default [Super Dealloc].
Although Arc gives us a lot of programming, there may be a memory leak. Here are two scenarios:
Circular reference: A has an attribute reference B,b has a property reference A, if all are strong references, two objects cannot be freed.
Dead loop: If there is an infinite loop in a viewcontroller, it also causes the Viewcontroller to be released even if the viewcontroller corresponding view disappears.
5. What is a memory pool?
Autorelease is similar to release, but not equivalent to it, release call after the reference count is immediately-1.
The Autorelease is written when the object is created, indicating that it joins the auto-release pool, and the reference count-1 is invoked when the pool is destroyed.
Automatically frees the pool and needs to be created manually.
When the [pool drain] method is called, all objects in the pool are called and the release is removed.
6. What are the advantages and disadvantages of three methods?
The advantage of arc is that it is relatively simple to use, without having to think about memory issues and so on, in the MRC era you need to retain an object you want to keep, and now
It's not necessary. Now the only thing to do is to point to the object with a pointer, and as long as the pointer is not empty, the object remains on the heap. When you point the pointer to a new value, the original object will be release once.
The disadvantage of arc is that if a circular reference occurs, a memory leak is prone. In addition, many third-party libraries are written in MRC and, if used, require some processing.
The MRC Advantage is that the programmer has a strong memory control and can solve the memory problem well.
The MRC disadvantage is the need to manually write a lot of code to manage memory, to ensure who creates who to release, to avoid null pointers, to be very careful.
Autorelease Advantages: It simplifies operations and is managed by an automated release pool. The object is destroyed only when the auto-free pool is destroyed, and the programmer does not have to worry about the object's destruction time, nor does it need to call release again.
Autorelease disadvantage: Because it is delayed release, it can prolong the life cycle of the object. So don't use it at will, because the object life cycle is prolonged, holding memory and slowing down.
Suitable for use in small memory objects.
Memory management of iOS face test questions