Friday, May 3, 2019

Memory Management In Java

Q- What are the different types of memory used by JVM?

In Java memory is divided into five different parts as listed below: 
  • Method Area or Class Area: Class loaded into this area by classloader.
  • Heap: In Java, objects are created into heap area. Heap is divided into three part- Young Generation, Old Generation and Permanent Generation
  • Stack: While a method invoked, a new frame is created in stack and execute after execution completed its delete the frame  automatically. 
  • PC Registers: its store the address of next instruction to be execute in Java virtual machine.
  • Native Method Area Stack: It used for native methods to be executed.
JVM memory is divided into separate parts (before Java 8)
Young Generation: The young generation is used to place newly created objects. That means young generation is place where all the new objects are created. Young Generation is further divided into three parts – Eden Memory and two Survivor Memory spaces. When the young generation is filled, garbage collection is performed. This garbage collection is called Minor GC. 
  • Eden space, where all new objects are stored(located). 
  • When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to one of the survivor spaces.
  • Minor GC also checks the objects in survivor and move them to the other survivor (survivor2) space. So at a time, one of the survivor space is always empty.
  • After many cycles of GC, if Objects that are survived, they are moved to the Old generation memory space. Usually, it’s done by setting a threshold for the age of the young generation objects before they become eligible to promote to Old generation
Old Generation: Long-lived Objects are moved from the Young Generation to the Old Generation. When objects are garbage collected from the Old Generation, it is a major garbage collection event.

Permanent Generation: Permanent Generation or “PermGen” contains Metadata such as classes,String Pool or constant pool and methods used in the application. PermGen also contains Java SE library classes and methods used by the application. Classes that are no longer in use may be  full garbage collected but In Java 8 it was removed and replaced by area called Metaspace into native memory
Note that Perm Gen is not part of Java Heap memory.

No comments:

Post a Comment