Video in progress
List the runtime data areas in the JVM specification.
Shared by all threads: the heap and the method area (Metaspace in HotSpot). Per thread: the PC register, the JVM stack and the native method stack.
📖 Answer
It is easy to split JVM memory into two groups.
The shared areas are the heap (objects and arrays, where the GC works) and the method area (class structure, method code, the runtime constant pool). The specification calls the method area a logical area, and since Java 8 HotSpot keeps it in Metaspace.
The areas that belong to each thread:
- the PC (program counter) register: which bytecode instruction the thread is executing right now. In a native method its value is undefined;
- the JVM stack: a sequence of frames;
- the native method stack: for C/C++ code called through JNI. HotSpot merges it with the JVM stack into one stack.
There are also places not in the specification that take memory in a real JVM: the JIT code cache, the GC's internal structures, direct ByteBuffers. These are exactly what people forget when sizing container memory.
To see all of them:
java -XX:NativeMemoryTracking=summary -jar app.jar
jcmd <pid> VM.native_memory summary
Sign up to mark studied questions and save your progress.