0.7 – Java Virtual Machine and Java Runtime Environment

by subbu on November 19, 2014

In the previous session, we come across different terms like JVM, JRE, and JIT etc. Here in this session we will be exploring in details.

JVM (Java Virtual Machine)

In the previous session we have learned that, the platform independent byte code generated by the compiler can’t be directly executed on any computer because the byte code is not generated for any particular platform. It is generated only to get executed by the JVM.

native-executable

Here the input to the JVM is the platform independent byte code and output from the JVM is the platform dependent native executable code because JVM interacts with the local platform and produces optimized executable to the local processor for better performance

Now we will see the inner mechanism of JVM. Actually the JVM is designed to see that, the byte code generated by the java compiler is executed on any computer under secured and efficient model.

JVM generally have an Interpreter, Just In Time (JIT) compiler, Garbage collector, Threads synchronizations mechanism and more.

JVM architecture

Java interpreter

Both java interpreter and JIT are used to translate the byte code into executable. Generally the java interpreter translates the total byte code into executable and sends to the system runtime. It is Ok if the size of application is small but takes much startup time and demands more memory if the size of application is big.

Just In Time Compiler (JIT)

If the size of application is big then an environment variable is set in the byte code so that, the byte code is fed to the Just In Time Compiler rather to the interpreter. The JIT compiler simply compiles each portion of code as it is called (just in time). When code has been compiled once, the resultant native executable is stored in the memory until the application exits completely. So that, it does not need to be recompiled next time when the same portion of code is called. It improves the performance and helps to effectively use the memory.

Others

Garbage collector is an integral part of JVM, helps to de-allocate the memory allocation of unused objects, So that the freed memory can be used by some other source. It helps to effectively use the memory by any java application.
Thread synchronization is another mechanism of JVM, helps to execute multiple processes at a time by single processor.

Note: We will discourse Garbage collector and thread synchronization in future chapters at greater length.

Why JVM is called a virtual machine (Abstract Computer)

Java interpreter, Just In Time Compiler, Garbage collector and Thread synchronization work like a machine to translate the byte code into native executable machine code that is not physically existed so called Java Virtual Machine

JVM implementations

JVM is just the specification (blue print) that is the know how in a document format. It explains how the platform independent byte code must be interpreted into native executable code on any machine. It is open to any body who would like to implement and develop their own JVM.

Different vendors have implemented JVMs according to the specification for different platforms. Even oracle has its own implementation of JVM, which will be installed on installing JDK (will be covered in the next session)
The following are different implementations of JVMs from different vendors

JVM Implementer
HotSpot Oracle Corporation
Apache Harmony Apache
Azul zulu Azul Systems
SAP JVM SAP LABS
J9 IBM
MRJ (Mac Runtime for Java) Apple
Microsoft Java Virtual Machine Microsoft Corporation

 

Is the JVM platform dependent or independent?

Different vendors implement JVM specification for different platforms, so JVM is available as different builds for different platforms so is platform dependent.

JRE (Java Runtime Environment)

It is the software environment with the class loader, byte code verifier, JVM, Java API and runtime libraries.

Like predefined functions in C language, Java API (Application Program Interface) is a set of predefined libraries that are required by the JVM to run the java program. Some of the runtime libraries (rt.jar) for memory management and error handling are also installed with JRE.

The class loader locates and loads the required classes from either java API or runtime library which are used in java application
The byte code verifier verifies the byte code, allows further if it feels no threat to the computer. It helps to enforce the security from unauthorized access to the local file system and hardware by the non-byte code.
Every computer must be pre loaded with Java Runtime Environment to run any java application.

Java Runtime Environment

Summary

  • Combination of java interpreter, Just In Time Compiler, Garbage collector and Thread Synchronization is called JVM
  • JVM translates byte code into native executable code
  • Different vendors implement JVM according to their requirement
  • Combination of JVM, class loader, byte code verifier, Java API and Runtime libraries is called Java Runtime Environment (JRE).
  • Every computer must be pre installed with JRE to run any java application

Previous post:

Next post: