JVM Memory Structure
Understanding the memory model used by Java is very helpful when writing Java programs.
JVM(Java Virtual Machine)
A Java process runs on a virtual system called the JVM. This structure allows compiled Java class files (bytecode) to run in many operating system environments.

Java Memory Area Structure
Every Java program runs through the Java Virtual Machine (JVM). When a Java program starts, the JVM receives the memory it needs from the operating system to run that program.
To use the allocated memory efficiently, the JVM divides and manages memory space by purpose as follows.

The Runtime Data Area is the memory area allocated by the operating system, and it stores the data used when running a Java application.
It can be broadly divided into the Method Area, Heap Area, Stack Area, PC Register, and Native Method Stack.
Method Area / Static Area
The method area stores information about classes used in a Java program, along with class variables (static variables).
When a specific class is used in a Java program, the JVM reads the class file (*.class), which is Java bytecode, and stores the runtime constant pool, member variables (fields), class variables (static variables), constructors, and methods for the loaded class and interfaces.
The contents stored in this area are loaded before the program starts and are destroyed when the program ends.
The runtime constant pool contains various constants, from numeric literals known at compile time to method and field references that must be resolved at runtime.
In computer science, a literal means a fixed value. A constant is a variable that does not change, while a literal is a value (data) that does not change.
Heap Area
The heap area stores all class object (instance) variables used by a Java program. It is an area dynamically allocated at runtime to store data in the program managed by the JVM.
Data that changes dynamically while the process runs is basically allocated here. In other words, when an instance is created with the new operator in a Java program, the JVM stores the instance information in the heap area.
The heap area is allocated from lower memory addresses toward higher addresses.
If a variable in the stack area references an instance in the heap area and later no longer references it, the instance is removed by garbage collection in the Java Virtual Machine.
Garbage collection is not executed every time an instance is no longer referenced. If it were, the program would consume many resources just by invoking garbage collection, degrading performance. Therefore, it runs according to the garbage collection algorithm.
The lifetime and thread-sharing scope of the heap area are as follows.
- When an object is no longer used or is explicitly declared as null
- Target of GC (Garbage Collection)
- The configuration method or GC method may differ by JVM vendor.
- Shared by all threads.
For applications that run as the same process for a long time, optimizing memory use in this heap area is important. Options for specifying the allocation size are as follows.
-Xms20m -Xmx100m
With the settings above, the configuration is as follows.
- Initial size of memory allocated to the heap area: 20MB
- Maximum size of memory allocated to the heap area: 100MB
Stack Area
The stack area stores a method’s stack frame when a method is called in a Java program.
When a method is called in a Java program, the JVM stores local variables and parameters related to the method call in the stack area. The stack area is allocated when a method is called and disappears when the method call completes. The method call information stored in the stack area is called a stack frame.
The stack area inputs data with Push and outputs data with Pop. Because this stack works in LIFO (Last-In First-Out) order, the data stored last comes out first.
The stack area is allocated from higher memory addresses toward lower addresses.
PC Register
- Holds the address of the JVM instruction currently being executed. It can be said to play the same role as the CPU’s PC.
- Program execution performs instructions on the CPU.
- While the CPU executes instructions, it stores required information in registers, which are storage devices inside the CPU.
- A storage device inside the CPU that stores operation results before sending them to memory. (Usually, while the CPU processes instructions, it stores the information needed during execution in CPU-internal storage called registers. This is inevitably dependent on the CPU. To realize Java’s philosophy, the role of CPU registers is implemented as a logical memory area on the JVM.)
Native Method Stack Area
This is a stack for native code written in languages other than Java.
In other words, it is the stack used to execute code such as C or C++ called through JNI (Java Native Interface).
It stores native method parameters, local variables, and so on as bytecode.