For our. NET developers,. NET provides an automatic memory management mechanism. We do not need to care about memory management. However, the iPhone cannot be developed. This article will briefly describe the memory management mechanism and methods and some features of objective-C.
Manual Memory Management
Both cocoa and objective-C classes are subclasses of nsobject. There are several nsobject methods for memory management. The alloc method allocates a piece of memory space for the object. The dealloc method is used to release the object space. However, the dealloc method will never be used in our code, because it will be called for you to release the memory space during runtime. What you need to do is reference counting. We will introduce what is reference counting later.
In addition to alloc and dealloc, nsobject also has two methods for reference counting: retain and release. The retain method adds 1 to the retaincount variable, and the release method minus 1 to the retaincount variable. When alloc is used to allocate a memory space for the object, retaincount is 1. During the lifecycle of this object, this object may be referenced by other variables. However, when a new variable points to this object, you should call the retain method so that the runtime will know that a new reference points to this variable, this object has its right to use during its lifetime. This is called "ownership" by objective-C developers ". For example:
Foo * myfooone = [[Foo alloc] init]; // retaincount is 1
Foo * myfootwo = myfooone; // myfootwo points to this object
// Retaincount is still 1
[Myfootwo retain]; // call the retain method. The runtime will know that myfootwo points to this object, and retaincount is 2.
In the above Code, myfootwo obtains the ownership of the foo object by calling the retain method. In the lifecycle of this object, there are many variables to point to and reference it. Variables pointing to this object can also be removed using the release method. The release method will tell the runtime that I have used this variable and no longer need it. The retaincount count is reduced by 1.
When the retaincount count of an object is greater than or equal to 1, the object will be maintained during running. When the retaincount of an object is 0, the object is released at runtime, and the occupied memory space is reclaimed.
Shows the lifecycle of a foo object. The Foo object first allocates a memory space in the memory and is referenced by myfooone. At this time, the retaincount of the foo object is 1.
Foo * myfooone = [[Foo alloc] init];
The second referenced variable points to the foo object. This referenced variable then calls the retain method, which is actually the retain method of the foo object. The retaincount of the foo object is changed to 2.
Foo * myfootwo = myfooone;
[Myfootwo retain];
Then, when myfooone reference is unnecessary, call the release method to revoke the ownership of the foo object. The retaincount of the foo object becomes 1.
[Myfooone release];
However, when myfootwo is not needed, call the release method to revoke the ownership of the foo object. The retaincount of the foo object becomes 0.
Memory leakage
We often declare objects in a method. Let's look at the example below:
-(Void) mymethod {
// Incorrect method
Nsstring * mystring = [[nsstring alloc] init]; // retaincount = 1
Foo * myfoo = [[Foo alloc] initwithname: mystring]; // retaincount = 1
Nslog (@ "foo's name: % @", [myfoo getname]);
}
In the above method, we allocated memory space for mystring and myfoo. After the method is executed, the two variables are out of the scope and are no longer valid. However, this method does not have the releases objects. Therefore, the memory occupied by these two variables is not released at runtime. Unless your application ends, the memory occupied by these two variables is always unavailable. We call it memory leakage.
To prevent memory leakage. Whenever we create an object or create a copy of an object, we must release it through the release method.
-(Void) mymethod {
Nsstring * mystring = [[nsstring alloc] init]; // retaincount = 1
Foo * myfoo = [[Foo alloc] initwithname: mystring]; // retaincount = 1
Nslog ("foo's name: % @", [myfoo getname]);
[Myfoo release]; // retaincount = 0 So deallocate
[Mystring release]; // retaincount = 0 So deallocate
}
Weak reference
See the following example:
-(Void) mymethod {
// An incorrect method
Foo * myfooone = [[Foo alloc] initwithname: @ "James"]; // retaincount = 1
Foo * myfootwo = myfooone; // retaincount still 1
[Myfooone release]; // retaincount = 0 So deallocated
Nslog ("Name: % @", [myfootwo printoutname]); // runtime error
}
Nyfootwo points to the foo object, but does not call the retain method. It is a weak reference. The above code will report an error during running. Because myfooone calls the release method. The value of retaincount is 0. The memory space of the object is recycled during runtime. Then, if myfootwo calls printputname, an error is reported. See the description.
Summary:This article briefly introduces the manual knowledge of objective-C, such as memory management, memory leakage, and weak references.