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

Source: Internet
Author: User

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

Aunt Objective-C:The operating system makes money by selling memory space. Can the price be lowered? 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, what do you say is that memory space is sold out?

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


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


Aunt Objective-C:
Nonsense. All the memory is 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 ?.


Aunt Objective-C:
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 unavailable 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.


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


C # sister:
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?


Aunt Objective-C:
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:
Starting from the Mac version, Mac OS X versions are all cat cats. 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:
Okay, you're not going to approve of the job. Next, iPhone and iPad development does not support garbage collection.


C # sister:
Does memory need to be manually released by programmers?


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

First, create an object to be tested and destroyed.

# Import "House. h "// first create an object House class to be deleted @ implementation House-(void) dealloc // Objective-C will automatically call this method when destroying the object {NSLog (@ "the house has been 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:Good cup house


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

House * h1 = [House new]; NSLog (@ ". 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.


In the above example, to demonstrate retain, release, and retainCount, the programmer enforces
Retain and release change the reference quantity statistical value (the reference quantity has not changed, of course). The actual operation will certainly not.

Let's take a look at the following example.

House * h1 = [House new]; // a reference after 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 also runs to join in the fun, at this time, three objects reference [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 the object [h3 release]; // The counter minus 1 NSLog (@ "% lu, % lu, % lu", [h1 retainCount], [h2 retainCount], [h3 retainCount]); // 0, 2, 2 h2 = nil; // h2 does not reference the object [h3 release] anymore; // The counter minus 1 NSLog (@ "% lu, % lu, % lu ", [h1 retainCount], [h2 retainCount], [h3 retainCount]); //, 1 [h3 release]; // The count is reduced to 0, this indicates 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:Basically, the three variables h1, h2, and h3 have been tossing around 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.


Aunt Objective-C:
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 ..

--

Dear colleagues, it takes a short time to learn Objective-C. learning Objective-C is not for Mac or iPhone development, but not practical,
Actually, it is a C # Learning note for the user to learn Objective-C. The exact purpose of learning is to help me understand C #After all, it is impossible to know the characteristics of C # without comparison.
Please take a critical look at this.If it is found that there are conflicts with other articles, books, comments, and materials, please refer to other articles as far as possible. And leave me a message
I also invite all experts to make bricks actively. I just used it to build a house ~~~
C # dialogue between sister and Aunt Objective-C

(01) meet Objective-C: greetings from the first meeting
(02) This is an aunt's dog.
(03) NSString -- meet another dog
(04) Basis of garbage collection-about the demolition team
(05) automatic release pool-foreign aid from the demolition team

Pending renewal

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.