Memory Management and Memory Management Software

Source: Internet
Author: User

Memory Management and Memory Management Software

IOS memory is managed by referencing the counting mechanism. It can be divided into MRC (manual reference count), ARC (automatic reference count ),

Why do we need to learn about memory management?

The memory management mode is the reference counting mechanism, which controls the reference counting of objects to implement the object operation function. An object can be generated, held, released, or destroyed in its lifecycle. The reference count is divided into ARC and MRC. In MRC, the methods for adding the reference count are retain, alloc, new, and copy. The method to reduce the reference count is release and autorelease. When the reference count is balanced, the system will automatically execute the dealloc method of the object to destroy the object and complete the memory management of the object. If a file is created, it must be released.

1. Develop good memory management habits and avoid memory problems from the source.

2. If memory problems occur, you can find and solve them. (Debugging bugs)

The concept of reference count management memory is to generate, hold, release, and destroy objects by controlling the memory or reference count of objects.

1. Generate. The reference count of the object ranges from 0 to 1-> for example, alloc

2. Hold and add a reference to add 1-> retain

3. release, reduce a reference, and reduce the reference count of the object by 1-> for example, release autorelease

4. Destroy. When the reference count of an object is 0, the system will reclaim the memory space. -> For example, dealloc

When this space is recycled by the system, it cannot be accessed through pointers, which may easily lead to a wild pointer.

Remember !! The concept of reference count exists only in the heap area, targeting the objects in the heap area.

The corresponding method is + (instancetype) alloc; a memory space is opened in the heap area to store objects, and the memory is cleared. At the same time, the reference count of this object is changed to 1, is the process from 0 to 1.

// 0-> 1

Person * person = [[Person alloc] init];

// RetainCount

NSLog (@ "person is % p, person's retainCount is % lu", person, [person retainCount]);

// 1

Person * p1 = person;

NSLog (@ "p1 is % p, p1's retainCount is % lu", p1, [p1 retainCount]);

// Hold, the method is retain, and Add 1 to the object's Method

// 1-> 2

[Person retain];

NSLog (@ "person is % p, person's retainCount is % lu", person, [person retainCount]);

// 2-> 3

[P1 retain];

NSLog (@ "p1 is % p, p1's retainCount is % lu", p1, [p1 retainCount]);

// 3-> 4

Person * p2 = [p1 retain];

NSLog (@ "p2 is % p, p2's retainCount is % lu", p2, [p2 retainCount]);

// Release the release to reduce the reference count of the object by 1 and the value is immediately reduced by 1

[Person release];

NSLog (@ "% lu", [person retainCount]);

[Person release];

[Person release];

NSLog (@ "% lu", [person retainCount]);

[Person release]; // before executing this release message, the reference count of the object person is 1. after release, the reference count is 0. At this time, the system will automatically try out the dealloc method, releases the person object to manage the memory of the person object.

// The following line of code can print the retainCount, which is 1, because the value of the reference count inside the system is not 0, 0, which is just a number introduced for convenience, but in essence, this line of code may have caused a wild pointer, because the person object has been recycled by the system and then accessed [person retainCount.

// The Wild pointer crashes because the occupied memory space is occupied. If you try again, there will be a problem. If you don't crash, you just get lucky, the recycled memory space is not occupied yet.

Autorelease this method also reduces the reference count of an object by 1. However, unlike release, TA does not immediately subtract 1, but at a certain time point in the future, the time when the minus 1 operation is triggered is closely related to the automatic release pool.

The automatic release pool is a container used to record the autorelease message received by the sub-objects in the pool, which object receives the autorelease message, which one receives the message first, and then which one receives the message. When the sub-pool is released, the autorelease message actually minus 1.

-(Void) dealloc {

NSLog (@ "my days, % @ has been released", self );

[Super dealloc];

} If dealloc is rewritten, [super dealloc] will always be in the last row. Release the instance variables first, and then execute the dealloc method in the parent class to release the inherited instance variables. The order in which the dealloc method is released is the opposite of that in the initialization method.

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.