The Java memory model is specified in the latest JVM specification, Java SE 7 Edition, and mainly in the chapters “2.5 Runtime Data Areas” and “2.6 Frames”.
In a nutshell primitive, object and class data are stored in 3 different memory areas:
- Heap Space – holds object data
- Method Area – holds class code
- Native Area – references to the code and object data.
The Method Area is also known as the Permanent Generation Space (PermGen). All class data are loaded into this memory space. This includes the fields, method data and the code for the methods and constructors.
In JDK-8 release, Oracle(JAVA) has completely remove the PermGen space from the JVM. Reason is the consolidation of HotSpot and JRockit. As a result the method area will be part of the operating system’s native heap.
All objects being instantiated during run-time are stored in the Heap Space. The Heap Space again is divided into several parts: Eden, Survivor, and Old Generation Space.
For a better understanding let’s have a look at an example code:
The data are then stored like this:
When we encounter a java.lang.OutOfMemoryError, we often find that Java Heap dumps, along with other artifacts, are generated by the Java Virtual Machine.