IOS Memory Management

Source: Internet
Author: User

The following are some of the common aspects that need attention in memory management that I have summarized. There is no strict data source, and some parts may be ambiguous, incorrect, or missing, you can talk to me about making progress together.

Using this method can greatly reduce memory problems and reduce the number of crashes caused by memory usage errors.

It is recorded here for your reference.

1. Keep the property/member variables of the object consistent with the reference count of the object itself

A) autorelease should not be used for member variable initialization. If it is autorelease, it is best to retain it, keep the reference count as 1, and then release it in dealloc.

B) For attributes, except the custom setter method and dealloc method, self must be used. testproperty = @ "testpropert" to assign values, so that you can keep the reference count controlled by yourself as 1; otherwise, you need to perform release before each assignment. For example

[_ Testproperty release];

_ Testproperty = testproperty;

C) corresponds to a). All member variables must be released in dealloc.

D) after each member variable is release, it must be set to nil unless the value is assigned immediately to prevent other references from being judged.

Dealloc can be processed as appropriate. It is okay not to set the member variable to nil. In a simple way, all released variables in dealloc can be set to nil.

E) All obj-C objects in the property should be set to retain (except delegate), and the reference count should be plus 1, instead of assign.

For bool, Int, and core ** frameworks, you need to use assign without changing the reference count.

However, attributes such as delegate must be declared using assign. The current object should not manage the reference count of proxy objects.

For example:

@ Property (nonatomic, copy) nsstring * testproperty;

@ Property (assign) boolflag;

@ Property (assign) ID <property> delegate;

F) The basic principle is who allocates and who releases it. Pay more attention when interacting with the c framework.

G) Pay attention to the issue of using obj-C object memory release in struct. Make sure that the reference count is changed to 0 or autorelease when you are free.

2. Use of attribute keywords

A) differences between retain and copy

The code generated by retain is similar to the following:

- (void)setTestProperty:(id)testProperty{    if(_testProperty != testPropert)    {        [_testProperty release];        _testProperty = [testPropert retain];    }}

Therefore, retain will release the old value, set it to a new value, and retain will keep its reference count. We can also see that when the member Variable _ testproperty references an external variable, it will always retain once.

Copy will generate a new object pointer, for example

The address of an nsstring * is 0x0011, And the content is @ "testproperty"

After the copy operation, a new nsstring * is generated. The address is 0x1122, the content is the same, and the retaincount of the new object is 1. operations on the old object are not performed, the old object has not changed.

So we can understand that retain is actually a copy of the pointer, and copy is a copy of the content. However, because nsarray and nsdictionay objects are also pointers, they are actually pointers inside the array.

In general, you can declare the nsstring attribute as copy. For example, @ property (nonatomic, copy) nsstring * test;

B) assign, nonatomic

The assign and setter Methods assign values directly without retain operations. They are usually used for basic types or references such as delegate (to prevent cyclic references ).

Nonatomic: Non-atomic access without synchronization. multi-threaded concurrent access improves performance. If this attribute is not added, the default value is atomic transactions. The lock is added to the object instance level. Nonatomic is also widely used.

C) @ synthesize xxx; generate the corresponding setter/getter Method for XXX

D) The system will assign an attribute with no corresponding variables by default, as shown in the following example:

@ Property (nonatomic, copy) nsstring * testproperty;

@ Synthesize testproperty;

In this example, no variable corresponds to the testproperty attribute, but you can still use self. testproperty to assign values and values.

This is because the system will assign a _ testproperty variable by default. However, we recommend that you add a member variable to make it clearer.

3. c frameworks such as CoreGraphics. Framework and coretelephony. Framework are used in obj-C programming.

The Class C framework uses cfrelease/cfretain to control the object lifecycle. It can be directly converted from the obj-C object. For example:

Nsstring * STR = [nsstrig stringwithformat: @ "% @", @ "abcdefg"];

Cfstringref ref = (nsstring *) [STR retain];

In this case, you need to use cfrelease (REF); to release it;

4. directly use the C language, you need to control the memory allocation and release.

Malloc and free must be strictly matched. If they are used as function parameters, you must use the variables pointing to pointers as function parameters. Release is provided externally.

For more information, see the Bible 《In-depth understanding of computer systems, Which is very thorough and detailed.

5. Special scenarios

A) @ "123" and initwithstring generate a very large reference count. The release operation is only performed to correspond to the previous init and will not be actually released.

B) after the object is release, it will not be released immediately, for example:

[OBJ release];

Nslog (@ "OBJ = % @", OBJ); // This statement will not cause a crash

Nslog (@ "OBJ = % @", OBJ); // calls at this time will cause a crash

C) performance issues of the NSAID utoreleasepool

The NSAID utoreleasepool also occupies certain performance. In xcode4.2, the @ autoreleasepool {} block is used by default to replace the NSAID utoreleasepool. Its performance is relative

For the NSAID utoreleasepool, there has been a major improvement (I remember reading a document saying it will increase by more than 6 times ).

By yytong

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.