JVMGarbageCollectorBasics

Unofficial Content
Associated with the Memory Management, in Java exists an Automatic Memory Management System (called Garbage Collector) which is in charge memory management. It means, releasing memory used by the dead objects (objects that are no longer referenced\used by the application).

The Virtual Machine garbage collector uses generational garbage collection which divides memory into several generations (old and young generation), and assigns each a memory pool., managing two or more generations segregating objects by age (memory pools holding objects of different ages). This exploits the following two observations:
  • Most allocated objects will die young.
  • Few references from older to younger objects exist.
To take advantage of this hypothesis, the Heap is split into two physical areas:
  • Young Generation. Most newly allocated objects are allocated here, which is typically small and collected frequently; most objects in it are expected to die quickly. It is split into three separate areas:
    • The Eden Space. This is where most new objects are allocated. The Eden is always empty after a minor collection.
    • The Two Survivor Spaces. These hold objects that have survived at least one minor collection but have been given another chance to die before being promoted to the old generation. Only one of them holds objects, while the other is unused.
  • Old Generation. Objects that are longer-lived are eventually promoted, or tenured, to the old generation

Type of Collectors

As an overview, we can say thant the Java HotSpot VM has three different garbage collectors, each of which is targeted to a different set of applications.
  • Serial Collector: The configuration of the Serial Collector is a copying young generation and a mark/sweep/compact old generation. This is the collector of choice for most applications that do not have low pause time requirements and run on client-style machines. It takes advantage of only a single CPU for garbage collection work (hence, its name). Still, on today's hardware, the Serial Collector can efficiently manage a lot of non-trivial applications with a few 100MBs of heap, with relatively short worst-case pauses (around a couple of seconds for major collections).
  • Parallel Collector: available on server-style machines. The minor collections take place in parallel, using all available CPUs (Parallel copying young generation).This is the default collector used with the Server VM.
  • Concurrent Mark-Sweep (CMS) or Mostly-Concurrent Collector: It manages its young generation the same way the Parallel and Serial Collectors do. Its old generation, however, is managed by an algorithm that performs most of its work concurrently.
More information