JVM, JRE and JDK in Java

JVM, JRE and JDK are the most basic common concepts to know in java. These are the basic features to understand how Java architecture works? JVM stands for Java Virtual Machine, which doesn't have any physical directories created in java installation. JRE stands for Java Runtime Environment, which creates the directory under Java installation path and also present in JDK. JDK stands for Java Development Kit, which creates the directory in Java installation path and also it has it's own JRE. Since we have already learn that Java is platform independent means if we have implemented any of the java class in one environment, it will be executed in any other environment and provides the same output. But, JVM, JRE and JDK all are platform dependent. So that, for windows, linux, unix, mac, solaris..etc has it's own JVM, JRE and JDK. One will be not compatible with other environments.

While installing the Java, we might come to know a bit about JRE and JDK. But, JVM is the other concept which need to be understand very clearly before digging into the Java programming. There are many things which we need to learn as part of JVM, like, class loaders, JIT-Compiler, Memory areas, etc.

JVM - Java Virtual Machine:
Java Virtual Machine is the heart of entire Java program execution process. It is responsible for taking the .class file and converting each byte code instruction into the machine language instructions that can be executed by the microprocessor.

The above diagram represents the architecture of Java Virtual Machine. Here are the few points to understand about the JVM Architecture.
  • The .java program converted into .class file consisting of byte code instructions by the java compiler. Java compiler is outside the JVM. Now, .class file is given to the JVM, there is a module called Class Loader Subsystem.
    • Class Loader Sub System loads the .class file into memory.
    • It verifies all byte code instructions are proper or not. If it finds any instruction suspicious, the execution is rejected immediately.
    • If the byte code instructions are proper, then it allocates necessary memory to execute the program.
    • This memory is divided into 5 parts, called Runtime data areas, which contains the data and results while running the program.
      • Method area: Method area is the memory block, which stores the class code, code of the variables and code of the methods in the java program.
      • Heap: Heap is the area where objects are created. Whenever JVM loads a class, a method and a heap area are immediately created in it.
      • Java Stack: Method code is store in method area. But, while running a method, it need some more memory to store data and results. This memory is called on Java Stack.
      • PC (Program Counter) Registers: These are the registers which contains the memory addresses of the instructions of the methods. If there are 3 methods, 3 PC registers will be used to track the instructions of the methods.
      • Native method: Java methods are executed on Java Stacks. Similarly, native methods (Ex, C/C++ functions) are executed on Native method stacks.
  • Execution engine contains interpreter and JIT (Just-In-Time) Compiler, which are responsible for converting the byte code into machine code so that the processor will execute them. In any other technologies like C, C++, Fortan..etc will use either interpreter or a compiler. But, in Java will use interpreter and JIT compiler both at the same time.
  • After loading .class into the memory, JVM will decide which code is to be left to interpreter and which one to JIT Compiler so that the performance is better.
Class Loaders:
When we generate the .class file from .java file, class loaders will be used to load the .class file. There are 3 different types of class loaders in java.
  1. Bootstrap Class Loader: This is used to load the JDK internal classes, typically rt.jar and other core java classes.
  2. Extension Class Loader: This is used to load classes from ext/extension directory from JAVA installation directory. Ex: C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext
  3. System Class Loader: This loads classes from current class path that can be set while invoking program either using -cp or -classpath command line. It will also set from project build path from eclipse or other IDE's.
Java Runtime Environment:
Java Runtime Environment (JRE) contains a set of software's to run Java application and it also includes the Java Virtual Machine (JVM) and platform supporting libraries. JRE is part of the JDK otherwise we can specially get and install JRE separately.

Java Development Kit:
Java Development Kit contains set of software's to develop Java application. It has it's own Runtime Environment also known as JRE so no need to install separately. JDK includes java development libraries along with JRE. Since, JRE it has it's own JVM that mean JDK has the both of JRE and JVM in it.



Comments

Post a Comment

Popular posts from this blog

how to count the page views by using JSP

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Multithreading in java with example