What is Java Virtual Machine (JVM)?
The Java compiler produces an intermedia code known as bytecode for a machine that does not exist. This machine is called the Java Virtual Machine and it exists only inside the computer memory. It's a simulated computer within the computer & does all the major functions of a real computer. The process of compiling a Java program into bytecode is depicted as below -
Basically, JVM is a engine that provides runtime environment to drive the Java Code or applications. It converts Java bytecode into machines language. JVM is responsible for allocating memory space.
Th vertual machine code is not machine code. The machine specific code is generated by the Java Interpreter by acting as an intermediatery between the vertual machine and the real machine as we can check from blow mentioned figure -
The Java object framework acts as the intermediary between the user programs & the virtual machine which in turn acts as the intermediary between the Operating System & the Java object framework. How Java works on a typical computer is illustrated in below figure -
JVM Architecture
Let's understand the Architecture of JVM. It contains class loader, memory area, execution engine etc.
Class Loader :
The class loader is a subsystem used for loading class files. It performs three major functions viz. Loading, Linking, and Initialization.
Method Area :
JVM Method Area stores class structures like metadata, the constant run time pool, and the code for methods.
Heap :
All the Objects, their related instance variables, and arrays are stored in the heap. This memory is common and shared across multiple threads.
JVM language Stacks :
Java language Stacks store local variables, and it’s partial results. Each thread has its own JVM stack, created simultaneously as the thread is created. A new frame is created whenever a method is invoked, and it is deleted when method invocation process is complete.
PC Registers :
PC register store the address of the Java virtual machine instruction which is currently executing. In Java, each thread has its separate PC register.
Native Method Stacks :
Native method stacks hold the instruction of native code depends on the native library. It is written in another language instead of Java.
Execution Engine :
It is a type of software used to test hardware, software, or complete systems. The test execution engine never carries any information about the tested product.
Native Method interface :
The Native Method Interface is a programming framework. It allows Java code which is running in a JVM to call by libraries and native applications.
Native Method Libraries :
Native Libraries is a collection of the Native Libraries(C, C++) which are needed by the Execution Engine.
Software Code Compilation & Execution process :
In order to write and execute a software program, you need the following
1) Editor – To type your program into, a notepad could be used for this
2) Compiler – To convert your high language program into native machine code
3) Linker – To combine different program files reference in your main program together.
4) Loader – To load the files from your secondary storage device like Hard Disk, Flash Drive, CD into RAM for execution. The loading is automatically done when you execute your code.
5) Execution – Actual execution of the code which is handled by your OS & processor.
With this background, refer the following video & learn the working and architecture of the Java Virtual Machine.
Why is Java slow?
The two main reasons behind the slowness of Java are
- 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 furthers slows down the speed
However, the latest version of Java has addressed the performance bottlenecks to a great extent.