Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Garbage Colleciton

NET's garbage collector manages the allocation and release of memory for your application.
Each time you create a new object, the common language runtime allocates memory for the
object from the managed heap. As long as address space is available in the managed heap, the
runtime continues to allocate space for new objects. However, memory is not infinite.
Eventually the garbage collector must perform a collection in order to free some memory. The
garbage collector's optimizing engine determines the best time to perform a collection, based
upon the allocations being made. When the garbage collector performs a collection, it checks
for objects in the managed heap that are no longer being used by the application and performs
the necessary operations to reclaim their memory.
.NET 垃圾回收器为应用程序管理内存的分配和释放。每创建一个新对象,CLR 会从托
管堆 (Managed Heap) 上为该对象分配内存。只要托管堆上的内存(地址)空间够用 ,
CLR 会继续为新创建的对象分配内存。然而,内存空间是有限的,最终,垃圾回收器
必须执行回收操作以清理出一些内存空间。基于内存分配模式,垃圾回收器的优化引
擎会决定执行内存回收的最佳时间。当它执行内存回收时,它会检查那些分配在托管
堆上并且在应用程中不再被使用的对象,并执行回收操作,以收回这些对象所占用的
内存。

Conditions for a garbage collection


Garbage collection occurs when one of the following conditions is true:
1. The system has low physical memory.
2. The memory that is used by allocated objects on the managed heap surpasses an acceptable
threshold. This threshold is continuously adjusted as the process runs.
3. The System.GC.Collect method is called. In almost all cases, you do not have to call this
method, because the garbage collector runs continuously. This method is primarily used for
unique situations and testing.
当下列条件中任何一个符合时,将会执行垃圾回收:
1. 系统的物理内存低;
2. 分配在托管堆上的对象使用的内存超过了一个可接受的阈值,这个阈值会随着程序
的运行不断被调整;
3. 调用了 System.GC.Collect 方法;通常情况下,你不需要调用这个方法,因为垃圾回
收器会不断地运行,这个方法主要用于特殊的情况下以及测试时。

The managed heap


After the garbage collector is initialized by the CLR, it allocates a segment of memory to
store and manage objects. This memory is called the managed heap, as opposed to a native
heap in the operating system.
There is a managed heap for each managed process. All threads in the process allocate
memory for objects on the same heap.
To reserve memory, the garbage collector calls the Win32 VirtualAlloc function, and reserves
one segment of memory at a time for managed applications. The garbage collector also
reserves segments as needed, and releases segments back to the operating system (after
clearing them of any objects) by calling the Win32 VirtualFree function.
Important
The size of segments allocated by the garbage collector is implementation-specific and is
subject to change at any time, including in periodic updates. Your app should never make
assumptions about or depend on a particular segment size, nor should it attempt to configure
the amount of memory available for segment allocations.
The fewer objects allocated on the heap, the less work the garbage collector has to do. When
you allocate objects, do not use rounded-up values that exceed your needs, such as allocating
an array of 32 bytes when you need only 15 bytes.
When a garbage collection is triggered, the garbage collector reclaims the memory that is
occupied by dead objects. The reclaiming process compacts live objects so that they are
moved together, and the dead space is removed, thereby making the heap smaller. This
ensures that objects that are allocated together stay together on the managed heap, to preserve
their locality.
The intrusiveness (frequency and duration) of garbage collections is the result of the volume
of allocations and the amount of survived memory on the managed heap.
The heap can be considered as the accumulation of two heaps: the large object heap and the
small object heap.
The large object heap contains very large objects that are 85,000 bytes and larger. The objects
on the large object heap are usually arrays. It is rare for an instance object to be extremely
large.
当 CLR 完成初始化垃圾回收器后,垃圾回收器会分配出用于存储和管理对象的内存段。
这段内存被称为托管堆,与操作系统的原生堆相反。
每个托管进程均有一个托管堆,而一个进程中的所有线程会在相同的托管堆中为对象
分配内存。
为了保留出内存,垃圾回收器会调用 Win32 函数 VirtualAlloc,为当前托管程序保留
出一段内存区域。垃圾回收器也会在需要时,继续保留出的内存段,并通过调用
VirtualFree 函数来释放内存段,将内存归还给操作系统。
重要

分配在托管堆上的内存越少,垃圾回收所需要的做的工作就越少。当你为对象分配内
存时,不要使用超过你需要的值,比如当你仅需要 15 个字节时,却分配了一个 32 个
字节的数组。
当垃圾回收被触发时,垃圾回收器会回收那些生命周期已结束的对象所占用的内存空
间。回收过程中,垃圾回收器也会把继续存留的对象压到一起,并移出不再使用的空
间,因此,托管堆占用的空间就会变得比之前小一些。这确保分配的对象能够在托管
堆上连续在一起。
托管堆可以被认为是两个堆的总和:大对象堆与小对象堆。
大对象堆包括那些大于 85000 字节的对象,这些对象通常是数组,而实例对象少有非
常大的。

What happens during a garbage collection


A garbage collection has the following phases:
 A marking phase that finds and creates a list of all live objects.
 A relocating phase that updates the references to the objects that will be compacted.
 A compacting phase that reclaims the space occupied by the dead objects and
compacts the surviving objects. The compacting phase moves objects that have
survived a garbage collection toward the older end of the segment.
Because generation 2 collections can occupy multiple segments, objects that are promoted
into generation 2 can be moved into an older segment. Both generation 1 and generation 2
survivors can be moved to a different segment, because they are promoted to generation 2.
Ordinarily, the large object heap is not compacted, because copying large objects imposes a
performance penalty. However, starting with the .NET Framework 4.5.1, you can use the
System.Runtime.GCSettings.LargeObjectHeapCompactionMode property to compact the
large object heap on demand.

The garbage collector uses the following information to determine whether objects are live:
 Stack roots. Stack variables provided by the just-in-time (JIT) compiler and stack
walker.
 Garbage collection handles. Handles that point to managed objects and that can be
allocated by user code or by the common language runtime.
 Static data. Static objects in application domains that could be referencing other
objects. Each application domain keeps track of its static objects.

Before a garbage collection starts, all managed threads are suspended except for the thread
that triggered the garbage collection.
The following illustration shows a thread that triggers a garbage collection and causes the
other threads to be suspended.

Thread that triggers a garbage collection

You might also like