JVM is Java Virtual Machine which converts java bytecode (.class file) to native code.
Below figure shows that when the actual working of JVM starts:
Fig.: Life cycle of Class |
JVM mainly consist of 3 parts:-
1. Class Loader
Subsystem
2. Memory areas
3. Execution engine.
1. Class Loader Subsystem:
- When we compile java file, after successful compilation the java file convert into ".class" file. So this class file is fed to Class Loader Subsystem of JVM.
- Class Loader Subsystem responsible for loading class file (A.class file)
There are 3 class loaders as below:
i. Bootstrap Class Loader (BCL)
ii. Extension Class Loader (ECL)
iii. Application Class Loader (ACL)
I. BCL will load the class from class path “JDK/jre/lib/rt.jar” eg. String, Object,
Driver etc classes.
II. All the API classes take care by BCL.
III. Priority of class loading is as below:
First check in Bootstrap Class Loader, if not found in BCL
then find in ECL and if not found in ECL then it check in ACL.
Priority of loading: 1) BCL > 2) ECL > 3) ACL
IV. ECL loads classes from class path “JDK/jre/lib/ext” if BCL not able to found class.
V. ACL loads class from application level/ environment variable
class path if ECL not able to found class.
VI. Finally class not found in ACL then throws an exception as
ClassNotFoundException (Exception concept explained in another section).
2. Memory Areas in JVM:
It mainly consist of 5 parts:
I. Method area
II. Heap
III. Stack
IV. Program Counter
V. Native method area
I. Method area:
- It consist all class level and static variables.
- After successful loading of class, read and store binary data to next part ie. Method area.
- After that JVM will create object of “Class” class to represent corresponding class level information.
II. Heap:
- All objects, instance variables and Array stored in Heap.
- It also consist of String constant pool memory to store String objects.
- This is not a thread safe (Thread concept explained in Multithreading section).
- Create an object of java.lang.Class class to represent any class information (eg. class Student, class Employee etc)
III. Stack:
- Reference variables and local variables stores in stack.
- For every thread one stack created at runtime.
IV. Program Counter
Register (PCR)
- Function of PCR is to hold address of currently executing instruction.
- For every thread, separate PCR created.
V. Native method area
- Hold an information of Native methods (Native method is a Java method whose implementation is written in another/old programming language such as C or C++.)
3. Execution engine:
It consist of
I. Interpreter:
- Execute bytecode line by line.
- If we are calling same method multiple times then every time interpreter is required.
II. JIT compiler:
- It reduces the amount of time required for compilation.
- If we are facing an issue with interpreter i.e. calling one method multiple times then interpreter required multiple times.
- So JIT will create native code or machine language code which will call that method without interpreter. So in this way performance will get improved.
III. JNI:
- Provide an information about native methods. Use of native method is that it can create Java objects.
- Then inspect and use these objects to perform specific tasks.
Different between Compiler and Interpreter:
Compiler
|
Interpreter
|
|
1
|
Read whole
program at a time
|
Read
program line by line
|
2
|
Execution is faster
|
Execution is slower
|
3
|
More memory
required
|
Less memory
required.
|
Memory areas:
Name
|
Memory area
|
|
1
|
Static variables
|
Method area
|
2
|
Methods
|
Method area
|
3
|
Non- static/ instance variable
|
Heap
|
4
|
Arrays
|
Heap
|
5
|
Objects
|
Heap
|
6
|
Native methods
|
Native Method area
|
7
|
Local variables
|
Stack
|
8
|
Reference variables
|
Stack
|
Please Note:
All outstanding interview
questions and maximum programs on this topic will be covered in another
section.
No comments:
Post a Comment
**** Please let me know if you have any doubts. ****