System Design Blog Series - JVM

Siva Naik
5 min readOct 29, 2022

--

Java technology is both a programming language and a platform.

The Java Programming Language

The Java programming language is a high-level language that can be characterized by all of the following buzzwords:

  • Simple
  • Object-oriented
  • Distributed
  • Multithreaded
  • Dynamic
  • Architecture neutral
  • Portable
  • High performance
  • Robust
  • Secure

Each of the preceding buzzwords is explained in The Java Language Environment, a white paper written by James Gosling and Henry McGilton.

In the Java programming language, all source code is first written in plain text files ending with the .java extension. Those source files are then compiled into .class files by the javac compiler. A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine1 (Java VM). The java launcher tool then runs your application with an instance of the Java Virtual Machine.

An overview of the software development process.

Because the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris™ Operating System (Solaris OS), Linux, or Mac OS. Some virtual machines, such as the Java SE HotSpot at a Glance, perform additional steps at runtime to give your application a performance boost. This includes various tasks such as finding performance bottlenecks and recompiling (to native code) frequently used sections of code.

Through the Java VM, the same application is capable of running on multiple platforms.

The Java Platform

A platform is the hardware or software environment in which a program runs. We’ve already mentioned some of the most popular platforms like Microsoft Windows, Linux, Solaris OS, and Mac OS. Most platforms can be described as a combination of the operating system and underlying hardware. The Java platform differs from most other platforms in that it’s a software-only platform that runs on top of other hardware-based platforms.

The Java platform has two components:

  • The Java Virtual Machine
  • The Java Application Programming Interface (API)

You’ve already been introduced to the Java Virtual Machine; it’s the base for the Java platform and is ported onto various hardware-based platforms.

The API is a large collection of ready-made software components that provide many useful capabilities. It is grouped into libraries of related classes and interfaces; these libraries are known as packages. The next section, What Can Java Technology Do? highlights some of the functionality provided by the API.

The API and Java Virtual Machine insulate the program from the underlying hardware.

As a platform-independent environment, the Java platform can be a bit slower than native code. However, advances in compiler and virtual machine technologies are bringing performance close to that of native code without threatening portability.

The terms” Java Virtual Machine” and “JVM” mean a Virtual Machine for the Java platform.

  • credits @ Oracle Documentation

JVM

JVM has several components

Now, Lets jump into each part.

1.Class Loader SubSystem :

Java Dynamic Class loading functionality is handled by class loader subsystem. “Load -> Link -> Initialize the class file” for the first time at runtime.

1.1. Loading : Classes will be loaded by this component.

  1. Bootstrap Class Loader — loads class from bootstrap class path (rt.jar -high priority)
  2. Extension Class Loader — loads the class which included in (jre/lib).
  3. Application Class Loader — Loads application level class.

1.2 Linking : Performs the Verification,Preparation and Resolution on Class loaded.

  1. Verify : Bytecode verifier will verify whether the generated bytecode is proper or not , else we will get Verification Error(java.lang.VerifyError)
  2. Prepare : For all static variables in bytecode , memory will be allocated and default values will be loaded.
  3. Resolve(Optional): Symbolic References of class will replaced with original References from method area.

1.3 Initialization : Final phase of class loader , here all static variables will be assigned original values & static block gets executed.

2. Runtime Data Area : (JVM Memory) — divided into 5 components

  1. Method Area : this will store all the class level data (fields, method,static variable). Only one method area per JVM.
  2. Heap Area : all the objects & corresponding instance , variable , array will be stored. One Heap per JVM
  • where Heap & Method Area are not thread safe it shared resource for JVM

3. Stack Area : For every thread new stack at runtime will be created. for every method call , one entry will be added in stack called stack frame.

  1. Local Variable will be stored in stack memory
  2. Thread safe
  3. Stack Frame is divide into three sub entities:

3.1 Local Variable Array: related to local method , variables are invloved

3.2 Operand Stack : Intermediate Operation — acts as runtime workspace to perform operation

3.3 Frame Data : all symbols corresponding to method will be stored , in case of any exception , catch block information will be maintained.

4. PC Registers: Each Thread will have register which stores the current executing operation.(once each instruction updating , PC register also will update next instruction)

5. Native Method Stack: Holds native method information for every thread.

3. Execution Engine : Byte code which is assigned in Runtime data area will be executed.

  1. Interpreter: Interprets the bytecode faster but execution slowly.(one method is called multiple times it will create new Interpretation is required).
  2. JIT Compiler : neutralize the disadvantage of interpreter.whenever it finds repeated code it uses JIT Compiler.

It will compile the bytecode into native code (Native code will used directly for Repeated method calls).

Intermediate Code Generator : Generates intermediate code whenever required

Code Optimizer: Optimize the code

Target Code Generator : Converts to Machine / Native Code

Profiler: Helps to find the multiple method calls (finding hotspots)

3. Garbage Collector : Collects and removes unreferenced objects

4. Java Native Interface : Interacting with native libraries and provides Libraries for Execution Engine.

5. Native Interface Library : collection of native libraries which required for execution engine.

--

--