C # Memory Management and garbage collection

Source: Internet
Author: User

Garbage collection has to start with the root, just like giving birth to children.

Root: The root is a position that stores a pointer pointing to an object in the managed heap, or a null pointer that does not point to any object, that is, null. The root is in the thread stack or the hosting heap, and most of the traces are on the thread stack, because the Defined variables exist on the thread stack, and the type object pointers exist in the hosting heap, because to instantiate an object, two additional fields, "type Object Pointer" and "synchronous block Index", must be assigned ".

 

Function of Type object pointer. The instantiation of an object does not allocate memory for its method. The static fields of the type are allocated memory. To call something of the type, the instance must pass the type object pointer. For example, if the instance of an object is a method of the common type, the instance only needs to call the method of the type through the type object pointer. For more information about the method call, see myThis blog.

 

Function of synchronizing block Indexes. 1: used for lock, so that the object can only be accessed by one thread at a time; 2: used to obtain the hashcode of the object; 3: Indicates whether an object is junk during garbage collection. AboutLockThe most typical example is Singleton. All implementations are to instantiate an object, lock it, and determine whether an instance is required to implement the singleton object. Why do we need to instantiate an object instead of directly locking (typeof (object)? This is because this will lock the object type. During the lock, any thread that uses lock (typeof (object) must wait, and the object can still be used normally. The reason why lock can be used for single-thread access is that it has an empty for endless loop, which keeps reading a single digit in the index of the synchronization block. If this bit is not marked to jump out of the loop, if it is marked, it will keep executing the loop until the method execution is complete, and other threads will keep waiting. Now you know that lock can make yourProgramYou can only ask questions in a single thread and know that the lock efficiency is low.

 

Nextobjptr is the best BPointer. All resources in the CLR are allocated from the managed heap. The managed heap is a continuous memory space and maintains a pointer nextobjptr. It points to the start position of the next object after the previous object address, if no object exists in the managed heap, it points to the starting position of the managed heap. Every time an object is allocated, nextobjptr points to the end of the object to prepare to allocate the next object. The position where the nextobjptr pointer moves is actually the length of the space where the previous object is located. Is it changed from the starting position pointing to the object to the end of the object. From where the object is allocated, it depends on nextobjptr.

 

How much space does it take to instantiate an object?Memory + type object pointer + synchronized block index required for all fields of the object. The role of type object pointers and synchronized block Indexes has been mentioned earlier. Some fields are not clearly defined, but they do exist. Each object except the object has a base field. Through this field, you can call the instance fields and methods of the parent class, you can access the fields and methods defined by your grandfather. The CLR uses recursion to call the parent class method. Of course, it depends on whether your grandfather is willing to let you call the method. You know the reason.

 

Faster than C before garbage collection startsFast. Objects are happily allocated in the managed heap. The capacity of the managed heap is limited. One day, The 0th generation will be full, so there will be no more sand. Garbage collection is now available. Before garbage collection, you are very happy to use the memory. Of course, the speed is very fast and faster than that of the C language, because the memory of C is randomly allocated, as long as you find a suitable area, allocate memory there, which will cause memory fragmentation. Sometimes a large memory is required and multiple traversal is required. The garbage collection time is not that good. The speed must be slower than that of C. Let's see why the program speed is slow when garbage collection is made.

 

Garbage collection is divided into two steps: 1: marking; 2: Compression

1: Mark. At the beginning of garbage collection, the garbage collector regards all objects in the managed heap as garbage, that is, no pointer is directed to the managed heap on the thread stack. It is estimated that an object is regarded as spam, that is, it is not referenced. When garbage collection starts, the garbage collector will linearly scan along the thread stack, when a variable on the thread stack references an object in the managed stack, the garbage collector will mark the object, that is, modify a specific bit in the object synchronization block index, A Synchronized block index is a bit array. Each element has a specific role. The three functions I know are listed above. The marked object may also reference other objects, and the referenced objects will also be marked. The Garbage Collector uses recursion to mark these objects one by one, an object may be referenced by multiple objects. When the garbage collector finds that an object is marked, it will exit recursion because recursion is unnecessary and an endless loop may occur.

In this way, the garbage collector linearly scans the thread stack, recursively scans the managed heap, and finally marks all referenced objects in the managed heap, but the objects not marked are junk, waiting for recycling.

 

2: Compression. After the garbage collection, the disk fragments will appear, and the managed heap should be organized, that is, compressed. Put the unrecycled objects together, close to the starting position of the managed heap, and free up the remaining memory to store new objects. Since many objects are compressed and the pointer that references them becomes invalid, the managed heap needs to modify all pointer pointers, to ensure that the object will not become inaccessible because of garbage collection, and the pointer will become invalid.

After compression, space is made up and new objects can be allocated. When the 0th generation is full, garbage collection is performed again, and garbage collection continues, after three generations are recycled, there is still no memory to allocate. That is, it is time to get rid of all the resources. CLR will tell you outofmemoryexception. The CLR memory is swallowed up by programs. For more information about generation, see myThis blog. Garbage collection will be performed when the 0th generation is full. After the 0th generation is fully recycled, there is still not enough memory to store the current object, and the 1st generation will be reclaimed if it is not enough, if it is enough, the next generation will not be recycled, and garbage collection can be used.CodeControl GC. Collect ().

For more information about memory management and garbage collection, please wait for my next blog.

 

Author: Chen taihan

Blog: http://www.cnblogs.com/hlxs/

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.