JVM Study

1 JVM and OS

 

2 JVM JRE JDK

JDK -> JRE -> JVM

 

3 JVM 

The JVM is a stack-based structure. The JVM translates bytecode in two ways: interpretation execution and JIT.

4 JVM Architecture

change init to initiation, execute cinit firstly, then is init.

linking 。

  • Verify: Checks whether the bytecode is safe (verifies format, metadata, bytecode, and symbolic references).
  • Prepare: Assigns initial values to static variables, such as giving an int a value of 0, while final variables are assigned directly. (Member variables are not assigned here; they are initialized in <init>.)
  • Resolve: Replaces symbolic references with direct references (direct references that can be linked directly). The resolution phase activates the class.

 

pc registers

thread-specific data

row number of byte code   It relies on the program counter within each thread to find the next instruction to execute. Each thread has its own independent program counter, allowing different threads to switch execution and resume correctly.
VM stack area
  • thread-specific
  • continuous space
  • local variable table
  • operand stack
  • dynamic linking
  • return address
 -xss
heap area Shared among threads, with a lifecycle that matches the JVM. The space addresses do not need to be contiguous.

Stores object instances. All object instances must be allocated on the heap.

 -Xms

-Xsx

-Xmn

(JDK 8)

 

method area

(meta area)

Shared among threads, with a lifecycle that matches the JVM. The space addresses do not need to be contiguous.

Stores data such as class information that has been loaded by the virtual machine, constants, static variables, and code compiled by the just-in-time compiler.

   
native method area thread specific for native.    

 

5 JVM runtime data

1)runtime area JDK8

In JDK 8, the method area was replaced by Metaspace. Metaspace and direct memory are located outside the JDK, occupying a separate block of memory.

 
2) why change permgen to metaspace
  • Strings in the permanent generation were prone to performance issues and memory overflow.
  • The size of class and method information is difficult to determine, making it challenging to specify the size of the permanent generation. If set too small, permanent generation overflow occurs; if set too large, it may lead to old generation overflow.
  • GC in the permanent generation introduced unnecessary complexity and had low collection efficiency.
  • HotSpot and JRockit were likely to be merged, and JRockit did not have a permanent generation.

6 VM stack area

 

Local Variable Table
  • Stores the eight basic primitive types.
  • Stores object references.
  • Stores return address types.
  • long and double occupy two slots.
Operand Stack
  • Push constants from the local variable table or method onto the operand stack for subsequent execution — this is the "push" operation.
    After computation, the result is returned to the local variable table or to the method.

dynamic linking
  • At runtime, there is a constant pool that stores symbolic references to methods.
    Dynamic linking converts these symbolic references into direct references.
return address

Stores the value of the program counter (PC) register that called this method (for normal return).

 

7 native method area

8 Heap

The largest area in the JVM. The heap stores almost all objects (some objects may be stored in the method area).

  • The largest memory area

  • Shared among threads (except for Thread Local Allocation Buffer, which is private)

  • Created when the virtual machine starts

  • Stores object instances

  • The primary area for garbage collection

  • Current garbage collection algorithms are mostly generational

  • The Java heap is physically discontinuous but logically continuous

  • Objects do not become invalid immediately after a method ends; they wait for GC to reclaim them

 

1)setting the memory size
  • -Xmx specifies the maximum heap size
  • -Xms specifies the initial (minimum) heap size

 

2) 

The heap is divided into several regions:

 
JDK7

 

  • young (Eden,S0,S1)
  • old
  • perm
JDK8

 

  • young
  • old
  • metaspace

 

3)young and old

 

Young Generation Size: Typically 1/3 of the heap
Old Generation Size: Typically 2/3 of the heap

Eden, Survivor0, Survivor1 Ratio: Default is 8:1:1

This means within the Young Generation:

    • Eden occupies 80%

    • Survivor0 (S0) occupies 10%

    • Survivor1 (S1) occupies 10% 

-XX:NewRatio Old = 2,  Old : New。

-XX:SurvivorRation = 8  Eden  : Young.

