Deep Dive into the Java Virtual Machine Architecture
Jul 08, 2025 am 02:38 AMJVM is the core of Java program operation, including runtime data area, class loading mechanism, bytecode execution engine and garbage collection mechanism. 1. The runtime data area includes method area (metaspace after JDK8), heap (used to store object instances and perform garbage collection), stack (save thread method call information), local method stack (supports Native methods) and program counter (records the current instruction address). 2. The class loading mechanism consists of three ClassLoaders, Bootstrap, Extension and Application. It follows the parent delegation model and goes through five stages: loading, verification, preparation, parsing and initialization in turn to ensure the security and uniqueness of class loading. 3. The bytecode execution engine runs code through interpretation execution and real-time compilation (JIT). HotSpot uses JIT to dynamically optimize hotspot code to improve performance. 4. The garbage collection mechanism adopts algorithms such as mark-clearing, copying, mark-collation. Modern JVM uses a generational collection strategy to divide the heap into the new generation and the old generation, and adopts suitable GC algorithms respectively. More efficient collectors such as G1 and ZGC can be selected according to the Full GC frequency and time. Mastering these four core modules helps to write efficient and stable Java applications and solve performance problems.
The Java virtual machine (JVM) is the core of Java program operation. It is responsible for loading classes, executing bytecode, and managing the program's runtime resources. Understanding the JVM architecture helps write more efficient and stable Java applications, and can also provide critical help when troubleshooting performance issues.

1. JVM runtime data area: the basic structure of program running
When running Java programs, the JVM divides out multiple memory areas, each of which assumes different responsibilities:

- Method Area : Store class information, constant pools, static variables, etc. In JDK8 and later, this part was replaced by metaspace.
- Heap : This is an area shared by all threads, mainly used to store object instances. Garbage recycling mainly happens here.
- Stack : Each thread has its own stack, which contains multiple stack frames. Each stack frame corresponds to a method call, saving local variables and operand stacks, etc.
- Native Method Stack : used to support the execution of Native methods.
- Program Counter Register : Records the address of the bytecode instruction executed by the current thread.
Understanding the role of these areas can help you locate problems like OutOfMemoryError or StackOverflowError faster.
2. Class loading mechanism: from class files to classes in memory
JVM does not load all classes into memory from the beginning, but loads them on demand. This process is completed by a class loader (ClassLoader) and mainly includes three parts:

- Bootstrap ClassLoader : Responsible for loading JDK core class libraries, such as the content in
rt.jar
. - Extension ClassLoader : Loads the class in the
jre/lib/ext
directory or the class with a specified path. - Application ClassLoader : also known as system class loader, responsible for loading classes on the user classpath.
The process of class loading includes five stages: loading, verification, preparation, parsing, and initialization. Among them, the "parent delegation model" is an important concept, which ensures that classes are not loaded repeatedly and also enhances security.
For example, when you write a custom java.lang.String
class, the JVM will not use your version, but will prefer to use the standard classes provided by the Bootstrap loader.
3. Bytecode execution engine: How does JVM run code
The Java source code is compiled into bytecode ( .class
file) and is then executed by the JVM. There are two main ways to execute JVM:
- Explanation execution : Read bytecode one by one and execute it.
- Instant Compilation (JIT) : Compile hotspot code (often executed code) into local machine code to improve execution efficiency.
The HotSpot virtual machine contains a JIT compiler, which will dynamically optimize code during operation. For example, the code in the loop body is easier to compile into machine code, thereby improving performance.
In addition, the execution engine also works in conjunction with the garbage collection system to automatically manage memory allocation and recycling.
4. Garbage collection mechanism: Who will clean up unused objects?
JVM automatically manages memory, and its core lies in the garbage collection mechanism (GC). The main task of GC is to identify and recycle objects that are no longer used and free up memory.
Common garbage collection algorithms are:
- Mark-Clear (Mark-Sweep)
- Copying
- Mark-Compact
Modern JVMs use generational collection strategies, usually dividing the heap into Young Generation and Old Generation, and different generations use different GC algorithms.
For example, the life cycle of the new generation objects is short, which is suitable for replication algorithms; the survival time of the old generation objects is long, which is suitable for marking and sorting.
If you find that you use Full GC frequently or GC for too long, you may need to adjust the heap size or choose a more suitable garbage collector, such as G1, ZGC, etc.
Basically that's it. The JVM architecture seems complex, but as long as you grasp these core modules, you can have a clear understanding of the Java program running mechanism.
The above is the detailed content of Deep Dive into the Java Virtual Machine Architecture. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Essentials for Java development: Detailed explanation of Java virtual machine installation steps, specific code examples required. With the development of computer science and technology, the Java language has become one of the most widely used programming languages. It has the advantages of cross-platform and object-oriented, and has gradually become the preferred language for developers. Before using Java for development, you first need to install the Java Virtual Machine (JavaVirtualMachine, JVM). This article will explain in detail the installation steps of the Java virtual machine and provide specific code examples.

The Java virtual machine uses reference counting to manage memory usage. When the reference count of an object reaches 0, the JVM will perform garbage collection. The reference counting mechanism includes: each object has a counter that stores the number of references pointing to the object. When the object is created, the reference counter is set to 1. When an object is referenced, the reference counter is incremented. When the reference ends, the reference counter is decremented.

With the continuous development of the Internet, more and more applications and businesses require the use of programs developed in the Java language. For the running of Java programs, the performance of the Java Virtual Machine (JVM) is very important. Therefore, optimizing configuration is an important means to improve the performance of Java applications. Pagoda panel is a commonly used server control panel that can help users manage servers more conveniently. This article will introduce how to use the Pagoda panel to optimize the configuration of the Java virtual machine. Step one: Install Java virtual machine

The stack frame is the basic data structure for executing methods in the Java Virtual Machine (JVM), and includes the following parts: Local variable table: stores the local variables of the method. Operand stack: stores operands and intermediate results. Frame data: Contains return address and current program counter. The functions of the stack frame include: storing local variables. Perform operand operations. Handle method calls. Assist with exception handling. Assisted garbage collection.

Detailed explanation of JVM principles: In-depth exploration of the working principle of the Java virtual machine requires specific code examples 1. Introduction With the rapid development and widespread application of the Java programming language, the Java Virtual Machine (JavaVirtualMachine, referred to as JVM) has also become indispensable in software development. a part of. As the running environment for Java programs, JVM can provide cross-platform features, allowing Java programs to run on different operating systems. In this article, we will delve into how the JVM works

JVM enables Java to run across platforms. 1) JVM loads, validates and executes bytecode. 2) JVM's work includes class loading, bytecode verification, interpretation execution and memory management. 3) JVM supports advanced features such as dynamic class loading and reflection.

Explore: The working principle and core functions of the Java Virtual Machine Introduction: The Java Virtual Machine (JavaVirtualMachine, JVM for short) is the core part of Java program running. It is responsible for compiling Java source code into executable bytecode and executing it. This article will delve into the working principles and core functions of the Java virtual machine, and use specific code examples to help readers better understand. 1. Working Principle of Java Virtual Machine 1.1 Class Loader (ClassLoader) J

Starting from scratch: Detailed explanation of Java virtual machine installation and configuration [Introduction] Java is a cross-platform programming language, and its execution platform depends on the Java Virtual Machine (JavaVirtualMachine, JVM). By installing and configuring the Java virtual machine, you can run Java programs on different operating systems. This article will take you from scratch, detail how to install and configure a Java virtual machine, and provide some commonly used Java code examples. Let’s start learning! [Part 1: J
