Sunday 15 July 2018

Memory Management In Java


Java Memory Management In Java 8

The heap is sometimes divided into two areas (or generations) called the nursery (or young space) and the old space. The nursery is a part of the heap reserved for allocation of new objects. When the nursery becomes full, garbage is collected by running a special young collection, where all objects that have lived long enough in the nursery are promoted (moved) to the old space, thus freeing up the nursery for more object allocation. When the old space becomes full garbage is collected there, a process called an old collection.


Java heap memory is the run time data area from which the Java Virtual machine allocates memory for all class instances and arrays. The heap may be of a fixed or variable size. The garbage collector is an automatic memory management system that reclaims heap memory for objects.




Eden Space: The memory pool from which memory is initially allocated for most objects.

Survivor Space: The memory pool containing objects that have survived the garbage collection of the Eden space. We have two equally divided survivor spaces called S0 and S1.

Tenured Generation or Old Gen: The memory pool containing objects that have existed for some time in the survivor space.

Note:- Metaspace and Code Cache are part of non-heap memory.


Non-heap memory

Non-heap memory includes a method area shared among all threads and memory required for the internal processing or optimization for the Java VM. It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors.

Meta Space(Before java 8 it was known as Permanent Generation): This memory is out of heap memory means non heap memory and part of the native memory.Meta space does not have an upper limit.This is designed to grow in order to avoid out of memory errors.This space is used to store the class definitions loaded by class loaders and class and method objects.If if it grows more than the available physical memory then operating system will use Virtual memory. But swapping the data from physical memory to virtual memory and vice versa will decrease the application performance. The size of the PermGen space is configured by the Java command line

option -XX:MaxPermSize


Note:- PermGen has been replaced with Metaspace since Java 8 release. PermGen is used by the JVM to hold loaded classes. You can increase it using:

-XX:MaxPermSize=256m


Code Cache: The HotSpot Java Virtual Machine also includes a code cache, containing memory that is used for compilation and storage of native code.


Excessive GC Time and OutOfMemoryError: The parallel collector will throw an OutOfMemoryError if too much time is being spent in garbage collection: if more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, an OutOfMemoryError will be thrown.


No comments:

Post a Comment