IOS memory management and ios Memory Management

Source: Internet
Author: User

IOS memory management and ios Memory Management

The memory resources of the device are allocated and used when the software is running. Therefore, memory management is required during software development to ensure efficient and fast memory allocation, release and recycle memory resources as appropriate.

1. Objective-C memory management objects

During IOS development, there are two main types of memory objects: value type, such as int, float, struct, and reference type, that is, all OC objects inherited from the NSObject class. The former value type does not need to be managed, and the latter type of reference needs to be managed by memory. Once the management is poor, it will have very bad consequences.

  Why does the value type not need to be managed, but the reference type need to be managed? That's because they have different memory allocation methods.

Value types will be placed in the stack, which are arranged in close order, occupying a contiguous memory space in the memory, following the principle of advanced and later. The reference type will be placed in the heap. When memory space is allocated to the object, it will randomly open up space from the memory. There may be blank space with uncertain size between the object and the object, therefore, a lot of memory fragments are generated and need to be managed.

Compared with the stack memory, the stack memory is superior to the stack memory because the stack follows the advanced post-release principle. Therefore, when the data volume is too large, the stack storage significantly reduces the performance. Therefore, we store a large amount of data into the heap and store the heap address in the stack. When we need to call the data, we can quickly find the data in the heap through the stack address.

The value type and the reference type can be converted to each other. The process of converting the value type to the reference type is called packing. For example, packaging int as NSNumber increases the running time of the program, reduces performance. The process of converting the reference type to the value type is called unpacking. For example, if NSNumer is converted to float, we must pay attention to the original data type during the unpacking process. If the type is incorrect, the Unpacking may fail, so there may be security issues. Manual unpacking and packing increase the running time of the program to reduce code readability and affect performance.

During IOS development, the Value Type System in the stack memory is automatically managed, and the reference type in the stack memory needs to be managed. Each OC object contains four bytes to store the reference counter. It is an integer that indicates the number of times the object is referenced. It can be used to determine whether the object is recycled, if the reference count is 0, the object is recycled. If the reference count is not 0, the object is not recycled. When an object executes alloc, new, or retain, the reference count is incremented by 1. When release is executed, the reference count is reduced by 1.

Ii. Objective-C Memory Management

Objective-c provides two memory management mechanisms: MRC (Mannul Reference Counting) and ARC (Automatic Reference Counting), which provide manual and Automatic management of memory, respectively, to meet different needs. Shows the differences between MRC and ARC.

 

1. MRC (manual reference count), Manual memory management.

In MRC mode, all objects need to manually add retain and release code to manage the memory. To use MRC, follow the principle of who creates and who recycles. That is, Who alloc, who release, who retain, who release.

When the reference count is 0, it must be recycled. If the reference count is not 0, it cannot be recycled. If the reference count is 0, but it is not recycled, memory leakage will occur. If the reference count is 0, releasing continues, resulting in a wild pointer. To avoid the occurrence of a wild pointer, we will first let the pointer = nil when releasing it.

2. ARC (automatic reference count), automatic memory management.

ARC is a new function launched by IOS5. It can automatically manage memory through ARC. In ARC mode, the object will be released as long as there is no strong pointer (strong reference) pointing to the object. In ARC mode, retain, release, retainCount, and other methods are not allowed. In addition, if the dealloc method is used, the [super dealloc] method cannot be called.

The modifier of the property variable in ARC mode is strong and weak, which is equivalent to retain and assign in MRC mode. Strong: replace retain. The default keyword indicates strong reference. Weak: In place of assign, it declares a weak reference that can automatically set nil. However, there is one more function than assign. After the address pointed to by the pointer is released, the pointer itself will be automatically released.

3. Memory-related Modifiers

Strong: strong reference, used in ARC, similar to retain in MRC. after use, counter + 1.

Weak: weak reference, used in ARC. If only the desired object is released, it points to nil, which can effectively avoid the wild pointer, and its reference count is 1.

Readwrite: this feature is readable and writable. It must be used to generate the getter method and setter method.

Readonly: Read-only feature. Only the getter method is generated and the setter method is not generated. You do not want the attribute to be changed outside the class.

Assign: The value assignment feature, which does not involve reference counting and weak references. The setter method assigns input parameters to instance variables and is only used when setting variables.

Retain: indicates that the property is held. The setter method will retain the input parameter first, and then assign a value. The retaincount of the input parameter will be + 1.

Copy: indicates the copy feature. The setter method copies the input object and requires a new variable.

Nonatomic: Non-atomic operations without synchronization. multi-threaded access improves performance, but threads are not secure. Determines whether the setter getter generated by the compiler is an atomic operation.

Atomic: atomic operation, synchronous, indicating multi-thread security, opposite to nonatomic.

Iv. MRC and ARC hybrid editing

MRC and ARC are theoretically incompatible, that is, if you create a project in the ARC mode, you cannot use release in your code; otherwise, memory problems may occur. Most programs now use the ARC method, but many third-party frameworks use the MRC mode. If you want to add these third-party files to your project, you need to identify them, otherwise, an error occurs during compilation.

In the ARC project, you can add the compilation option-fno-objc-arc to the MRC file. In the MRC project, you can add the fobjc-ARC identifier to the arc file. Steps are shown in.

 

Converting the MRC file to ARC is actually removing the retain and release in the file, so it is also completed in the medium way.

 

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.