iOS memory management

Source: Internet
Author: User

Before iOS5, there is no arc, so the memory on its own manual recycling or use of autorelease. The non-ARC scenario is described below, assuming there are two pointers A and b.

1. Keyword asign. The case is for direct assignment, only for basic types, such as int float double short bool char long, and so on. It is only assigned directly and does not have a reference count, so it does not have to manage memory.

2. Keyword retain. Assuming pointer a points to a chunk of memory, and we point B to the memory, we assume that pointer A is no longer in use, can we release that memory? The answer is obvious. No! Because B is still using this memory, if released, it will be crash off. So the reference counting function is introduced. Retain is a count of +1, and when we set the Retain keyword on the property, Alloc Init will count the memory +1

3. Keyword Release.release and the second is just the opposite, the reference technology-1 operation. When the reference technique is 0 o'clock, the dealloc is called and the memory is recycled. If pointer a points to two memory, you should call release two times.

4. Keyword copy. Copy means copying two blocks of memory.

5. Keyword Atomic. This property only supports synchronous operations, which is thread-safe.

6. Keyword nonatomic. Supports asynchronous operations, non-thread safe.

7. Keyword autorelease. If the object a points to is autorelease, then we will not need to release the A. When a is no longer used, a will enter NSAutoreleasePool. A's life cycle will be extended, and a will be destroyed when the pool drain. An nsautoreleasepool is initialized when the app starts. In the main function.

8.NSAutoReleasePool Introduction.

The method of initialization is:nsautoreleasepool *subpool = [[NSAutoreleasePool alloc] init];

The method of release is not release but rather [subpool drain];

The following English is excerpted from Apple's official documentation.

In a garbage collected environment, release was a no-op. NSAutoreleasePool therefore provides a drain method that in a Refe rence-counted environment behaves the same as calling release, but which in a garbage collected environment triggers Garba GE Collection (if the memory allocated since the last collection was greater than the current threshold). Typically, therefore, you should use drain rather than release to dispose of an autorelease pool.

Usage scenarios: 1. When we use autorelease to manage too much memory, we should initialize a pool and perform the reclamation, because the pool manages the time of memory reclamation is indeterminate. 2. When we start a new thread, if we use autorelease, we should initialize a pool ourselves, because the main thread is not managing the autorelease of the new threads.

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.