In our day-to-day work, we often don’t care about JVM’s internal memory allocation.
However, knowing the basics of the JVM memory model comes in handy for performance optimization and improving code quality.
In this article, we’ll explore JVM storage for the static methods and members.
2. JVM’s Memory Classification
Before deep-diving into memory allocation for the static members, we must refresh our understanding of JVM’s memory structure.
2.1. Heap Memory
Heap memory is the runtime data area shared among all JVM threads to allocate memory for all class instances and arrays.
Java classifies heap memory into two categories – Young Generation and Old Generation.
The JVM internally separates the Young Generation into Eden and Survivor Space. Similarly, Tenured Space is the official name of the Old Generation.
The lifecycle of an object in the heap memory is managed by an automatic memory management system known as a garbage collector.
Therefore, the garbage collector can automatically either deallocate an object or move it in the various sections of the heap memory (young to old generation).
2.2. Non-Heap Memory
Non-heap memory consists primarily of a method area that stores class structures, fields, method data, and the code for methods/constructors.
Similar to the Heap memory, all JVM threads have access to the method area.
The method area, also known as Permanent Generation (PermGen), is considered a part of Heap memory, logically, although the simpler implementations of JVM may choose not to garbage-collect it.
However, Java 8 removes the PermGen space and introduces a new native memory space named Metaspace.
2.3. Cache Memory
The JVM reserves the cache memory area for the compilation and storage of native code, such as JVM internal structures and native code produced by the JIT compiler.
3. Static Members Storage Before Java 8
Before Java 8, PermGen stores static members like static methods and static variables. Additionally, PermGen also stores interned strings.
In other words, PermGen space stores the variables and their technical values, which can be primitives or references.
4. Static Members Storage From Java 8 and Beyond
As we’ve already discussed, PermGen space is replaced with Metaspace in Java 8, resulting in a change for memory allocation of the static members.
Since Java 8, Metaspace only stores the class metadata, and heap memory keeps the static members. Furthermore, the heap memory also provides storage for interned strings.
In this short article, we explored JVM storage for static members.
First, we took a quick look at the JVM’s memory model. Then, we discussed JVM storage for static members before and after Java 8.
Simply put, we know that the static members were a part of PermGen before Java 8. However, since Java 8, they’re a part of heap memory.