"C # sister and Objective-C aunt dialogue" (04) Basis of garbage collection-The Demolition Team

Source: Internet
Author: User

C # sister: the operating system controls the memory space price every day. Why is it so high? I purchased a huge amount of memory space, and later found that it was actually on the hard disk! Virtual Memory! TMD!

Objective-C Ayi: the operating system relies on selling memory space to make money. Do you think the price will fall? If you look at the psychology of those programmers, you will have a balance. Shanghai buys a house in Jiangsu, Beijing buys a house in Hebei, and goes to work across provinces. Every day, It Is Object-Oriented. In the end, no object can be found ..

C # sister: By the way, I still say "the same nightmare in the same world". What do you do if the memory space is sold out?

Aunt Objective-C: reclaim your memory and sell it to others.

C # sister: Ah ??!! Is the memory used by others also recycled?

Objective-C aunt: Nonsense. All the memory is from the operating system, just lend it to you. The operating system forces you to quit. You have a way to stop you and send a press release, just say "the dead are currently in stable mood ., Don't believe it. What is the validity period of your "memory space Certificate?

C # sister: 60 ms ?! Really TMD! Is the memory right law just introduced so domineering ?.

Objective-C Ayi: the operating system implements the "Interim Regulations on garbage collection" formulated in 1949. It has been in the interim for more than 60 years. Besides, maintaining stability is the core of all work! If you listen to the operating system, you can buy space for sale, rent for rent, and release space for rent. In particular, you can release unnecessary objects as soon as possible. The operating system is the most annoying. If it is light, it will be forcibly released. If it is heavy, it will delete the program ..

C # sister: Fortunately, our. NET Program invited a demolition team "Garbage Collector" to do this. If no useful objects are found, the system will return them to the operating system. The memory will be handed over to the operating system early. I am glad to make some compensation for the memory location. Don't arrange any virtual memory for me, in addition, the forced demolition team requested by the operating system is brutal, so they should not be bothered as much as possible.

Aunt Objective-C: Well, this is a good idea, but how does the. NET Garbage Collector know that you cannot use these objects? Will it also remove the active objects?

C # sister: the so-called useless means that these objects are no longer referenced in the code ,. all objects in. NET are directly or indirectly referenced by local variables, global variables, and static variables pointing to the CPU registers of the managed heap ", the garbage collector only needs to start from the root, scan these roots to reference those objects, and record them all. The remaining objects that are not recorded can be recycled. The garbage collector automatically performs the removal. Programmers generally do not need to intervene. complex object programmers need to leave the removal method in the Finalize () method. The Garbage Collector is not very good at removing unmanaged resources, the programmer should give some advice. This process is very secure and the objects in use will not be recycled.

Objective-C aunt: How does the Garbage Collector know the reference relationship between objects?

C # sister: through metadata, metadata describes the reference relationships between objects, and. NET is type-safe. object pointers can only point to corresponding objects.

C # sister: Aunt Objective-C, how is your memory managed?

Objective-C Ayi: Objective-C 2.0 also has a garbage collection mechanism, but it can only be used in Mac OS X Leopard or later versions.

C # sister: Leopard? Leopard? What is it?

Aunt Objective-C: From the Mac version, Mac OS X is a cat. The latest version is Snow Leopard, which may be released this summer.

The figure below roughly describes the history of Mac OS X.

 

Which of the following is not creative like MS in your hometown? One window this year will be made next year, and one window will be made every day, this is not because the MS Asia Pacific Research Center Building fell down by the end of March.

C # sister: continue to talk about garbage collection...

Aunt Objective-C: Well, I will not approve of your hometown. Next, iPhone and iPad development does not support garbage collection.

C # sister: Do programmers need to release the memory?

Aunt Objective-C: Yes, but Cocoa has been simplified. Let's take a look at the example below.

First, create an object to be tested and destroyed.

# Import "House. h"
// Create an object House class to be deleted first
@ Implementation House
-(Void) dealloc // Objective-C will automatically call this method when destroying the object
{
NSLog (@ "house demolished ");
[Super dealloc];
}
@ End
This House class is empty, and there is only one dealloc method in it. A message "the House has been demolished" can be displayed during demolition ".

C # sister: house with good cups

Objective-C aunt: The following program calls this House object

House * h1 = [House new];
NSLog (@ "A. Object reference quantity: % lu", [h1 retainCount]);
[H1 retain];
NSLog (@ "B. Object reference quantity: % lu", [h1 retainCount]);
[H1 release];
NSLog (@ "C. Object reference quantity: % lu", [h1 retainCount]);
[H1 release];


[H1 retainCount] indicates the number of places where the h1 instance (House instance) is referenced. However, whether this statistic is accurate is not closely related to the system depends on whether the programmer's statistics are accurate.

Objective-C uses the reference counting method to check whether objects need to be recycled. The counters for objects created through new and alloc are all 1. When location A object is just added to the new instance, the reference quantity is 1;

If this object is referenced by another object once, the number of references increases by 1. Location B has just called retain, so the number of references is increased by 1, SO 2 is displayed.

If a referenced object releases the object, call the release once and the number of referenced objects is reduced by 1. Location C has just called release, so the number of references is reduced by 1, so 1 is displayed.

Finally, a release is called. At this time, the number of instances referenced by this instance is 0, indicating that this instance is no longer in use. Objective-C will automatically call the dealloc method of this object to recycle resources. So it shows that the House has been demolished "~

The difference with. NET is that. NET is the garbage collector that automatically counts objects that have not been referenced, but this job is on Objective-C and must be done by programmers.


The above example is purely intended to show retain, release, and retainCount. The programmer forces retain and release to change the reference quantity statistical value (the reference quantity has not changed, of course ), the actual operation will certainly not do this.

Let's take a look at the following example.

House * h1 = [House new]; // a reference after new
House * h2;
House * h3;
NSLog (@ "% lu", [h1 retainCount]); // return 1

H2 = h1; // h2 is also referenced to this object.
[H1 retain]; // Therefore, manually update the reference counter to 2
NSLog (@ "% lu, % lu", [h1 retainCount], [h2 retainCount]); // returns 2

H3 = h2; // h3 is also running, so there are three referenced objects.
[H1 retain]; // Therefore, manually update the reference counter to 3.
NSLog (@ "% lu, % lu, % lu", [h1 retainCount], [h2 retainCount], [h3 retainCount]); // returns 3, 3

H1 = nil; // h1 no longer references objects
[H3 release]; // The counter minus 1
NSLog (@ "% lu, % lu, % lu", [h1 retainCount], [h2 retainCount], [h3 retainCount]); // return 0, 2

H2 = nil; // h2 does not reference objects anymore.
[H3 release]; // The counter minus 1
NSLog (@ "% lu, % lu, % lu", [h1 retainCount], [h2 retainCount], [h3 retainCount]); // 0, 0, 1

[H3 release]; // The count is reduced to 0, indicating that the object has not been referenced. When the demolition team rises, the object's memory is reclaimed and "the house has been demolished" is returned"
C # sister: I have basically understood that the three variables h1h2h3 have been tossing for a long time. In fact, they all point to an instance and constantly synchronize the reference quantity of objects through retain and release, once the number of references is 0 (not the actual number of references, it is the number of reference calculated by the programmer is 0), the demolition team will go up to demolish the house, right? Therefore, it is very important for programmers to reference statistical objects. In. NET, it is done by the garbage collector to judge that the reference is 0.

Objective-C aunt: Yes, Objective-C manages the memory in this way, but this is just the beginning. Next time there will be more complex examples and more concise methods.


Xiao Mo's children's shoes

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.