Heap memory in Java is the area of memory that is used by the JVM (Java Virtual Machine) to store objects created by the application. It is an area of memory that is shared among all the threads running in the JVM, and it is used to store objects that are created dynamically at runtime.
One of the main characteristics of heap memory is that it is an area of memory that is managed automatically by the JVM. When an object is no longer needed, the JVM will automatically reclaim the memory used by that object, a process known as Garbage Collection.
Unlike stack memory, heap memory is not automatically deallocated when it is no longer needed. To maintain the efficiency of memory usage, the garbage collector must be used to free up unused objects in the heap.
The size of the heap memory is determined by the JVM when it starts, and it can be specified using command-line options. By default, the size of the heap is set to a specific value depending on the platform and the amount of physical memory available on the system.
- Use
-Xms
to specify the initial Java heap size - Use
-Xmx
to specify the maximum heap size - Use
-Xmn
to specify the Young generation size
Example usage:
java -Xms256m -Xmx2048m
The heap memory is divided into two main regions: Young Generation and Old Generation.
The heap memory is divided into two main regions: the young generation and the old generation.
Young generation is further divided into two regions: the Eden space and the survivor space.
Fun Fact: In the Holy books, It is believed that life was first created at Eden Garden.
Eden space is where most new objects are initially allocated. When an object is no longer needed, it becomes eligible for garbage collection. If the object survives the garbage collection process, it is moved to the survivor space. If an object remains in the survivor space for a certain number of garbage collection cycles, it is moved to the tenured space (old space), which is part of the old generation.
When Eden space is filled with objects, a Minor Garbage Collection (GC) process is triggered. During this process, all the surviving objects in the Eden space are moved to one of the survivor spaces.
A threshold is set for the young generation objects to be considered as the old generation. When the threshold is breached, objects are moved to the old generation. -XX:MaxTenuringThreshold parameter specifies for how many minor GC cycles an object will stay in the survivor spaces until it finally gets tenured into the old space.
Example usage:
-XX:MaxTenuringThreshold:10
Old generation is where objects that have survived many garbage collection cycles are stored.
The old generation is collected by Garbage Collection less frequently than the young generation, and the garbage collection process is typically more time-consuming. This is because the old generation contains a larger number of objects that have a longer lifespan, and it can take longer to determine which objects are no longer needed and can be safely removed.
It is important to monitor heap space to ensure that it is not becoming too full and impacting the performance of the application. If the old generation is consistently full or is growing rapidly, it may indicate that the application is creating too many long-lived objects or that the garbage collection process is not effectively removing unneeded objects. If heap space is full, Java throws java.lang.OutOfMemoryError.
When Old Generation is full/near full, a Major Garbage Collection process is triggered.
There are a number of tools available for monitoring heap memory usage, such as the Java VisualVM tool or the Jmap command-line utility.
Conclusion
I tried to focus on basics of Java Heap Memory and its spaces. I hope the post was helpful you.
If you want to understand the Stack memory and differences between Heap Memory and Stack memory, you can access them from the links given below.
Lots of Garbage collection terms are used in this post. Don’t panic! Memory Management 101: Garbage Collection post is on the way :)
Thanks for reading!