A good understanding of garbage collection theory is necessary for writing high performant applications for the Java platform. Part 2 of the JVM performance tuning blog entries will discuss the theoretical process of garbage collection in detail.
An important question we could ask ourselfs is why we should care about garbage collection?
There is a cost related to the allocation and collection of memory. It can play an important role in how the software performs especially when the application requires large amounts of RAM and forces the OS to use virtual memory. This behaviour often occurs when programs have memory leaks, meaning that memory will be allocated, but not properly released. Although the JVM is responsible for freeing unused memory, a developer has to make it clear what is being unused.
Garbage collection is the process of cleaning up unreachable java objects. Object are said to be unreachable when no more strong reference to it exist. As soon as an object is unreachable it can be collected by the GC. The object is a candidate for collection, but this doesn't mean it will be immediately collected.
The global working of the garbage collection contains three phases:
- Lock it down: Objects participating in garbage collection first need to be locked.
- Mark: iteration phase. Al unreachable object will be marked.
- Sweep: sweeping phase of al the marked objects.
- Serial versus Parallel: Serial GC uses only one thread to perform garbage collection, even when multiple CPU's are available. Parallel GC uses multiple threads to execute GC in parallel. This introduces a little more overhead, but the use of multiple thread decreases the total GC overhead.
- Concurrent versus Stop the World: A stop the world GC stops all the current running application threads at the moment the garbage collector will start cleaning up the dead objects. At that moment the application seems to be frozen. With a concurrent GC only a small part of the overal GC process will stop the application threads. The largest part of GC occurs concurrently with the application threads. Since the state of an object can change during concurrent GC, this type of GC introduces extra overhead an memory requirements.
- Compacting versus non-compacting versus copying: At the moment the GC has deleted the unreachable objects, the GC can decide to perform a compacting operation. It means all the remaining objects will be put next to each other in the java heap to avoid fragmentation. Compacting introduced extra overhead during GC, but makes allocation of new objects faster, since the JVM doesn't need to search for a place in the fragmented heap to store the object. A copying collector copies the objects to another place in memory.
- CMS: concurrent mark sweep (explained in detail later)
GC performance can be measured based on performance metrics:
- Throughput: % of the time not spent on GC
- Garbage Collection Overhead: % of the time spent on GC
- Pause Time: the time application threads will be stopped to perform GC
- Frequency of Collection: how often GC occurs
- Footprint: how many resources (memory and CPU) the GC needs
- Promptness: the time between an object become garbage and the time the object will be cleaned
No comments:
Post a Comment