-xmn young

 

9 Object Allocation Process

  • New objects are created in the Eden space 
  • When Eden Fills Up, Minor GC (Young Generation GC) is triggered, Cleans the entire young Generation (Eden + Survivor spaces)
  • Objects remaining in Eden after GC are moved to Survivor0, Object age increases to 1, S0 is full, objects are moved directly to Survivor1 (S1)
  • When an object's age reaches the threshold (default 15), it's promoted to the Old Generation,Threshold can be set with: -XX:MaxTenuringThreshold=N
  • When Old Generation runs out of space → Major GC (Full GC) is triggered
    If Old Generation still cannot accommodate objects after Major GC → OutOfMemoryError (OOM)

 

10 GC

minor GC young

eden is full, not survival.

Major GC old

 

Mixed GC    

Full GC

 

it cleans young generations and old generations

stop the world event.

 

11 initialization

Class initialization is the final step of the class loading process.

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    static {
        i = 1;
        // System.out.println(i); 报错
    }
    
    static int i = 2;
}
  •  i = 0 
  •  i = 1;
  • System.out.println(i) , error
  • last step is i = 2; 

 

12 GC

1) algorithms

 

reference counting

  • Each object maintains a reference counter

  • When an object is referenced → counter +1

  • When a reference becomes invalid → counter -1

  • When counter reaches 0 → object is considered garbage and can be collected

It is easy to detect, but you can not detect circle references

object1.obj = object.2

object2.obj = object.1

 

object1 = null;

object2 = null;

 

reachability analysis

How it works:

  • Starts from GC Root objects

  • Traverses the reference chain (object graph)

  • If an object cannot be reached from any GC Root → it's considered garbage

 

Two-Marking Process for Object Death

An object is not immediately garbage collected when found unreachable. It goes through a two-marking process before being declared dead.

 

Step 1: First Marking

  • Object is found to have no references from GC Roots

  • Filtering phase: The JVM checks whether the finalize() method needs to be executed

When finalize() is NOT executed (object dies immediately):

  • The class does not override finalize()

  • The finalize() method has already been executed once (finalize runs only once)

  • The object fails to establish a connection to the reference chain in finalize()

 

Step 2: Second Marking

  • If finalize() needs execution, the object is placed in the F-Queue

  • A low-priority Finalizer thread executes the finalize() methods

  • Note: finalize() does NOT execute immediately - it waits for the Finalizer thread

  • During finalize(), the object can "resurrect" itself by establishing a connection to the reference chain

After finalize() execution:

  • The collector performs a second marking on F-Queue objects

  • If the object re-establishes a reference connection → it survives

  • If still unreachable → object is declared dead and collected

 

 

mark and sweep

Approach 1: Mark Alive, Sweep Dead

  • Mark all reachable (live) objects from GC Roots

  • Reclaim all unmarked objects

Approach 2: Mark Dead, Sweep Marked

  • Mark all unreachable (dead) objects

  • Reclaim all marked objects

 

How it works:

  1. Mark phase: Traverse from GC Roots, marking all reachable objects

  2. Sweep phase: Reclaim memory from unmarked objects

 

❌ Memory fragmentation

❌ Unstable execution efficiency

 

 

mark and copy

Process:

  1. Copy live objects → Move all reachable objects to a reserved area (Survivor space)

  2. Initialize remaining area → Quickly clear the original space (fast operation)

  3. Swap roles → The reserved area becomes the new active space

 

 

The "98% Rule"

  • Approximately 98% of objects die in Eden (short-lived objects)

  • Very few objects survive each Minor GC cycle

 

Disadvantages of the Copying Algorithm

  • memory waste
  • performace degrades when 

 

 

mark-compact algorithm

Mark-Compact Algorithm

Process:

  1. Mark phase → Same as Mark-Sweep: mark all live objects from GC Roots

  2. Compact phase → Move all live objects to one end of the memory region, creating contiguous space

 

 

 

13 Reference level

