In Java, a heap is a chunk of memory which is shared among all threads. In a heap, all class instances and the array is allocated. It is created when JVM starts-up. An automatic storage management system reclaims heap. It may be of fixed and variable size. It does not need to be contiguous.
The heap is divided into two parts (or generations):
It is the part of the heap. It is reserved for allocation of objects. When it becomes full, the minor GC collects the garbage from Eden to one of the survivor spaces. Objects that are available for garbage in one of the survivor spaces are cleared, and remaining objects move to the other survivor space. Hence, there is always one empty survivor space. All objects that live long in the young space are transfers to old space.
The recent release of Java has a part of the nursery called keep area. It contains the most recently allocated object in the nursery. It does not collect garbage until the next young generation.
All the live objects from the whole young generation (Eden space + Survivor from space) are moved to the Survivor To space. After completion of this process, a survivor from space is empty. The process of copying the live objects between survivor spaces is repeated several times until some objects are considered to have matured and old enough. Such objects can thus move to old space instead of move into other survivor space. To determine whether the object is ready to move into old space, GC counts the total number of collections an object survived. The life of the objects increased if it does not collect.
It contains objects that are lives long. This process is called an old collection. Long-lived objects survived after many rounds of minor GC. When the old space is about to reach its limit, major resources of old memory are cleared. Usually, the garbage collector is performed in the old generation when it reaches its limit. An old generation garbage collector is called a major GC. It takes a too long time to remove the objects.
Here a question arises "what happens if an object in the old generation needs to reference an object in the young generation?"
To handle this type of cases, JVM manages a table in the old generation called card table. It is a memory chunk of 512 bytes. When an object in the old generation references an object in the young generation, it records in the card table. When minor GC executes for the young generation, GC search for this table only. It determines whether or not it is subject for GC, instead of checking the reference of all the objects in the old generation. Write barrier manage the card table. It is a device that allows faster performance for minor GC.
It includes a method area shared among all threads. It stores the structure of each class. It may be of fixed or variable size. It does not need to be contiguous.
Permanent Generation (PermGen)
JVM generates it at runtime. It contains the application metadata required by JVM. Metadata includes the classes and methods used in the application. It also includes the Java SE library classes and methods.
Metaspace replaced it in Java 8. It means java.lang.OutOfMemoryError does not occur in Java 8. Two new flag introduced in metaspace they are: -XXMetasapceSize and ?XXMaxMetaspaceSize. The main motive of metaspace is that, as long as the classloader is alive, the metadata remains alive in the metaspace.
The main difference between PermGen and metaspace is: PermGen is a part of heap while the metaspace is a part of native memory.
Code Cache: It is a memory area separate from the heap. It is used for compilation and storage of native code. It is a fixed size space. If it became full, the JVM would not compile any additional code. To avoid this, you can tune the code cache with the following size options:
JVM uses a Use Code Cache Flushing option to control the flushing of the code cache area. Its default value is false. Just-in-time (JIT) compiler is the biggest user of the code cache area.
Causes of java.lang.OutOfMemoryError
The error triggers when the application attempts to add more data into the heap space area, but there is not enough space for it. JVM throws Java heap space error whenever it reaches the heap size limit.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at MemoryLeaksDemo.main(MemoryLeaksDemo.java:5)