. Net garbage collection mechanism

Source: Internet
Author: User

Mark-compact tag Compression algorithm

Generation

Managed code/unmanaged code

Weak references (Weak References

A weak reference (weak reference) keeps a reference to the object while allowing the garbage collector to release the object at the garbage collection time it deems appropriate and reclaim memory. Suppose that there is a relatively inexpensive object creation, but it consumes a lot of memory, if you want to keep this object, the application needs to use it, but also want to be able to tell the garbage collector, when necessary to recover memory.

In general, the way we can improve GC performance is:

1. Clean up work. Don't let the resources open! Ensure that all open connections are closed as much as possible, and all unmanaged resources are cleared. When working with unmanaged objects, the initialization is as complete as possible, and the cleanup work should be as timely as possible.

2. Do not over-quote. Use reference objects when needed, and remember that if your object is active, all objects referenced by it are not garbage collected. When we want to clean up the things that some classes refer to, you can remove them by setting them to null. One way I like to do this is to point an unused reference to a lightweight nullobject to avoid the exception that produces a null reference. When GC is garbage collected, fewer references reduce the stress of mapping processing.

3. Use less finalizer. Finalizer is a very expensive resource when it comes to garbage collection, we should only use it if necessary. If we use IDisposable instead of finalizer it will be more efficient because our objects can be collected directly by GC instead of at the second recycle.

4. Try to keep objects and their sub-objects together. GC is very easy to replicate large chunks of memory data, and it is hard to replicate fragments in the heap, so when we declare an object that contains many other objects, we should try to keep them together when initializing.

5. Finally, it is possible to use static methods to keep objects lightweight.

Http://www.cnblogs.com/mirobright/archive/2009/11/21/1607773.html

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.