UnderstandingCocoaMediumMemoryHow to manage it is what we will introduce in this article. If you don't talk about it, go to the topic. Let's talk about it todayCocoaAboutMemoryManagement issues. This issue is important for both desktop development and mobile development.
Especially for iPhone development. Why more? Because the iPhone is a mobile terminal, it does not have as much CPU as a Mac, nor a lot of memory, which makes the iphone program very expensive in terms of memory overhead. Therefore, if we do not apply any restrictions, the program we develop may use all the memory, so that we do not have free memory to run other programs, no one wants to be unable to answer the call while running the program, right. Therefore, here we should emphasize the importance of memory management.
Of course, with the upgrade of the iPhone, its CPU will become more and more powerful, and its memory will grow more and more. Even so, we need to develop good programming habits and always consider the memory problem, develop a perfect program without memory leakage.
Today, we will discuss the issue of memory in two aspects.
1. How to create an object and allocate memory to it?
2. How to release the memory occupied by objects?
I. How to allocate memory
We use the alloc and init methods to create and initialize objects, which includes allocating memory to objects. Let's look at the code below.
- Fraction *myFract = [[ Fraction alloc ] init];
Fracion is a class. The above statement creates a new object myFract of the Fraction class and assigns it a memory space for storing variables in it. This is a simple statement. You can use this statement when you want to create a new object for any class.
There are many other methods that can be used to obtain objects, just as we did before. Below are some examples.
- NSString *newDisplay = [display.text stringByAppendingString:digit];
- NSArray *keys = [dictionary allKeys];
- NSString *lowerString = [string lowercaseString];
- NSNumber *n = [NSNumber numberWithFloat:42.0];
- NSDate *date = [NSDate date];
The above methods will get objects, and there are many similar methods. Now, the key problem is that we allocate memory to these objects. Who is responsible for releasing them? Let's take a look at the following.
Ii. Memory release
Now, let's continue with the problem we left above. To solve this problem, I will introduce the following concepts.
A. Reference Counting)
B. Ownership)
C. Autoreleasepool)
Reference Counter
We are talking about the release of the Object Memory. When do we need to release the object? The only criterion for determining whether an object needs to be released is that the object is no longer used and has no value to use in the program. We use the reference counter to determine whether the object is still in use in the program.
The reference counter is used to track the number of references of an object. It works like this: when an object is created, set its reference count to 1, every time another object or method in the program calls it, its counter is added with 1. When the call ends, the counter is reduced by 1, which means the use of this method on the object is over. When the counter of an object is 0, it indicates that this object is not called by any other objects and methods in the program, that is, the program no longer needs it, at this time, we can safely release it.
There are two very important methods, one is retain persistence) and the other is release ). When an object is referenced, we send the retain Method to the object to add 1 to its counter. after the reference is complete, we will send the release method to him to reduce its counter by 1. in this case, the retain and release methods appear in pairs. Each retain corresponds to a release. Similarly, we also have a release method that corresponds to the initial value 1 of the counter, in this way, the counter can be reduced to 0 and the object can be released.
Ownership)
You may feel abstract and difficult to understand. Next we will introduce the concept of ownership, which will help you understand the counter. In the counter introduction, we say that an object is referenced by an object. For example, if the object myFract is referenced by the object myNumbe, we say that myNumbe has ownership of Fract. Correspondingly, when myNumbe no longer uses Fract, it will give up its ownership of Fract. When there are no objects or methods in the program that have ownership of Fract, the object Fract can be released. It should also be emphasized that an object can be owned by multiple objects or methods, that is, multiple objects or methods in the program can simultaneously have ownership of the same object.
Now, we will link the counter with the ownership.
When object A is called by object B (or method), object B sends A retain message to object A to obtain ownership of object A. At the same time, counter A adds 1;
After B calls A, object B sends A release message to A to give up the ownership of A. At the same time, the timer of A is reduced by 1;
When the counter of A is 0, that is, no one in the program has ownership of A, then A can be safely released.
Do you understand now?
Auto Release pool
Let's talk about the last concept. We need to associate this problem with the first two concepts because they are all part of Cocoa memory management. Let's talk about the automatic release pool.
The system uses an automatic release pool to track objects so that they can be released later. After using some objects, you are not sure whether you will use them again. If they are released at this time, they will be very troublesome to use in the future. The automatic release pool can solve this problem. Because you can add an object to the automatic release pool by marking it. When the automatic release pool is released, the objects it contains will also be released together. When an object is added to the release pool, it is not released, so you can use it, but you do not have to worry about forgetting to release it, you should have added it to the release pool.
The following statement is used to create a release pool:
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
This statement is used to release the automatically released pool:
- [pool drain];
To add an object to the Auto Release pool, use the following statement:
- [ myFract autorelease] ;
Note that adding an object to the release pool does not add 1 to the counter of the object.
Iii. Summary of memory management rules
A releases an object and can release the memory occupied by it. If your program creates many objects at runtime, you should pay attention to the release of objects. The good rule is, release the created or maintained objects when they are no longer used.
B does not necessarily destroy an object when sending a release message. This object is destroyed only when the reference counter of an object is 0. The system releases the memory occupied by the object by sending a dealloc message.
C. The automatic release pool is used to automatically release objects in the pool when you release itself.
D. If you no longer need an object in your method, but you need to return it, send an autorelease message to it and mark it for future release.
E. If you use the alloc and copy methods to directly create an object, you are responsible for releasing it. Each retain object should be release or autorelease.
Summary: UnderstandingCocoaMediumMemoryThe following describes how to manage the content.Cocoa memoryThe basic concept of management is also the most important concept. PairMemoryManagement focuses on program development, and we need to emphasize its importance over and over again. Finally, I hope this article will help you!