Summary of features in. NET 4.0 (iii): garbage collection

Source: Internet
Author: User

1. Memory Basics

    • Each process has its own separate virtual address space. All processes on the same computer share the same physical memory, and if there is a page file, the page file is also shared.
    • By default, each process on a 32-bit computer has a 2 GB user-mode virtual address space.
    • As an application developer, you can only use virtual address space and do not manipulate physical memory directly. The garbage collector allocates and frees virtual memory on the managed heap for you.
    • If you are writing native code, use the WIN32 function to handle the virtual address space. These functions assign and release virtual memory on the native heap for you.
    • There are three states of virtual memory:

Available. The memory block does not have a reference relationship and can be used for allocation.

Keep. Memory blocks are available to you and cannot be used for any other allocation requests. However, you cannot store the data in the memory block until it is committed.

Submit. The memory block has been assigned to the physical storage.

    • There may be virtual address space fragmentation. This means that there are some available blocks in the address space called holes. When a virtual memory allocation is requested, the virtual Memory manager must find a single free block that is large enough to satisfy the allocation request. Even if you have 2 GB of free space, 2 GB of allocation requests may not succeed unless all of these spaces must be in a single address block.
    • If you run out of reserved virtual address space or committed physical space, you may run out of memory.

2. Conditions for garbage collection

A garbage collection occurs when one of the following conditions is true:

    • The system has low physical memory.
    • The memory used by an allocated object on the managed heap exceeds an acceptable threshold. This means that the acceptable memory usage threshold has exceeded the managed heap. As the process runs, this threshold is continuously adjusted.
    • Call the GC. Collect method. In almost all cases, you do not have to call this method because the garbage collector continues to run. This method is mainly used for special cases and testing

3. Managed heap

After the garbage collector is initialized by the CLR, it allocates a piece of memory for storing and managing objects. This memory is called the managed heap (relative to the native heap in the operating system).

Each managed process has a managed heap. All threads in the process are allocated objects on the same heap.

When a garbage collection is triggered, the garbage collector reclaims the memory occupied by the dead object. The recycle process compresses the active objects so that they are moved together and the dead space is removed, making the heap smaller. This ensures that the objects that are allocated together are all on the managed heap, preserving their locality.

The intrusion (frequency and duration) of garbage collection is determined by the number of allocations and the amount of memory reserved on the managed heap.

This heap is visible as a cumulative of two heaps: the large object heap and the small object heap.

The large Object heap contains objects with a size of 85,000 bytes and more bytes. oversized objects on the large object heap are usually arrays . very large instance objects are rare.

4. Algebra

There are three generations of objects on the heap:

    • Section 0 generation . This is the youngest generation, which contains short lifetime objects. An example of a short-lived object is a temporary variable. Garbage collection occurs most often in this generation.
    • The newly allocated object forms the next generation of objects and is an implicit No. 0-generation collection, unless they are large objects, in which case they will go into the large object heap in the 2nd generation of collections.
    • Most objects are recycled through garbage collection in generation No. 0 and are not retained to the next generation.
    • Section 1 generation . This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.
    • Section 2 generation . This generation contains long-lived objects. An example of a long-lived object is an object in a server application that contains static data that is active during the process.

When conditions are met, garbage collection occurs on a specific generation. Reclaiming a generation means reclaiming the objects in this generation and all of their younger generations. The 2nd generation garbage collection is also known as full garbage collection because it reclaims all objects on all generations (that is, all objects in the managed heap).

Survival and ascension: objects that are not recycled in garbage collection are also known as survivors and are promoted to the next generation. The objects surviving in the No. 0 generation garbage collection will be promoted to the 1th generation, and the objects surviving in the 1th generation of garbage collection will be promoted to the 2nd generation, while the objects surviving in the 2nd garbage collection will remain the 2nd generation.

When the garbage collector detects a high rate of survival in a generation, it increases the allocation threshold for that generation, so the next collection will fetch a very large amount of reclaimed memory. The CLR balances before the following two priority levels: not allowing the application's working set to get too much memory and not allowing garbage collection to take too much time.

5. What happens in the garbage collection process

Garbage collection is divided into the following stages:

    • Tag stage, locate and create a list of all active objects.
    • The relocation phase, which updates a reference to the object that will be compacted.
    • The compression phase is used to reclaim space occupied by dead objects and to compress the surviving objects. The compression phase moves the objects that survived the garbage collection to the earlier end of the segment.

Because a 2nd-generation collection can occupy more than one segment, you can move objects that have been promoted to the 2nd generation to the earlier segment. The 1th and 2nd generation survivors can be moved to different segments because they have been promoted to the 2nd generation.

The large object heap will not be compacted because it will increase memory usage over an unacceptable length of time.

The garbage collector uses the following information to determine whether an object is an active object:

    • The stack root . Stack variables provided by the just-in-time (JIT) compiler and the stack viewer.
    • garbage collection handle. A handle that points to a managed object that can be assigned by user code or the common language runtime.
    • static Data . Static objects in the application domain that may reference other objects. Each application domain will track its static objects.

Until garbage collection starts, all managed threads except the thread that triggered the garbage collection are suspended.

Demonstrates a thread that triggers a garbage collection and causes other threads to hang.

6. Background Garbage collection

In the background garbage collection, during the 2nd generation of recycling, the temporary era (generation NO. 0 and 1th Generation) will be collected as needed. Background Garbage collection cannot be set; it automatically runs and enables concurrent garbage collection. Background Garbage collection is an alternative to concurrent garbage collection. As with concurrent garbage collection, background garbage collection is performed on a dedicated thread and only applies to 2nd generation recycling.

Garbage collection during background garbage collection is called foreground garbage collection. When a foreground garbage collection occurs, all managed threads are suspended.

When background garbage collection is in progress and you have allocated enough objects in generation No. 0, the CLR performs a No. 0-generation or 1th-generation foreground garbage collection. A dedicated background garbage collection thread checks on common security points to determine whether there is a request for foreground garbage collection. If present, background recycling suspends itself so that foreground garbage collection can occur. After the foreground garbage collection is complete, the dedicated background garbage collection thread and the user thread continue.

Background Garbage collection eliminates the allocation limit imposed by concurrent garbage collection, because a temporary garbage collection can occur during a background garbage collection. This means that background garbage collection can remove dead objects from a transient generation, and can also expand the heap as needed during generation 1th garbage collection.

Background Garbage collection is not currently available for server garbage collection

Summary of features in. NET 4.0 (iii): garbage collection

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.