JVM

The basic concept

The JVM (JAVA Virtual Machine) is an imaginary computer that can run JAVA code. It runs on the operating system. Each platform has a different interpreter but implements the same virtual machine, which is why Java can be cross-platform.

thread

The JVM allows an application to execute multiple threads concurrently. When thread local storage, buffer allocations, stacks, and program counters are ready, an operating system native thread is created. The Java thread terminates, and the native thread is reclaimed, releasing all associated resources.

JVM memory region

Thread private

The life cycle is the same as that of a thread, with the user thread starting/ending and creating/destroying

Program Counter Register

  • A small memory space where each thread has a separate program counter that records the address of the virtual machine’s bytecode instructions (that is, the address of the current instruction)

VM Stack VM Stack

  • Each method is executed with a Stack Frame that stores information about local variables, operand stacks, dynamic links, method exits, and so on. The process of each method from invocation to completion corresponds to the process of a stack frame in the virtual machine stack from stack to stack
  • Stack frames are created as a method is called and destroyed as a method completes, counting method completion as normal or abnormal

Native Method Stack

  • Similar to the virtual machine stack, the virtual machine stack serves Java methods, while the local method stack serves Native methods

Threads share

Thread shared areas are created/destroyed when a virtual machine is started/shut down

Heap Heap

  • Save all created objects, arrays
  • The most important memory area for the garbage collector to perform

Method Area/permanent generation

  • Stores information about all classes loaded by the JVM, constants, static variables, code compiled by the just-in-time compiler, etc
  • Runtime Constant Pool The Runtime Constant Pool is also part of the methods area, which holds various literal and symbolic references generated at compile time

JVM runtime memory

The new generation

The new generation is usually used to store new objects, takes up 1/3 of the heap, and frequently triggers MinorGC.

MinorGC uses the replication algorithm to copy the survivable objects in Eden, survivorFrom to the survivorTo zone and add their age +1. Then empty Eden, the objects in survivorFrom, and swap survivorTo and survivorFrom, and the objects in the original survivorTo will become the survivorFrom section of the next GC.

Eden

  • The birthplace of a new Java object, and MinorGC is triggered when this area runs out of memory

SurvivorFrom

  • The survivor of the last GC, the scanned of this GC

SurvivorTo

  • A MinorGC survivor was saved

The old s

In older times, objects are stable, and a MinorGC is usually performed before MajorGC is executed. MajorGC uses a mark-clearing algorithm, which scans all objects, marks alive objects and reclaims all unmarked objects.

The permanent generation

GC does not clean up the permanent area while the main program is running, so it is possible to throw OOM exceptions as more classes are loaded

metadata

In JAVA8, persistent generations have been removed and replaced by metadata sections. It is not in a virtual machine, but uses local memory. Thus, the size of the metadata area is limited only by local memory