IOS Memory Management

Source: Internet
Author: User

While the software is running, it allocates and uses the memory resources of the device, so in the process of software development, memory management is required to ensure efficient and fast allocation of memory and to release and reclaim memory resources when appropriate.

Objective-c Objects for memory management

In iOS development, there are two main kinds of objects in memory, one is value type, such as int, float, struct and other basic data types, and the other is reference type, that is, all OC objects inherit from NSObject class. The former value type does not require us to manage, and the latter is a reference type that requires us to manage memory and, once poorly managed, can have very bad consequences.

  Why doesn't a value type need to be managed, whereas a reference type needs to be managed? That's because they're not allocating memory the same way.

Value types are placed on the stack, which in turn is tightly arranged, occupying a contiguous memory space in memory, followed by the advanced post-out principle. The reference type is placed in the heap, and when the object is allocated memory space, it will randomly open up space from memory, the object and the object may leave an indeterminate size of white space, so it will produce a lot of memory fragmentation, we need to manage.

Stack memory compared to heap memory performance, stack memory is better than heap memory, because the stack follows the principle of advanced, so when the data volume is too large, the stack will significantly reduce performance. Therefore, we will put a lot of data into the heap, and then the stack of the address of the heap, when the need to call the data, we can quickly through the address in the stack to find the data in the heap.

Value types and reference types can be converted to each other, and the process of converting value types to reference types is called boxing, such as wrapping int as nsnumber, which increases the program's run time and degrades performance. The process of converting a reference type to a value type is called unpacking, such as converting Nsnumer to float, in the process of unpacking, we must pay attention to the original type of the data, if the type is wrong, may cause the unboxing to fail, so there is a security issue. Manual unpacking and boxing will increase the running time of the program, reduce the readability of the code, and affect performance.

In the iOS development process, the value type system in the stack memory is automatically managed, and the reference types in the heap memory need to be managed. Each OC object is specifically four bytes to store the reference counter, which is an integer that represents the number of times the object is referenced, through which it can be judged whether the 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, or retain, when the reference count is added to the 1,release.

Second, objective-c Management of Memory way

The OBJECTIVE-C provides two memory management mechanisms, MRC (Mannul Reference counting) and arc (Automatic Reference counting), which provide manual and automated management of memory to meet different requirements. The MRC differs from ARC as shown.

1.MRC (Manual reference count), manually manage memory.

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. In order to avoid the presence of wild pointers, we will let the pointer =nil when we release it.

2.ARC (auto reference count) to automatically manage memory.

ARC is the new feature introduced by IOS5, which allows automatic management of memory through arc. In Arc mode, the object is freed as long as no strong pointer (strong reference) is directed to the object. In Arc mode, retain, release, Retaincount, etc. are not allowed. Also, calling the [Super Dealloc] method is not allowed if the Dealloc method is used.

The property variable modifier in arc mode is strong, weak, which is equivalent to retain, assign in MRC mode. Strong: Instead of retain, the default keyword, which represents a strong reference. Weak: Instead of assign, declares a weak reference that can automatically set nil, but a function more than assign, the pointer to the address is freed, the cursor itself is automatically released.

Third, 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 object is released, it points to nil, can effectively avoid the wild pointer, its reference count is 1.

ReadWrite: Readable writable feature that needs to be used when generating getter methods and setter methods.

ReadOnly: A read-only attribute that generates only getter methods does not generate setter methods and does not want properties to change outside of the class.

Assign: Assignment attribute, not involving reference count, weak reference, setter method assigns the passed parameter to the instance variable, and only when the variable is set.

Retain: Represents the owning attribute, the setter method retains the passed in parameter first, then assigns the value, the retaincount of the passed parameter will be +1.

Copy: Represents the copy attribute, and the setter method copies the incoming object to a copy, requiring a completely new variable.

Nonatomic: Non-atomic operation, no synchronization, multi-threaded access can improve performance, but threads are unsafe. Determines whether the setter getter generated by the compiler is an atomic operation.

Atomic: Atomic operation, synchronous, means multithreading security, contrary to nonatomic.

Iv. MRC vs. arc

MRC and ARC are theoretically incompatible, i.e. if you create a project that is in ARC mode, you cannot use release in your code, otherwise there will be a memory problem. Most programs now choose the ARC approach, but many third-party frameworks are MRC, and if you want to add these third-party files to your project, you'll need to identify them, or you'll get an error when compiling.

In the ARC project, you can add the identity of the compilation option-fno-objc-arc to the MRC file, and in the MRC project, you can add the identity of the compile option-fobjc-arc to the arc file. The steps are as shown.

Converting the MRC file to arc actually removes the retain and release from the file, so it is done in the same way.

IOS Memory Management

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.