iOS memory management

Source: Internet
Author: User

Memory Management Guidelines:

(i) Principles

As long as someone is still using an object, the object will not be recycled: if you want to use this object, then you should let the object reference count +1, when you do not want to use this object, let the object's reference count minus one;

(ii) who created, who release

(1) If you create an object through Alloc,new,copy, you must call the release or Autorelease method

(2) You don't have to be responsible if you don't create it.

(iii) who retain, who release

As long as you call the retain, no matter how this object is generated, you have to call release

(iv) Summary

After the beginning and finish, there is a minus. Once you have an object counter plus 1, you should make it last-1.

1.

Alloc object is assigned a reference count of 1
Reference count for retain objects +1
Copy copy of an object into a new object (new memory address) reference count of 1 original object count unchanged

Release object reference count-1 if 0 frees memory
Autorelease object reference count-1 if 0 is not released immediately, released at the nearest pool
NSLog (@ "smessage retaincount:%u", [smessage Retaincount]);
[Smessage retain]; 2
NSLog (@ "smessage retaincount:%u", [smessage Retaincount]);
NSLog (@ "smessage retaincount:%u", [smessage Retaincount]);
NSLog (@ "smessage retaincount:%u", [smessage Retaincount]);
The principle of memory management is to balance the final reference count,
Memory leaks If last reference count is greater than 0
If the reference count equals 0 also operates on the object, memory access failure occurs, crash so try to set as nil

Both of these problems are serious, so be sure to pay attention to memory release and set to nil after use
2.
Each project has a main.m file: The contents are as follows:
int main (int argc, char *argv[]) {

NSAutoreleasePool * Pool = [[NSAutoreleasePool alloc] init];
int retVal = Uiapplicationmain (argc, argv, nil, nil);
[Pool release];
return retVal;
}
The main function of the C language is obvious:
NSAutoreleasePool is used to do autorelease variable release, said before, Autorelease will not be released immediately, when he reached the nearest pool release will check Reatin count is not 0, 0 is released.
When we add a lot of autorelease variables to a piece of code, we should add a
Autoreleasepool, no other time.
A Autorelease object is often returned when an object is returned because the OBJECTC object is dynamic memory and has no
Stack concept, so you cannot return an object to the stack as a C + +, only with the Autorelease object.
3.
It's not as simple as it is, you might need to call another function to assign a variable in one function.
There are two choices: class member variables and usage properties
@interface Testmem:nsobject {
Testobject *m_testobject; Member variables
Testobject *testobject; Member variables
}
The member variables are consistent with the memory management above, but the balance of reference count plus and minus is maintained in different functions.
So you have to check each time you assign the last time you've been assigned. Is it possible to invoke
When do I use attributes?
1. Make the members public.
2. Outlet is generally declared as a property (this memory is in the system control, but we should still do the same operation, will speak later)
3. If many functions need to change this object, or the function triggers many times, it is recommended to use attributes. Let's see what it looks like when the property function expands:

Assign
-(void) Settestobject:(ID) newvalue{
Testobject= NewValue;
}
Retain
-(void) Settestobject:(ID) newvalue{
if (testobject!= newvalue) {
[Testobject release];
testobject= [newvalue retain];
}
}
Copy
-(void) Settestobject:(ID) newvalue{
if (testobject! = newvalue) {
[Testobject release];
Testobject = [newvalue copy];
}
}

iOS memory management

Related Article

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.