OBJECTIVE-C Memory Management

Source: Internet
Author: User

Objective-c Objects for memory management

1. Value types: such as int, float, struct and other basic data types.

    • Value types are placed in the stack, occupying a contiguous memory space in memory, followed by the advanced post-out principle, so no fragmentation occurs.
    • Managed by System allocation

2. Reference type: Objective-c object

    • Reference types are placed in the heap, and memory allocations follow a certain allocation algorithm, but no matter how good the algorithm is, fragmentation will always occur.
    • Memory allocation and release is managed by programmer operations

3. Mutual conversions between value types and reference types

    • Boxing: The process of converting a value type to a reference type is called boxing, such as wrapping int as nsnumber.
    • Unboxing: The process of converting a reference type to a value type is called unpacking, such as converting a nsnumer to an int.

Ii. methods of objective-c memory management Objects

1. Reference counting

    • Represents the number of times an object is referenced, through which it can be used to determine whether an object is recycled.
    • If the reference count is 0, the object is reclaimed and not 0 is not recycled.
    • The reference count is reduced by 1 when the object executes alloc, new, copy, or retain when the reference count is added to the 1,release.

Iii. How to manage memory objective-c

1. Manual memory management mechanism: MRC

    • In MRC mode, all objects need to manually add retain, release code to manage memory.
    • Using MRC, you need to follow the principles of who created, who recycles. That is who alloc, who release, who retain, who release.
    • When the reference count is 0, it must be recycled, the reference count is not 0, it cannot be reclaimed, and if the reference count is 0, but not recycled, it can cause a memory leak. If the reference count is 0, continuing to release will result in a wild pointer. To avoid the presence of wild pointers, we set the pointer to nil first when we release it.

2. Automatic memory management mechanism: ARC

1) property variable modifiers in arc mode

    • Strong reference strong: equivalent to retain, after use, the counter value of +1.
    • Weak reference weak: Equivalent to assign, use does not change the value of the counter, the address that the pointer points to is freed, it is automatically set to nil.
    • Retain: Reference count value +1, which is the number of object holders +1.
    • Assign: Use when setting variables, do not design reference count.
    • Copy: Copies the object.
    • ReadWrite and ReadOnly: Restrict the permissions of the visitor.
    • Atomic and Nonatomic: Atomic and non-atomic operations. Atomic operations apply to multi-threaded, secure access to shared resources. Non-atomic operations are more useful for non-multi-threading and can improve performance.

2) automatic release of the pool

Automatic memory release declares a block of code using the @autoreleasepool keyword, and if an object calls the Autorelase method at initialization time, when the code block executes, Objects that have called the Autorelease method in a block will automatically call the release method once. This has the effect of automatic release, while the object's destruction process is also delayed (Unified call release method).

    1. The Autorelease method does not alter the object's reference counter, but simply places the object in the auto-release pool;
    2. The auto-free pool is essentially the release method that calls the object when the auto-release pool is destroyed, and does not necessarily destroy the object (for example, if the reference counter of an object is >1, it cannot be destroyed at this time);
    3. Since the auto-free pool finally uniformly destroys objects, if an operation is more memory intensive (objects are more or objects occupy more resources), it is best not to put them into the auto-release pool or consider putting them into multiple auto-release pools;
    4. The static methods in the class library in OBJC generally do not need to be released manually, and the Autorelease method is called internally.

OBJECTIVE-C 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.