Please bookmark this post since I will be added reference to other posts and case studies on this subject in the future.
When learning Java for the first time, a lot of focus is often spent on the Java language itself, Object-oriented programming principles, design patterns, compilation etc. and not so much on the Java VM itself such as the Java Heap memory management, garbage collection, performance tuning which are often considered “advanced” topics.
A beginner Java or Java EE programmer ends up creating his first program or Web application. Java Heap memory problems are then often observed such as OutOfMemoryError which can be quite challenging for Java beginners or even intermediates to troubleshoot.
Java Heap Space – Overview & life cycle
Proper knowledge of the Java VM Heap Space is critical; including for Java beginner so my recommendation to you is to learn these principles at the same time you learn the Java language technicalities.
Your Java VM is basically the foundation of your Java program which provides you with dynamic memory management services, garbage collection, Threads, IO and native operations and more.
The Java Heap Space is the memory “container” of you runtime Java program which provides to your Java program the proper memory spaces it needs (Java Heap, Native Heap) and managed by the JVM itself.
Your Java program life cycle typically looks like this:
- Java program coding (via Eclipse IDE etc.) e.g. HelloWorld.java
- Java program compilation (Java compiler or third party build tools such as Apache Ant, Apache Maven..) e.g. HelloWord.class
- Java program start-up and runtime execution e.g. via your HelloWorld.main() method
The Java Heap space is mainly applicable and important for the third step: runtime execution. For the HotSpot VM, the Java Heap Space is split in 3 silos:
- Java Heap for short & long lived objects (YoungGen & OldGen spaces)
- PermGen space
- Native Heap
Now let’s dissect your HelloWorld.class program so you can better understand.
- At start-up, your JVM will load and cache some of your static program and JDK libraries to the Native Heap, including native libraries, Mapped Files such as your program Jar file(s), Threads such as the main start-up Thread of your program etc.
- Your JVM will then store the “static” data of your HelloWorld.class Java program to the PermGen space (Class metadata, descriptors..)
- Once your program is started, the JVM will then manage and dynamically allocate the memory of your Java program to the Java Heap (YoungGen & OldGen). This is why it is so important that you understand how much memory your Java program needs to you can properly fine-tuned the capacity of your Java Heap controlled via –Xms & -Xmx JVM parameters. Profiling, Heap Dump analysis allow you to determine your Java program memory footprint
- Finally, the JVM has to also dynamically release the memory from the Java Heap Space that your program no longer need; this is called the garbage collection process. This process can be easily monitored via the JVM verbose GC or a monitoring tool of your choice such as JConsole
Sounds complex? The good news is that the JVM maturity has improved significantly over the last 10 years and provides you with out-of-the-box tools allowing you to understand your Java program Java Heap allocation monitor it and fine-tuned.
Related posts & case studies
I suggest that you review the articles below for more detail on this topic. You will also find from this Blog several case studies on OutOfMemoryError related problems and resolution strategies.
For any question or additional help please simply post a comment or question below this article. You can also email me directly @email@example.com.