|
| 1 | +--- |
| 2 | +title: Fundamentals of garbage collection |
| 3 | +description: Fundamentals of garbage collection |
| 4 | +keywords: .NET, .NET Core |
| 5 | +author: shoag |
| 6 | +manager: wpickett |
| 7 | +ms.date: 08/16/2016 |
| 8 | +ms.topic: article |
| 9 | +ms.prod: .net-core |
| 10 | +ms.technology: .net-core-technologies |
| 11 | +ms.devlang: dotnet |
| 12 | +ms.assetid: 9d5fce64-95a4-4609-8eee-b0ac70078cdb |
| 13 | +--- |
| 14 | + |
| 15 | +# Fundamentals of garbage collection |
| 16 | + |
| 17 | +In the Common Language Runtime (CLR), the garbage collector serves as an automatic memory manager. It provides the following benefits: |
| 18 | + |
| 19 | +* Enables you to develop your application without having to free memory. |
| 20 | + |
| 21 | +* Allocates objects on the managed heap efficiently. |
| 22 | + |
| 23 | +* Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations. Managed objects automatically get clean content to start with, so their constructors do not have to initialize every data field. |
| 24 | + |
| 25 | +* Provides memory safety by making sure that an object cannot use the content of another object. |
| 26 | + |
| 27 | + |
| 28 | +This topic describes the core concepts of garbage collection. It contains the following sections: |
| 29 | + |
| 30 | +* [Fundamentals of memory](#Fundamentals-of-memory) |
| 31 | + |
| 32 | +* [Conditions for a garbage collection](#Conditions-for-a-garbage-collection) |
| 33 | + |
| 34 | +* [The managed heap](#The-managed-heap) |
| 35 | + |
| 36 | +* [Generations](#Generations) |
| 37 | + |
| 38 | +* [What happens during a garbage collection](#What-happens-during-a-garbage-collection) |
| 39 | + |
| 40 | +* [Manipulating unmanaged resources](#Manipulating-unmanaged-resources) |
| 41 | + |
| 42 | +## Fundamentals of memory |
| 43 | + |
| 44 | +The following list summarizes important CLR memory concepts. |
| 45 | + |
| 46 | +* Each process has its own, separate virtual address space. All processes on the same computer share the same physical memory, and share the page file if there is one. |
| 47 | + |
| 48 | +* By default, on 32-bit computers, each process has a 2-GB user-mode virtual address space. |
| 49 | + |
| 50 | +* As an application developer, you work only with virtual address space and never manipulate physical memory directly. The garbage collector allocates and frees virtual memory for you on the managed heap. |
| 51 | + |
| 52 | +* Virtual memory can be in three states: |
| 53 | + |
| 54 | + * Free. The block of memory has no references to it and is available for allocation. |
| 55 | + |
| 56 | + * Reserved. The block of memory is available for your use and cannot be used for any other allocation request. However, you cannot store data to this memory block until it is committed. |
| 57 | + |
| 58 | + * Committed. The block of memory is assigned to physical storage. |
| 59 | + |
| 60 | +* Virtual address space can get fragmented. This means that there are free blocks, also known as holes, in the address space. When a virtual memory allocation is requested, the virtual memory manager has to find a single free block that is large enough to satisfy that allocation request. Even if you have 2 GB of free space, the allocation that requires 2 GB will be unsuccessful unless all of that space is in a single address block. |
| 61 | + |
| 62 | +* You can run out of memory if you run out of virtual address space to reserve or physical space to commit. |
| 63 | + |
| 64 | +Your page file is used even if physical memory pressure (that is, demand for physical memory) is low. The first time your physical memory pressure is high, the operating system must make room in physical memory to store data, and it backs up some of the data that is in physical memory to the page file. That data is not paged until it is needed, so it is possible to encounter paging in situations where the physical memory pressure is very low. |
| 65 | + |
| 66 | +## Conditions for a garbage collection |
| 67 | + |
| 68 | +Garbage collection occurs when one of the following conditions is true: |
| 69 | + |
| 70 | +* The system has low physical memory. |
| 71 | + |
| 72 | +* 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. |
| 73 | + |
| 74 | +* The [GC.Collect](xref:System.GC#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. |
| 75 | + |
| 76 | +## The managed heap |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | +There is a managed heap for each managed process. All threads in the process allocate memory for objects on the same heap. |
| 81 | + |
| 82 | +> [!IMPORTANT] |
| 83 | +> 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. |
| 84 | + |
| 85 | +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. |
| 86 | + |
| 87 | +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. |
| 88 | + |
| 89 | +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. |
| 90 | + |
| 91 | +The heap can be considered as the accumulation of two heaps: the large object heap and the small object heap. |
| 92 | + |
| 93 | +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. |
| 94 | + |
| 95 | +## Generations |
| 96 | + |
| 97 | +The heap is organized into generations so it can handle long-lived and short-lived objects. Garbage collection primarily occurs with the reclamation of short-lived objects that typically occupy only a small part of the heap. There are three generations of objects on the heap: |
| 98 | + |
| 99 | +* **Generation 0.** This is the youngest generation and contains short-lived objects. An example of a short-lived object is a temporary variable. Garbage collection occurs most frequently in this generation. |
| 100 | + |
| 101 | + Newly allocated objects form a new generation of objects and are implicitly generation 0 collections, unless they are large objects, in which case they go on the large object heap in a generation 2 collection. |
| 102 | + |
| 103 | + Most objects are reclaimed for garbage collection in generation 0 and do not survive to the next generation. |
| 104 | + |
| 105 | +* **Generation 1.** This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects. |
| 106 | + |
| 107 | +* **Generation 2.** 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 live for the duration of the process. |
| 108 | + |
| 109 | +Garbage collections occur on specific generations as conditions warrant. Collecting a generation means collecting objects in that generation and all its younger generations. A generation 2 garbage collection is also known as a full garbage collection, because it reclaims all objects in all generations (that is, all objects in the managed heap). |
| 110 | + |
| 111 | +### Survival and promotions |
| 112 | + |
| 113 | +Objects that are not reclaimed in a garbage collection are known as survivors, and are promoted to the next generation. Objects that survive a generation 0 garbage collection are promoted to generation 1; objects that survive a generation 1 garbage collection are promoted to generation 2; and objects that survive a generation 2 garbage collection remain in generation 2. |
| 114 | + |
| 115 | +When the garbage collector detects that the survival rate is high in a generation, it increases the threshold of allocations for that generation, so the next collection gets a substantial size of reclaimed memory. The CLR continually balances two priorities: not letting an application's working set get too big and not letting the garbage collection take too much time. |
| 116 | + |
| 117 | +### Ephemeral generations and segments |
| 118 | + |
| 119 | +Because objects in generations 0 and 1 are short-lived, these generations are known as the ephemeral generations. |
| 120 | + |
| 121 | +Ephemeral generations must be allocated in the memory segment that is known as the ephemeral segment. Each new segment acquired by the garbage collector becomes the new ephemeral segment and contains the objects that survived a generation 0 garbage collection. The old ephemeral segment becomes the new generation 2 segment. |
| 122 | + |
| 123 | + |
| 124 | +The ephemeral segment can include generation 2 objects. Generation 2 objects can use multiple segments (as many as your process requires and memory allows for). |
| 125 | + |
| 126 | +The amount of freed memory from an ephemeral garbage collection is limited to the size of the ephemeral segment. The amount of memory that is freed is proportional to the space that was occupied by the dead objects. |
| 127 | + |
| 128 | +## What happens during a garbage collection |
| 129 | + |
| 130 | +A garbage collection has the following phases: |
| 131 | + |
| 132 | +* A marking phase that finds and creates a list of all live objects. |
| 133 | + |
| 134 | +* A relocating phase that updates the references to the objects that will be compacted. |
| 135 | + |
| 136 | +* 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. |
| 137 | + |
| 138 | +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. |
| 139 | + |
| 140 | +Ordinarily, the large object heap is not compacted, because copying large objects imposes a performance penalty. However, you can use the [GCSettings.LargeObjectHeapCompactionMode](xref:GCSettings#System_Runtime_GCSettings_LargeObjectHeapCompactionMode) property to compact the large object heap on demand. |
| 141 | + |
| 142 | +The garbage collector uses the following information to determine whether objects are live: |
| 143 | + |
| 144 | +* **Stack roots.** Stack variables provided by the just-in-time (JIT) compiler and stack walker. |
| 145 | + |
| 146 | +* **Garbage collection handles.** Handles that point to managed objects and that can be allocated by user code or by the Common Language Runtime. |
| 147 | + |
| 148 | +* **Static data.** Static objects in application domains that could be referencing other objects. Each application domain keeps track of its static objects. |
| 149 | + |
| 150 | +Before a garbage collection starts, all managed threads are suspended except for the thread that triggered the garbage collection. |
| 151 | + |
| 152 | +The following illustration shows a thread that triggers a garbage collection and causes the other threads to be suspended. |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +Thread that triggers a garbage collection |
| 157 | + |
| 158 | +## Manipulating unmanaged resources |
| 159 | + |
| 160 | +If your managed objects reference unmanaged objects by using their native file handles, you have to explicitly free the unmanaged objects, because the garbage collector tracks memory only on the managed heap. |
| 161 | + |
| 162 | +Users of your managed object may not dispose the native resources used by the object. To perform the cleanup, you can make your managed object finalizable. Finalization consists of cleanup actions that you execute when the object is no longer in use. When your managed object dies, it performs cleanup actions that are specified in its finalizer method. |
| 163 | + |
| 164 | +When a finalizable object is discovered to be dead, its finalizer is put in a queue so that its cleanup actions are executed, but the object itself is promoted to the next generation. Therefore, you have to wait until the next garbage collection that occurs on that generation (which is not necessarily the next garbage collection) to determine whether the object has been reclaimed. |
| 165 | + |
| 166 | +## See Also |
| 167 | + |
| 168 | +[Garbage collection in .NET](gc.md) |
0 commit comments