What is garbage collector how does it function




















As simple as this sounds, it raises a question: what is the first reference in the tree? Every object tree must have one or more root objects.

As long as the application can reach those roots, the whole tree is reachable. But when are those root objects considered reachable? Special objects called garbage-collection roots GC roots; see Figure 2.

Figure 2. To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a mark-and-sweep algorithm.

As you might intuit, it's a straightforward, two-step process:. Garbage collection is intended to remove the cause for classic memory leaks: unreachable-but-not-deleted objects in memory. However, this works only for memory leaks in the original sense. It's possible to have unused objects that are still reachable by an application because the developer simply forgot to dereference them. Such objects cannot be garbage-collected.

Even worse, such a logical memory leak cannot be detected by any software see Figure 2. Even the best analysis software can only highlight suspicious objects. We will examine memory leak analysis in the Analyzing the Performance Impact of Memory Utilization and Garbage Collection section, below. There are no classic memory leaks. Analysis cannot really identify memory leaks; it can only point out suspicious objects.

This has a couple of important ramifications: Object creation is faster because global synchronization with the operating system is not needed for every single object. An allocation simply claims some portion of a memory array and moves the offset pointer forward see Figure 2. It is possible to include a hint in the code to run the garbage collector with the System.

The best approach to tuning Java garbage collection is setting flags on the JVM. Flags can adjust the garbage collector to be used e. Serial, G1, etc. Young Generation, Old Generation , and more. The nature of the application being tuned is a good initial guide to settings. On the other hand, the CMS garbage collector is designed to minimize pauses, making it ideal for GUI applications where responsiveness is important. Additional fine-tuning can be accomplished by changing the size of the heap or its sections and measuring garbage collection efficiency using a tool like jstat.

Prefix works with. Click here to read more about the acquisition. Try Our Free Code Profiler. Try Our Code Profiler. By Role. By Technology.

By Language. Documentation Support Ideas Portal Menu. Start Free Trial. 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 Windows 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 Windows VirtualFree function. 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, don't 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's 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 objects that are 85, bytes and larger, which are usually arrays.

It's rare for an instance object to be extremely large. You can configure the threshold size for objects to go on the large object heap. Garbage collection primarily occurs with the reclamation of short-lived objects.

To optimize the performance of the garbage collector, the managed heap is divided into three generations, 0, 1, and 2, so it can handle long-lived and short-lived objects separately.

The garbage collector stores new objects in generation 0. Objects created early in the application's lifetime that survive collections are promoted and stored in generations 1 and 2.

Because it's faster to compact a portion of the managed heap than the entire heap, this scheme allows the garbage collector to release the memory in a specific generation rather than release the memory for the entire managed heap each time it performs a collection. 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. Newly allocated objects form a new generation of objects and are implicitly generation 0 collections. However, if they are large objects, they go on the large object heap LOH , which is sometimes referred to as generation 3. Generation 3 is a physical generation that's logically collected as part of generation 2. Most objects are reclaimed for garbage collection in generation 0 and don't survive to the next generation.

If an application attempts to create a new object when generation 0 is full, the garbage collector performs a collection in an attempt to free address space for the object. The garbage collector starts by examining the objects in generation 0 rather than all objects in the managed heap.

A collection of generation 0 alone often reclaims enough memory to enable the application to continue creating new objects. Generation 1. This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects. After the garbage collector performs a collection of generation 0, it compacts the memory for the reachable objects and promotes them to generation 1.

Because objects that survive collections tend to have longer lifetimes, it makes sense to promote them to a higher generation. The garbage collector doesn't have to reexamine the objects in generations 1 and 2 each time it performs a collection of generation 0. If a collection of generation 0 does not reclaim enough memory for the application to create a new object, the garbage collector can perform a collection of generation 1, then generation 2. Objects in generation 1 that survive collections are promoted to generation 2.

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's live for the duration of the process.

Objects in generation 2 that survive a collection remain in generation 2 until they are determined to be unreachable in a future collection. Objects on the large object heap which is sometimes referred to as generation 3 are also collected in generation 2. 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 objects in all generations that is, all objects in the managed heap. Objects that are not reclaimed in a garbage collection are known as survivors and are promoted to the next generation:. When the garbage collector detects that the survival rate is high in a generation, it increases the threshold of allocations for that generation.

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 large by delaying garbage collection and not letting the garbage collection run too frequently. Because objects in generations 0 and 1 are short-lived, these generations are known as the ephemeral generations.

Ephemeral generations are allocated in the memory segment that's 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. The size of the ephemeral segment varies depending on whether a system is bit or bit and on the type of garbage collector it is running workstation or server GC.



0コメント

  • 1000 / 1000