When executing a java program, you may simply input in terminal, like:

javac HelloWorld.java
java HelloWorld

Behind these commands, JVM, as the cornerstone of the Java platform, does a lot of work, which are what this blog attempts to figure out. Today, a new series about JVM will be started, and this is the first episode.

JVM’s Internal Architecture

An overview of JVM architecture lists above.

Execution Engine

When javac is called, Java Compiler compiles Java source code into Java bytecodes, after which, a class file would be generated, then java command executes Java class file.

For detail, the JIT compiler compiles the entire bytecode to change it to native code that execution engine directly uses.

Besides JIT compiler, interpreter is the other option to executes the bytecode instructions one by one. It can quickly interpret one bytecode, but slowly executes the interpreted result. Execution in native code compiled by JIT is much faster than interpreting instructions one by one.

The class File Format

“Write once, run anywhere” (WORA) is a slogan created by Sun Microsystems to illustrate the cross-platform benefits of the Java language. JVM is the component of the technology responsible for its hardware- and operating system-independence.

The key to achieve independence is class file format, the binary format used to represent compiled classes and interfaces.

Implementation details that are not part of this post would be left to next blog.

Class Loader

If a class loader finds an unloaded class, the class is loaded and linked by following the process illustrated below.

Each stage is described as follows.

  1. Loading: A class is obtained from a file and loaded to the JVM memory. Verifying: Check whether or not the read class is configured as described in the Java Language Specification and JVM specifications. This is the most complicated test process of the class load processes, and takes the longest time. Most cases of the JVM TCK test cases are to test whether or not a verification error occurs by loading wrong classes.
  2. Preparing: Prepare a data structure that assigns the memory required by classes and indicates the fields, methods, and interfaces defined in the class.
  3. Resolving: Change all symbolic references in the constant pool of the class to direct references.
  4. Initializing: Initialize the class variables to proper values. Execute the static initializers and initialize the static fields to the configured values.

Run-Time Data Areas

The Java Virtual Machine defines various run-time data areas that are used during execution of a program. Some of these data areas are created on Java Virtual Machine start-up and are destroyed only when the Java Virtual Machine exits. Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits.

The pc Register

The Java Virtual Machine can support many threads of execution at once. Each Java Virtual Machine thread has its own pc (program counter) register. At any point, each Java Virtual Machine thread is executing the code of a single method, namely the current method for that thread.

If that method is not native, the pc register contains the address of the Java Virtual Machine instruction currently being executed. If the method currently being executed by the thread is native, the value of the Java Virtual Machine’s pc register is undefined. The Java Virtual Machine’s pc register is wide enough to hold a returnAddress or a native pointer on the specific platform.

JVM Stacks

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. A Java Virtual Machine stack stores frames. A Java Virtual Machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return.

Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java Virtual Machine stack does not need to be contiguous.

Heap

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java Virtual Machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor’s system requirements.

Method Area

The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads. The method area is analogous to the storage area for compiled code of a conventional language or analogous to the “text” segment in an operating system process. It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods used in class and interface initialization and in instance initialization.

The method area is created on virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it.

Native Method Stacks

An implementation of the Java Virtual Machine may use conventional stacks, colloquially called “C stacks,” to support native methods (methods written in a language other than the Java programming language).

Native method stacks may also be used by the implementation of an interpreter for the Java Virtual Machine’s instruction set in a language such as C. Java Virtual Machine implementations that cannot load native methods and that do not themselves rely on conventional stacks need not supply native method stacks. If supplied, native method stacks are typically allocated per thread when each thread is created.

Besides the description mentioned here, the JVM has various features and technologies. Hope we can capture all these strongholds later.

Reference

  1. The Structure of the Java Virtual Machine
  2. 一点一滴探究JVM之类加载机制
  3. Understanding JVM Internals
  4. JVM architecture介绍
  5. JVM 的 工作原理,层次结构 以及 GC工作原理
  6. Java虚拟机及GC基础介绍