Top 30 JVM (Java Virtual Machine) Interview Questions and Answers Question 1. What Is JVM in Java?
Top 30 JVM (Java Virtual Machine) Interview Questions and Answers Question 1. What Is JVM in Java?
Top 30 JVM (Java Virtual Machine) Interview Questions and Answers Question 1. What Is JVM in Java?
JVM (Java Virtual Machine) consists of Class Loader Subsystem, Runtime Data
Areas and Execution Engine. Let’s discuss each of them in quickly.
Question 3. What is Class Loader Subsystem of JVM? What is its functioning and
purpose?
Answer. It is important jvm interview question for experienced developers.
2) Heap >
Heap is place where all objects are stored in JVM (java virtual machine).
Heap even contains arrays because arrays are objects.
How stack frames are created when thread calls new method?
As we know each and every thread has its own stack. Whenever new method is
called new stack frame is created and it is pushed on top of that thread's stack.
What does thread stack contains?
The stack contain
All the local variables,
All the parameters,
All the return address.
Does stack stores/contains object OR what stack doesn’t contains?
Stack never stores object, but it stores object reference.
Interpreter > Interpreter is responsible for reading the bytecode and then executing
the instructions.
Question 7. What is JIT Compiler (Just In Time Compiler)? Explain in detail with
example.
Answer. Most the developers know about JIT in short. So, it's become very difficult
and challenging to answer this jvm interview question. 5 years+ experienced
developers must prepare it well.
JIT compiles bytecodes to machine code at run time and improves the performance
of Java applications.
starts up, lots of methods are called. Compiling all of these methods might can affect
startup time significantly, though program ultimately may achieve good performance.
Methods are not compiled when they are called first time. For each and every
method JVM maintains a call count, which is incremented every time the method is
called. The methods are interpreted by JVM until call count not exceeds JIT
helps the JVM to start quickly. The threshold has been selected carefully by java
Therefore, very frequently used methods are compiled as soon as JVM has started,
After a method is compiled, its call count is reset to zero and subsequent calls to the
method increment it call count. When the call count of a method reaches a JIT
recompilation threshold, the JIT compiler compiles method second time, applying
This process is repeated until the maximum optimization level is reached. Most
frequently used methods are always optimized to maximize the performance benefits
Example -
method increment it call count. When the call count of a method reaches a 2 (i.e. JIT
recompilation threshold), the JIT compiler compiles method second time, applying
The JIT compiler can also be disabled, if disabled the entire Java program will be
interpreted. Disabling the JIT compiler is not recommended. We must disable JIT to
For more details : What is JIT Compiler (Just In Time Compiler) in JVM
Read : JVM (java virtual machine) in detail in java and How Garbage Collection
(GC) works internally
Top 50 Garbage collection interview Questions and answers for experienced and
freshers
developing,
compiling and
JRE contains-
JVM,
java program.
In short JRE = JVM + class libraries (rt.jar) + other libraries (if any).
JDK is required for JRE provides environment JVM is the virtual machine
programs. executes.
running Java
JVM,
JDK contains-
class libraries and
JVM libraries.
In short In short
any).
For detail please read - JDK (Java Development Kit), JRE (Java Runtime
environment), JVM (java virtual machine), Differences between JDK, JRE and
JVM
Question 12. Mention some of the most important VM (JVM) PARAMETERS you
have used in JVM Heap memory?
Answer. It is very very important garbage collection interview question for fresher
and experienced developer.
-Xmx : Xmx is the maximum heap size that JVM can use.
Example of using -Xmx VM option in java >
java -Xmx512m MyJavaProgram
It will set the maximum heap size of JVM to 512 megabytes.
For more explanation -Xms and -Xmx JVM parameters and differences
For explanation and example - What are -XX:PermSize and -XX:MaxPermSize with
Differences
Question 13. Mention some of the most important VM (JVM) PARAMETERS you
have used for Young Generation in JVM Heap memory?
Answer. It is important garbage collection interview question for experienced
developers to specifically identify and answer jvm parameters for young generation.
Question 14. Mention some of the most important VM (JVM) PARAMETERS you
have used for Old Generation (tenured) in JVM Heap memory?
Answer. It is difficult garbage collection interview question for experienced
developers to specifically identify and answer jvm parameters for old generation.
Question 15. Mention some of the most important VM (JVM) PARAMETERS you
have used for Permanent Generation?
Answer. Another complex and challenging garbage collection interview question for
experienced developers to specifically identify and answer jvm parameters for
permanent generation.
-XX:MaxPermSize: It’s maximum value of Permanent Space that JVM can allot up
to.
Example of using -XX:MaxPermSize VM (JVM) option in java >
java -XX:MaxPermSize=512m MyJavaProgram
It will set maximum value of Permanent Space as 512 megabytes to JVM
For more details - What are -XX:PermSize and -XX:MaxPermSize with Differences
Read : JVM (java virtual machine) in detail in java and How Garbage Collection
(GC) works internally
Increase the heap size using -Xms and -Xmx jvm parameters as a solution to this
issue.
Must read: How to set, change, increase or decrease heap size in tomcat server
and eclipse to avoid OutOfMemoryError ?
Read : JVM (java virtual machine) in detail in java and How Garbage Collection
(GC) works internally
Top 50 Garbage collection interview Questions and answers for experienced and
freshers
Read : JVM (java virtual machine) in detail in java and How Garbage Collection
(GC) works internally
Now, let’s prepare few JVM related java programs for interview >
/**
* Example/program to Find Free Memory Available In
* JVM (Java virtual machine) In your System
*/
public class FreeMemoryAvailableInJavaVirtualMachineExample{
public static void main(String[] args) {
/**
* first we will get the java Runtime object using the
* Runtime class's getRuntime() method in java.
*/
Runtime runtime = Runtime.getRuntime();
/**
* freeMemory() method of Runtime class returns the total
* amount of free memory in the JVM (Java virtual machine).
*
* freeMemory() is the native method.
*
* Calling gc (garabage collector) may result in
* increasing free memory in java.
*/
long freeMemoryAvailableInJVM = runtime.freeMemory();
System.out.println("Total amount of free memory available in the "
+ "JVM (Java virtual machine) in bytes = "+ freeMemoryAvailableInJVM);
}
}
/* output
Total amount of free memory available in the JVM (Java virtual machine) in bytes
= 253398664
*/
/**
* Example/program to Find Maximum Memory That
* JVM (Java virtual machine) AttemptsToUseExample
*/
public class FindMaximumMemoryThatJVMAttemptsToUseExample{
public static void main(String[] args) {
/**
* first we will get the java Runtime object using the
* Runtime class's getRuntime() method in java.
*/
Runtime runtime = Runtime.getRuntime();
/**
* maxMemory() method of Runtime class returns maximum amount
* of memory that the JVM (Java virtual machine) will try to use
*
* maxMemory() is the native method.
*
*/
long maximumMemoryThatJVMAttemptsToUse = runtime.maxMemory();
System.out.println("Maximum Memory That "
+ "JVM (Java virtual machine) will try to use "
+ "in bytes = "+ maximumMemoryThatJVMAttemptsToUse);
}
}
/* output
Maximum Memory That JVM (Java virtual machine) will try to use in bytes =
3797417984
*/
/**
* Example/program to terminate
* JVM (Java virtual machine) using halt method
*/
public class TerminateJVMExampleUsingHaltMethod{
public static void main(String[] args) {
System.out.println("Halt method program - to terminate JVM (Java virtual
machine)");
/**
* first we will get the java Runtime object using the
* Runtime class's getRuntime() method in java.
*/
Runtime runtime = Runtime.getRuntime();
/**
* halt method of Runtime class
* terminates the running JVM (Java virtual machine) forcibly.
* halt method never returns normally.
*/
runtime.halt(1); //pass status as 1
System.out.println("JVM (Java virtual machine) halted"); //This line won't be
printed
}
}
/* output
Halt method program - to terminate JVM (Java virtual machine)
*/
1.2) Program to terminate JVM (Java virtual machine) using halt method And
/**
* Example/program to terminate
* JVM (Java virtual machine)
* Using Halt Method And Show registered
* ShutdownHooks Are Not Executed
*/
public class
TerminateJVMUsingHaltMethodAndShowShutdownHooksAreNotExecuted{
public static void main(String[] args) {
System.out.println("halt method program - to terminate JVM (Java virtual
machine)");
Runtime runtime = Runtime.getRuntime();
//Register/ add ShutdownHook, it won't get called after
//JVM is shutDown using halt method.
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run() {
System.out.println("Executing shutdown hook...");
System.out.println("shutdown hook executed successfully");
}
});
//call halt method
runtime.halt(1);
System.out.println("JVM (Java virtual machine) halted"); //This line won't be
printed
}
}
/* output
halt method program - to terminate JVM (Java virtual machine)
*/
2) Terminate JVM (Java virtual machine) using exit method in java >
/**
* Example/program to terminate
* JVM (Java virtual machine) using exit method
*/
public class TerminateJVMExampleUsingExitMethod{
public static void main(String[] args) {
System.out.println("Exit method program - to terminate JVM (Java virtual
machine)");
/**
* first we will get the java Runtime object using the
* Runtime class's getRuntime() method in java.
*/
Runtime runtime = Runtime.getRuntime();
/**
* exit method of Runtime class
* terminates the running JVM (Java virtual machine) by
* initiating the shutdown sequence.
*
*/
runtime.exit(1); //pass status as 1 to exit method
System.out.println("JVM (Java virtual machine) halted"); //This line won't be
printed
}
}
/* output
Exit method program - to terminate JVM (Java virtual machine)
*/
2.2) Program to terminate JVM (Java virtual machine) using exit method And
/**
* Example/program to terminate
* JVM (Java virtual machine)
* Using exit Method And Show registered
* ShutdownHooks Are Executed
*/
public class
TerminateJVMUsingExitMethodAndShowShutdownHooksAreExecuted{
public static void main(String[] args) {
System.out.println("Exit method program - to terminate JVM (Java virtual
machine)");
Runtime runtime = Runtime.getRuntime();
//Register/ add ShutdownHook, it will get called after
//JVM is shutDown using exit method.
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run() {
try{
System.out.println("Executing shutdown hook...");
}catch (Exception e){
e.printStackTrace();
}
System.out.println("shutdown hook executed successfully");
}
});
//call exit method
runtime.exit(1);
System.out.println("JVM (Java virtual machine) exited"); //This line won't be
printed
}
}
/* output
Exit method program - to terminate JVM (Java virtual machine)
Executing shutdown hook...
shutdown hook executed successfully
*/