BIG
DATA

JAVA

INTRODUCTION TO JVM

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

JVM (Java Virtual Machine)

When you write and run a Java program, you are tapping the power of java technologies. You express the program in source files written in the Java programming language, compile the source to Java class files, and run the class files on a Java virtual machine.

Together, the Java virtual machine and Java API form a "platform" for which all Java programs are compiled. In addition to being called the Java runtime system, the combination of the Java virtual machine and Java API is called the Java Platform.

At the heart of Java's network-orientation is the Java virtual machine, which supports all three prongs of Java's network-oriented architecture: platform independence, security, and network-mobility.

The Java virtual machine is an abstract computer. Its specification defines certain features every Java virtual machine must have, but leaves many choices to the designers of each implementation. For example, although all Java virtual machines must be able to execute Java bytecodes, they may use any technique to execute them.

JVM is a specification where working of JVM is specified. But implementation provider is independent to choose the algorithm. Its implementation has been provided by Sun and other companies.JVM's implementation is known as JRE (Java Runtime Environment).Whenever you write java command on the command prompt to run the java class, and instance of JVM is created.

What JVM does?

A Java virtual machine's main job is to load class files and execute the bytecodes they contain.JVM contains a class loader, which loads class files from both the program and the Java API. Only those class files from the Java API that are actually needed by a running program are loaded into the virtual machine. The bytecodes are executed in an execution engine.

jre-jdk

The execution engine is one part of the virtual machine that can vary in different implementations. On a Java virtual machine implemented in software, the simplest kind of execution engine just interprets the bytecodes one at a time. Another kind of execution engine, one that is faster but requires more memory, is a just-in-time compiler. In this scheme, the bytecodes of a method are compiled to native machine code the first time the method is invoked. The native machine code for the method is then cached, so it can be re-used the next time that same method is invoked.

When running on a Java virtual machine that is implemented in software on top of a host operating system, a Java program interacts with the host by invoking native methods.

The Class Loader Architecture

A class loader is an object that is responsible for loading classes. In a JVM, each and every class is loaded by some instance of a java.lang.ClassLoader. The class ClassLoader is an abstract class.Java code is compiled into class file by javac compiler and JVM executes Java program, by executing byte codes written in class file. ClassLoader is responsible for loading class files from file system, network or any other source.

There are 3 class loader in java:

Bootstrap or Primordial class loader

Whenever a new JVM is started by typing java MyMainClass, the "bootstrap class loader" is responsible for loading key Java classes like java.lang.Object and other runtime code into memory first. The runtime classes are packaged inside of the JRE\lib\rt.jar file.It is parent of all class loaders in Java. Bootstrap class loader don't have any parents.Bootstrap class loader is also known as Primordial ClassLoader in Java.

Extension class loader

We can store extension libraries, those that provide features that go beyond the core Java runtime code, in the path given by the java.ext.dirs property. JAVA_HOME/jre/lib/ext contains jar packages that are extensions of standard core java classes. The ExtClassLoader is responsible for loading all .jar files kept in the java.ext.dirs path. A developer can add his or her own application .jar files or whatever libraries he or she might need to add to the classpath to this extension directory so that they will be loaded by the extension class loader.

System or Application class loader

The third and most important class loader from the developer perspective is the AppClassLoader. The application class loader is responsible for loading application specific classes from CLASSPATH environment variable, -classpath or -cp command line option, Class-Path attribute of Manifest file inside JAR. Application class loader is a child of Extension ClassLoader.

Except Bootstrap class loader, which is implemented in native language mostly in C, all Java class loaders are implemented using java.lang.ClassLoader.Below is the location from where class loaders loads class files:

  • Bootstrap ClassLoader - JRE/lib/rt.jar
  • Extension ClassLoader - JRE/lib/ext or any directory denoted by java.ext.dirs
  • Application ClassLoader - CLASSPATH environment variable, -classpath or -cp option, Class-Path attribute of Manifest inside JAR file

How class loader works

A class is loaded in Java, only when its needed.Suppose you have a class called HelloWorld.class, request of loading this class will come to Application ClassLoader which will delegate to its parent Extension ClassLoader which further delegates to Primordial or Bootstrap class loader. Primordial will look for that class in rt.jar and since that class is not there, request comes to Extension class loader which looks on jre/lib/ext directory and tries to locate this class there, if class is found there than Extension class loader will load that class and Application class loader will never load that class but if its not loaded by extension class-loader than Application class loader loads it from Classpath in Java.

Execution Engine

Execution Engine executes the bytecode that is assigned to the runtime data areas in the JVM through class loader .The execution engine reads the Java Bytecode in the unit of instruction.But the Java Bytecode is written in a language that a human can understand, rather than in the language that the machine directly executes. Therefore, the execution engine must change the bytecode to the language that can be executed by the machine in the JVM. The bytecode can be changed to the suitable language in one of two ways.

  • Interpreter:"Interpreted" means that the computer looks at the language, and then turns it into native machine language.The Java interpreter, is used to run Java applications from the command line. It takes as an argument the name of a class file to run, as in the following example:
    java HelloWorld

    Although Java class files end with the .class extension, this extension will not be specified when using the interpreter.The class loaded by the Java interpreter must contain a class method called main() that takes the following form:
    public static void main(String[] arguments) {
    // Method here
    }

    Interpreter Reads, interprets and executes the bytecode instructions one by one. As it interprets and executes instructions one by one, it can quickly interpret one bytecode, but slowly executes the interpreted result. This is the disadvantage of the interpret language.

  • JIT (Just-In-Time) compiler:

    The JIT compiler has been introduced to compensate for the disadvantages of the interpreter. The execution engine runs as an interpreter first, and at the appropriate time, the JIT compiler compiles the entire bytecode to change it to native code. After that, the execution engine no longer interprets the method, but directly executes using native code. Execution in native code is much faster than interpreting instructions one by one. The compiled code can be executed quickly since the native code is stored in the cache.

    However, it takes more time for JIT compiler to compile the code than for the interpreter to interpret the code one by one. Therefore, if the code is to be executed just once, it is better to interpret it instead of compiling. Therefore, the JVMs that use the JIT compiler internally check how frequently the method is executed and compile the method only when the frequency is higher than a certain level.

Oracle Hotspot VM uses a JIT compiler called Hotspot Compiler. It is called Hotspot because Hotspot Compiler searches the 'Hotspot' that requires compiling with the highest priority through profiling, and then it compiles the hotspot to native code. If the method that has the bytecode compiled is no longer frequently invoked, in other words, if the method is not the hotspot any more, the Hotspot VM removes the native code from the cache and runs in interpreter mode.

Difference between JDK, JRE and JVM

jvm-jre-jdk
JVM

When we compile a Java file, output is not an 'exe' but it's a '.class' file. '.class' file consists of Java byte codes which are understandable by JVM. Java Virtual Machine interprets the byte code into the machine code depending upon the underlying operating system and hardware combination. It is responsible for all the things like garbage collection, array bounds checking, etc… JVM is platform dependent.

The JVM is called “virtual” because it provides a machine interface that does not depend on the underlying operating system and machine hardware architecture. This independence from hardware and operating system is a cornerstone of the write-once run-anywhere value of Java programs.

JRE

JRE(Java Runtime Environment) contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc. JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system

JDK

JDK(Java Developer Kit) contains tools needed to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), etc… Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method.You need JDK, if at all you want to write your own programs, and to compile them.

For running java programs, JRE is sufficient. JRE is targeted for execution of Java files.
JRE = JVM + Java Packages (like util, math, lang etc) + runtime libraries.
JDK = JRE + development tools