IOS memory management and ios Memory Management Mechanism

Source: Internet
Author: User
Tags float double

IOS memory management and ios Memory Management Mechanism

Before ios5, there was no ARC, so the memory was manually recycled or autorelisted. The following describes non-ARC conditions. Assume there are two pointers a and B.

1. Keyword asign. Direct assignment is used only for basic types, such as int float double short bool char long. It only performs direct value assignment without reference counting, so it does not need to manage the memory.

2. Keyword retain. Assume that pointer a points to a memory area and pointer B points to this memory area. In this case, we assume that pointer a is no longer in use. Can we release this memory? The answer is obvious. No! Because B is still using this memory, if it is released, it will crash. Therefore, the reference count function is introduced. Retain is the count + 1. When we set the retain keyword for the attribute, alloc init will count the memory + 1

3. the keyword release. release is the opposite of the second, and the reference technology-1 operation is performed. When the referenced technology is 0, dealloc will be called and the memory will be recycled. If pointer a points to two pieces of memory, call the release twice.

4. Keyword copy. Copy means that two pieces of memory are copied.

5. Keyword atomic. This attribute only supports synchronization operations, that is, thread security.

6. Keyword nonatomic. It supports asynchronous operations and is not thread-safe.

7. Keyword autorelease. If a points to the object autorelease, we do not need to perform the release operation on. When a is no longer used, a will enter the lifecycle of the NSAID pool. a will be extended. When pool drain is used, a will be destroyed. When the application starts, it will Initialize an NSAID utoreleasepool. In the main function.

8. Introduction to NSAID utoreleasepool.

The initialization method is: NSAID utoreleasepool * subPool = [[NSAID utoreleasepool alloc] init];

The release method is not release but [subPool drain];

The following is an English Excerpt from Apple's official documentation.

In a garbage collected environment, release is a no-op. NSAID utoreleasepool therefore provides a drain method that in a reference-counted environment behaves the same as calling release, but which in a garbage collected environment triggers garbage collection (if the memory allocated since the last collection is greater than the current threshold ). typically, therefore, you should use drain rather than release to dispose of an autorelease pool.

 

Use Cases: 1. When the memory managed by autorelease is too large, we should initialize a pool and recycle it, because the memory reclaim time managed by the pool is uncertain. 2. When we start a new thread, if we use autorelease, we should initialize a pool by ourselves, because the main thread does not manage the autorelease of the new thread.


Ios Memory Management Mechanism

When the background does not occupy cpu but memory usage, some programs will be automatically closed when the program is opened to free up storage.

For IOS memory management problems (I want to know how to clear background programs, class is one-click cleaning)

360 mobile guard can also provide many functions
 

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.