OBJECTIVE-C basic knowledge of memory management

Source: Internet
Author: User

Memory Management principles:

    • As long as someone else is using this object, the object will not be recycled;
    • As long as you want to use this object, then you should let the object reference counter +1;
    • When you do not want to use this object, you should let the object reference counter-1;
    • If you create an object through Alloc,new,copy, you must call the release or Autorelease method;
    • As long as you call retain, you should call release at the end.

The scope of OC memory management:

    • Manages any inherited NSObject objects that are not valid for other basic data types. Memory management is primarily the memory management of objects in the heap area. The essential reason is that the object is stored in the heap, the other local variables are mainly stored in the stack, when the code block ends, where all local variables are reclaimed, the pointer to the object is also recycled, the object has no pointer object, but also exists in memory, causing memory leaks.

The role of the reference counter:

    • Reference counters are the basis for judging objects to not be recycled (there is an exception: when the object value is nil, the reference count is 0, but the space is not reclaimed).

Operations on Reference counters

    • Sends a message to the object for the corresponding counter operation.
    • Retain message: To make counter +1, the method returns the object itself.
    • Release message: Making the counter-1 does not mean releasing the object
    • Retaincount message: Gets the object's current reference counter value%LD

There are three ways to increase the value of the object reference counter:

    • Show creating an object using Alloc
    • Show using Copy[withzone:] or Mutablecopy[withzone:] Copy Object
    • Show using retain

Destruction of objects

    • When an object's reference counter is 0 o'clock, it is destroyed and the memory it consumes is reclaimed by the system.
    • In OC, the instance method Dealloc is used to free the heap space that the instance variable in the object points to.
    • When the object is destroyed, the system automatically sends an DEALLOC message to the object, typically overriding the Dealloc method, where the associated resources are freed.
    • Once the Dealloc method is overridden, [super Dealloc] must be called and placed at the end of the code block.
    • Once the object is recycled, the storage space he occupies is no longer available, and sticking to it can cause the program to crash.

Attention:

    • If the object's counter is not 0, the memory it consumes will not be reclaimed during the entire program run.
    • Any object that has just been born, the reference counter is 1. When you create an object using Alloc.new.copy, the object's reference counter defaults to 1

Auto Free pool:

    • Auto-free pool is a memory auto-recycle mechanism of OC, which can collect some temporary variables through the automatic release pool to be released uniformly
    • Whenever an object receives a autorelease message, the object is placed in the auto-free pool, and when the auto-free pool is freed, the object inside him receives a release message
    • When an object receives a autorelease message, the system places the object in the nearest auto-free pool. When you need to empty the auto-free pool, you can send a release message to the auto-release pool.
    • Simply put, when you send an autorelease message to an object, the object is not released immediately.
    • Use autorelease need to note: First, send too many autorelease messages, just as you send too much release, when emptying the auto-free pool, may cause memory failure. Second, although the release message can be replaced with autorelease, for system performance reasons, you can use release to try not to use the autorelease, because the auto-free pool does much more work than directly using release. Finally, the automatic release of the pool's deferred release mechanism can result in useless memory consumption.
    • When you create an object, you do not need to use release or autorelease if you are not using Alloc. But if you display the use of alloc, then you should not forget to use release or Autorelease.
    • When the auto-free pool is released, objects in the auto-free pool may be freed. This is because when the automatic release of the pool is released, the system sends a release message to each object in the release pool, which causes the value of the object's reference counter to be reduced by 1, and if the value of the object reference counter is reduced to 0, the system sends an DEALLOC message to the object to destroy the object completely.

Autorelease Disadvantages:

    • It is not possible to precisely control the time the object is destroyed, lengthen the object's life cycle, and reduce the memory processing efficiency. You can use Autorelease when you are not sure when an object is to be released, or if the object of the operation takes up memory for an hour, but you use release when the operation consumes a large memory object. In general, release priority is used.

OBJECTIVE-C basic knowledge of 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.