JVM Memory Architecture Explained
When a Java application starts, the Java Virtual Machine allocates runtime memory divided into five primary data areas:
- Classloader Subsystem: Loads, links, and initializes
.classbytecode files. - Method Area (Metaspace): Stores class-level structure, static variables, and runtime constant pool.
- Heap Memory: Stores all instantiated objects. Divided into Young Generation (Eden, S0, S1) and Old (Tenured) Generation.
- JVM Stack: Stores stack frames for active method invocations containing local primitive variables and object references.
- PC Registers & Native Method Stacks: Track instruction execution addresses for each thread.
Static vs Final Keywords in Java
- `static`: Associates variable or method with the Class rather than individual instances. Allocated once in Method Area.
- `final`: Prevents mutation. When applied to variables, value cannot be reassigned; applied to methods, prevents overriding; applied to classes, prevents inheritance (e.g.
java.lang.String).