For memory, retain, copy, and assign

Source: Internet
Author: User

 

Memory Management Mechanism

Reference count: because there is no garbage collection mechanism in OC, it is based on the reference count collection mechanism. When the reference count is 0, the space is destroyed. That is to say, determine whether an object is released based on the reference count.

Operation that can change the reference count: allco retain release copy dealloc

Constructor

Auto Release pool:

Each time an object calls the autorelease method (the official statement in obj-C should be: Send an autorelease message to the object), the reference count of the object does not actually change, but adds a record to the pool, write down this requirement of the object. Finally, when the pool sends a drain or release message, the requirements for all objects in the pool are executed one by one (that is, all objects in the pool are notified before the pool is destroyed, when you send all the release messages, the reference count is actually reduced. If the object has not sent the autorelease message before)

Retain: reference count + 1 operation to obtain the ownership of the object, which corresponds to release.

Thread: When the view exits, you need to close the thread that you open.


Alloc: Open up space, the reference count is 0 to 1, dealloc: destroy space When manually calling dealloc, the reference count is forcibly set to 0;

Copy: copy the source data again, the reference count of the original data remains unchanged, and the reference count of the copy is + 1. Copy an object and store it in different memory areas, the reference count of the original object remains unchanged. The nscoping protocol can be used for deep copy.

Assign: The reference count of integer or other constants. No change

In Arc mode, strong is similar to retain. Strong is used in Arc mode to indicate that the object belongs to a strong reference, similar to retain, but more secure than retain, as long as the object has an owner, it can be referenced without being released. As long as the object loses its owner, it is immediately discarded and no longer has to point the pointer to the released object to null. Avoid wild pointers.

Weak is similar to assign and has weak references. The reference count does not increase. Release pointer automatically set to nil

In Arc mode, weak is similar to assign,

Readwrite: read/write, including setter and getter

Readonly: only read operations, that is, only Getter

Nonatomic: Non-atomic features that do not guarantee security under multiple threads

Atomic: The Atomic feature ensures the security of multiple threads. One attribute needs to access a variable for a thread at the same time and can be automatically unlocked using atomic.

This article from "a long time back to one" blog, please be sure to keep this source http://lulun426.blog.51cto.com/9197884/1565371

For memory, retain, copy, and assign

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.