Thursday, 10 December 2015

Key features of JVM (Java Virtual Machine)

Java Tutorial:

Key Features of JVM (Java Virtual Machine):

Q1. Which part of JVM allocates the memory for Java Program?
When we execute a java program, Class loader of JVM will allocate the necessary memory.

Q2. How Java is platform independent?
In any other language only complier or interpreter is being used to execute a program, but in java 1st code will be converted to byte-code by Java compiler and from .java to .class (.class is nothing but byte code) file will be created. Once .class file created, Interpreter will convert this .class file to machine language, and machine language will be executed by microprocessor, and provides the result back.
As this .class file is portable, so we can take this .class file to any other machine. If we will run this, we will get same answer.

Q3. What is garbage collection is Java? Can we explicitly call garbage collection?
Garbage collection is a process which is already implemented in java. Here if an object or variable is not being used, then It will be automatically removed by Garbage collector of JVM.
Yes we can explicitly call garbage collector by using System.gc()
Note: Garbage collection in java performed on the basis of many algorithms but most frequent one is Mark and Sweep.    

Q4. What is JIT (Just-In-Time) complier?
JIT (Just-in-Time) compiler is a program that converts byte code into instructions that can be sent directly to the processor. In another words, JIT compiler is a program which convert java byte code in a format that can be easily processed by processor.

Q5. In JVM which is use to convert byte code into Machine language Interpreter or JIT compiler?
In general either Interpreter or JIT compiler used to convert byte code into machine code, but in Java both are being used. For more clarification we will see below instructions:
Assume below code is a byte code instruction.
                                   Print Rajnish
                                   Print Rajnish Again
                                   For i=1 to i=10
                                          Print Rajnish Once Again
Now first interpreter will execute 1st instruction and Print Rajnish will be converted to machine code,( to convert this suppose Interpreter took 2 nanoseconds) and this will be given to microprocessor.
Again Interpreter comes back in memory and reads 2nd instruction Print Rajnish Again, It will again take 2 nanoseconds to convert 2nd instruction into machine code, and then 2nd instruction will be given to microprocessor to execute.
Again Interpreter comes back and reads 3rd instruction which is a loop, so it will be repeated 10 times.
Interpreter will read Print Rajnish Once Again and will convert it again into machine code in 2 nanoseconds. Total 10 repetitions are there so 10*2=20 nanoseconds for 3rd Instruction which is not time efficient. So JIT comes into picture.
JIT will convert Print Rajnish Once Again into machine code in 2 nanoseconds and will place it in memory. Let’s say placing the result into memory will take 2 nanoseconds so total 4 nanoseconds and now processor will take the result from memory and execute it 10 times.
Here we saw how interpreter taking 20 nanoseconds for a task where JIT taking only 4 nanoseconds for the same task.
Note that 1st and 2nd instruction won’t be assigned to JIT because again it will take 4 nanoseconds for each task (converting bytecode to machine code 2 nanoseconds and placing result in memory 2 nanoseconds) where Interpreter will take only 2 nanoseconds for each tasks.

So for above example we can easily see how Interpreter and JIT is being used simultaneously in JVM to speed up the execution.




No comments:

Post a Comment