Strong A a = new A(); when memory is running low, don't collect. Throw OOM
Soft String str = new String("abc"); // 
SoftReference<String> softRef = new SoftReference<String>(str);

// When to Use Large Object Allocation

  • When memory is sufficient → Soft references are NOT recycled

  • When memory is insufficient (before OOM) → Soft references ARE recycled

Weak String str = new String("abc"); //
WeakReference<String> weakRef = new WeakReference<String>(str); // 
must be recycled
Phantom PhantomReference Phantom references are the weakest reference type and must be used with a ReferenceQueue. They do not determine object's life and are used only for tracking object回收

 

14 GC

 

CMS,G1.

Serial GC

-XX: +UseSerialGC

 Serial

Stop the world,

mark and copy at safe point

Parallel GC

 

-XX: +ParNew

 

 

Parallel Scavenge  -XX: 

 

CMS GC  JDP 14废除

 

CMS Collection Phases:

1. Initial Mark (Stop-the-World)

  • Marks objects directly reachable from GC Roots

  • Short pause - only scans roots, not the entire heap

  • All application threads stopped briefly

2. Concurrent Mark (Concurrent)

  • Traverses object graph from initial marks

  • Runs concurrently with application threads

  • No pause - user threads continue executing

  • Risk: Application may modify references during marking

3. Remark (Stop-the-World)

  • Corrects changes made during concurrent mark

  • Handles references modified by application threads

  • Short pause - only fixes inconsistencies

4. Concurrent Sweep (Concurrent)

  • Removes garbage objects

  • Runs concurrently with application threads

  • No pause - user threads continue executing

 

Process: (White, Gray, Black)

  1. Initial state: All objects are White

  2. GC Roots: Mark roots as Gray

  3. Marking: Process Gray objects → mark referenced objects as Gray → turn current object Black

  4. Complete: When no Gray objects remain → all reachable objects are Black, unreachable remain White

 

❌ CPU Resource Sensitivity

❌ Floating Garbage

❌ Space Fragmentation

G1 GC  -XX:+UseG1GC

 G1 is designed for multi-core CPUs and large memory servers, and has become the mainstream garbage collector in modern JVMs.

 

Instead of continuous generations, G1 divides the heap into multiple regions:

 

✅ Multi-CPU Optimization

✅ No Fragmentation

✅ Predictable Pause Times

✅ Garbage-First Priority

Screenshot 2026-03-10 at 7.10.59 PM

 

 

Throughput = User Time / (User Time + GC Time)
 

15 GC Optimization:

1) jps is a command-line tool to list running Java processes with various output options.

-l Shows process ID + full main class name or JAR path 12345 com.example.MyApplication
-q Shows only process IDs (quiet mode) 12345
-m Shows process ID + main method arguments 12345 MyApplication --port=8080
-v Shows process ID + JVM arguments 12345 MyApplication -Xmx2g -XX:+UseG1GC
 

2) jinfo - Java Configuration Info

jinfo is a command-line tool to view and dynamically modify JVM parameters for a running Java process. It can also display system properties and dump configuration information.

 

3)  jstat - JVM Statistics Monitoring Tool

jstat is a command-line tool to monitor JVM runtime statistics, including memory status, garbage collection activity, and class loading information.

 

4)  jstack - Thread Dump Tool

jstack is a command-line tool to print Java thread dumps for a running JVM process or core file. It's essential for diagnosing thread-related issues like deadlocks, hangs, high CPU usage, and responsiveness problems.

 

Example:

  1. Kafka consumer, use command "watch -n [interval] -d 'jstat -gc [pid]' " to check, find that young generation gc is 5~10 / sec. So most objects are created in Eden.
  2. JVM didn't allocate the correct parameters for sufficient ratio.
  3. -Xmx 4096m, -Xms 4096m, -Xmn 2560m, -XX Survival Ratio=23
  4. then the young generation gc is 1 / sec, FGC is every 12 hours 1 time. 

 https://blog.csdn.net/weixin_43717407/article/details/118662068

posted @ 2024-03-27 21:17  ylxn  阅读(49)  评论(0)    收藏  举报