Java Memory Management - My Empty Mind


Java Memory Management

Java provide excellent feature called garbage collection, it allows developer to create object without any worries.

Java only take care of memory allocation and de-allocation (In C/C++ developer has to take care of Object memory allocation and memory de-allocation). 
important work of Garbage Collection is to free unwanted Object space.

As you see the below image JVM memory is divided different part, At high level it is divided in two major parts
  1. Young generation
  2.  Old generation

Young generation - Young generation is area where all new objects are created. When this area is filled garbage collection is performed.this garbage collection is called Minor GC. young generation is further divided in to different parts.
  • Eden Memory  
  • Survivor memory spaces  (S1 and S0 as shown in Above Image)
How garbage collection works in Eden Memory?
Most of newly created objects are created in Eden memory space. When Eden space is filled with newly created objects then minor GC is performed and all objects are moved to one of Survivor space, at same time minor GC also check survivor space and available objects move them to Other survivor space.
Objects which are Lived after many cycles of GC, are moved to Old generation memory space.

How garbage collection works in Old Generation Memory?
Old generation memory contains objects which are long lived and survived after many cycles of Minor GC. when Old generation memory space is full with Objects, then garbage collection is performed, this garbage collection is called Major GC. There is one drawback of Major GC, when garbage collection is performed 
all threads are stopped until operation completes.

Permanent generation
permanent generation or 'Perm Gen' contains application metadata required by JVM to explain methods and classed used in application. 
Perm gen is filled by JVM at run time.

No comments:

Post a Comment