Published October 18, 2022 by

JVM Architecture : Java virtual Machine

A virtual machine is a layer of abstraction that gives a program one simplified interface for interacting with a variety of physical computers and their operating systems.

JVM core

Design Goals of Java

  1. Java must enable the development of secure, high-performance, and highly robust applications on multiple platforms in heterogeneous, distributed networks.
  2. Java must be architecture-neutral, portable, and dynamically adaptable.
  3. Simple, Familiar, Object Oriented, Multithreaded.

Java Environments

A specific environment that comprises all the necessary components, application programming interfaces, and libraries in order to develop, compile, debug and execute its programs.


Java has 2 such environments and everyone working with Java has to start their work after setting up one of these environments on their local development or production environment platforms.

JDK core

  • Local development (For Developers)
    • JDK (Java Development Kit): the complete development environment used for developing and executing Java applications. It includes both JRE and development tools. JDK is meant for programmers
  • Production environment platform. (For Users)
    • JRE (Java Runtime Environment): the minimum environment needed for running a Java application (no support for developing). It includes JVM (Java Virtual Machine) and deployment tools. JRE is meant for users


Why is Java slow?

Dynamic Linking = Unlike C, linking is done at run-time, every time the program is run in Java.

Run-time Interpreter = the conversion of byte code into native machine code is done at run-time in Java which further slows down the speed.

JVM


1. .class file:

Which is Byte Code is converted by the compiler.

Below the example :  . class file for magin number program.


.class file


2. Class Loader Subsystem

It is mainly responsible for three activities


Class Loader Subsystem

Loading:

  • Loads all the Java Libraries, JDK Extensions, and Class Loading from the .class file.

Linking:

  • Does Verification of Is the .class file from the right compiler, file formatted.
  • Do prepare for the memory allocation for variables, class Variables.
  • Does method name calling matching of eight method names , symbolic reference.

Initialization:

  • In this phase, all static variables are assigned with their values defined in the code and static block. class initializing,.


3. RunTime Data Area

MEMORY ALLOCATION for the Complete Program




Method area :In the method area, all class-level information like class name, immediate parent class name, methods and variables information, etc. are stored, including static variables. There is only one method area per JVM, and it is a shared resource.

Heap area : Information of all objects is stored in the heap area. There is also one Heap Area per JVM. It is also a shared resource.

Stack area : For every thread, JVM creates one run-time stack which is stored here. Every block of this stack is called an activation record/stack frame which stores method calls. All local variables of that method are stored in their corresponding frame. After a thread terminates, it’s run-time stack will be destroyed by JVM. It is not a shared resource.

PC Registers: Store address of current execution instruction of a thread. Obviously, each thread has separate PC Registers.

Native method stacks:For every thread, the separate native stack is created. It stores native method information.


4. Execution Engine

Actual Process Execution starts here after all the Verifications, Checks, allocations, and Storage in RAM Temp. are done.

Interpreter: Executes each and every Instruction Line by line in the byte Code.


JIT Compiler: (Just in time) Compile the code fast compilation. As it compiles Unique methods once it could not Compile the same method code again thereby saving TIME. First, it compiles Byte Code to native machine code. The native code is stored in the cache, thus the compiled code can be executed more quicker.

JIT compiler softwares:

  • Oracle Hotspot VMs
  • IBM AOT (Ahead-Of-Time) Compiling

Execution Engine


Garbage Collector: Creates Objects for class by the code and when it is not required it will remove the uncalled objects that remained in the memory.

→ This is done by the Garbage Collector: System.gc()


5. Java Native Interface (JNI)

It Interacts with the Native Library which is required for the execution and loads.This enables JVM to call C/C++ libraries and to be called by C/C++ libraries which may be specific to hardware.

Java Native Interface (JNI)


6. Native Method Libraries

A collection of C/C++ Native Libraries which is required for the Execution Engine and can be accessed through the provided Native Interface.


Native Method Libraries



JVM Pros & Cons

Security: JVM will validate all programming before it is executed, so that not to give any errors from Java code to direct to JVM.

Cross-Platform: JVM allows programs to be written and compiled once, which can be run on wide various systems / OS without modification.

Speed: The Byte Code get from the compiler, as JVM will convert it into Underlying Architecture Machine Code rapidly and executes the instruction. Compared to any other execution time.

Platform-Specific Feature: Java programs usually do not use structures specific to one OS because the Java Virtual Machine must run continuously on a wide variety of computers. In Java Applications “Look & Features” can frequently be different from the operating system that the device comes with, and applications written specifically for that OS.

 

Read More