. Net Technical FAQ (5)-garbage collection

Source: Internet
Author: User

5. Garbage Collection
5.1 What is garbage collection?
Garbage collection is a system in which the runtime component manages object lifecycles and heap memory they occupy. For. net, it is not a new concept-Java and many other languages/runtime libraries have been using garbage collection for some time.
 
5.2 after the last reference of the object is revoked, it may not be destroyed immediately, right?
Yes. The garbage collector does not guarantee the time when the object is destroyed and its memory is stored.
 
5.3. Net why does not it provide a deterministic structure?
Because of the garbage collection algorithm .. Net spam collector periodically scans the list of all objects being used by the application. All undiscovered objects during the scan can be destroyed and the memory is released. After the last reference of the object is revoked, this implementation of the algorithm prevents the Runtime Library from being notified immediately-it can only be found at the next time the heap is cleared.
In addition, this algorithm collects garbage as little as possible to achieve the most efficient work. In general, the consumption of heap capacity triggers the collection process.
 
5.4 is there a problem with the lack of deterministic destructor in. Net?
This does affect the component design. If your object requires expensive or scarce resources (such as locking the database), you need to provide a way for the client to notify the object after the work is complete to release the resource. Microsoft recommends that you provide a method called dispose () for this purpose. However, this will cause problems in distributed objects-who calls the dispose () method in a distributed system? Some kind of reference-counting mechanism or owner management mechanism is required to handle distributed objects-unfortunately, the Runtime Library cannot help this.
 
5.5 does the deterministic structure affect the use of COM objects in the managed code?
Yes. When using COM objects in managed code, you actually rely on the garbage collector to finally release your objects. If your COM Object occupies expensive resources and can only be released after the object is finally released, you may need to provide a new interface on your object to support explicit dispose () method.
 
5.6 I heard that I should avoid using the Finalize method. Should I implement finalize in my class?
For the Garbage Collector, objects with the Finalize method need to do more work than objects without this method. At the same time, the finalized order of objects is not guaranteed, so there are different views on accessing other objects from the finalized method. Finally, the finalized method cannot be called. Therefore, you should never rely on it to clean up object Resources.
Microsoft recommends the following methods:
Public class ctest
{
Public override void dispose ()
{
... // Cleanup activities
GC. suppressfinalize (this );
}
 
Protected override void finalize ()
{
Dispose ();
}
}
Generally, when the client calls dispose (), the object's resources are released, and suppressfinalize () is called, the garbage collector is exempted from its obligation to finalize it. In the worst case, when the client forgets to call dispose (), there is a great opportunity to call finalize () through the garbage collector to finally release the object's resources. Due to the defects of the garbage collection algorithm, this seems like a reasonable solution.
 
5.7 do I have any means to control the garbage collection algorithm?
One thing. The system. GC class provides an interesting method. The first is the collect method-which forces the Garbage Collector to immediately collect all unreferenced objects. The other is requestfinalizeonshutdown (), which tells the Garbage Collector to run the finalize () method on each object when the application is closed. When an application is closed, the garbage collector generally chooses a quick exit method instead of calling finzlize (). Therefore, this method can manually force the running of the database to take more responsibility.
If you want to verify that this is not just a theoretical statement, try the following test procedure:
Using system;

Class ctest
{
Protected override void finalize ()
{
Console. writeline ("this is the finalizer .");
}
}

Class capplication
{
Public static void main ()
{
Console. writeline ("this is main .");
Ctest test = new ctest ();

// GC. requestfinalizeonshutdown ();
}
}
Run this program, and then remove the annotation mark before GC. requestfinalizeonshutdown () and run it again ......
 
5.8 how do I know what the garbage collector is doing?
Many interesting statistics in the. NET Runtime Library are output through the 'com + Memory 'performance object. Use Performance Monitor to view them.

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.