Heap memory

Heap memory is a part of memory allocated to JVM, which is shared by all executing threads in the application.
 It is the part of JVM in which all class instances and are allocated. It is created on the Start-up process of JVM.
 It does not need to be contiguous, and its size can be static or dynamic.
 Space allocated to the memory is reclaimed through an automatic memory management process called garbage collection. Heap memory is a shared area that is utilized during the runtime of Java applications.
 It is created during the instantiation of Java Virtual Machine (JVM).
This memory is shared by instances of all the classes created during the runtime of an application. As per system configuration, the size of heap memory may be fixed or variable. 
In order to reclaim the space of heap memory, an automatic memory management process called garbage collection is triggered by JVM.
 JVM provides control to developers to vary the size of heap memory according to requirement.
New objects are always created in heap space, and the references to these objects are stored in stack memory.
These objects have global access and we can access them from anywhere in the application.
It's accessed via complex memory management techniques that include the Young Generation, Old or Tenured Generation, and Permanent Generation.
If heap space is full, Java throws java.lang.OutOfMemoryError.
Access to this memory is comparatively slower than stack memory
This memory, in contrast to stack, isn't automatically deallocated. It needs Garbage Collector to free up unused objects so as to keep the efficiency of the memory usage.
Unlike stack, a heap isn't threadsafe and needs to be guarded by properly synchronizing the code.
Posted on by