Concept#
Local memory = Direct memory + Metaspace
Direct memory#
- Concept: Direct memory is not part of the virtual machine's runtime data area. It is a memory area that is directly allocated from the system outside of the Java heap. Direct memory uses NIO and operates on native memory through DirectByteBuffer stored in the heap, resulting in high read and write performance.
- Reason: Direct memory is a physical memory specifically used for JVM and IO device interaction. Since JVM operates based on automatic garbage collection, data in memory is constantly moved during GC. If you call a system API to write the memory at a certain location to disk, and at the same time GC moves that data, the operating system may write incorrect data after GC finishes.
Metaspace#
- Concept: The concept of Metaspace appeared in JDK 1.8, previously known as the method area/permanent generation. It stores loaded class information, constants, static variables, constant pool, and JIT-compiled code.
Working memory and local memory#
Each thread in JMM has its own working memory to store shared variables in JVM memory. Each thread can only operate on variables in its own working memory. Working memory is an abstract concept, not actual memory, but rather CPU registers and caches.