Sobes.tech
Junior

What memory areas in JVM are you familiar with?

sobes.tech AI

Answer from AI

The main memory areas in JVM are as follows:

  • Heap:

    • Stores objects of all classes and arrays.
    • Managed by the garbage collector.
    • Divided into Generation (new objects), Old (old objects that have survived several GC cycles), and Permanent Generation (before Java 8, stored class metadata and static variables) or Metaspace (from Java 8 onwards, stores class metadata).
  • Stack:

    • Each thread has its own stack.
    • Stores local variables of primitive types and references to objects.
    • Also contains information about method call frames, including method parameters and return values.
    • The stack size is fixed or set at JVM startup.
  • Method Area:

    • Before Java 8, it was part of Permanent Generation.
    • From Java 8 onwards, class metadata is stored in Metaspace (often outside the heap).
    • Stores class structures, constants (from the constant pool), static variables, and bytecode of methods.
  • PC Register:

    • A register specific to each thread.
    • Contains the address of the instruction currently being executed by the virtual machine.
  • Native Method Stack:

    • For threads executing native methods (written in other languages like C/C++).
    • Managed by the operating system, not JVM.

Schematically, it can be represented as:

            +-------------------------+
            |          Heap           |
            +-------------------------+
            |  Generation (Young)     |
            |  Generation (Old)       |
            |  Metaspace (Java 8+)     |
            +-------------------------+
            |                         |
            |   Stack (per Thread)    |
            +-------------------------+
            |                         |
            |    PC Register (per Thread) |
            +-------------------------+
            |                         |
            | Native Method Stack (per Thread) |
            +-------------------------+