JDK / JRE / JVM / Architecture
What is JDK / JRE / JVM ?
JDK – Java Development kid is a software environment which provides both developing and running Java applications.
JRE – Java Runtime Environment is software environment which helps to only run the Java Applications.
JVM – Java Virtual Machine is responsible for executing the program line by line.
JDK = JRE + Development Tools
JRE = JVM + Library classes
What is a Development Tool ?
This is an environment where we can develop our Java programs.
Development tools are external tools like IDE (eg ,Eclipse, Netbeans, VIM editor, Notepad etc)
So Development tools is used to develop Java program and running the same program in Java Runtime Environment.
How Java programs runs in JRE Environment ?
JRE is the combined bundle of JVM and Library classes.
What is Library classes in Java?
Java Class Libraries are basically written in Java, will be dynamically called during runtime of the program.
Since Java is platform independent, these libraries will not present in System Software Operating system , Its is a pre-created standard libraries present in Java itself.
So JVM uses Java Class libraries for executing the Java program line by line.
What is inside JVM and how it works ?
JVM comprises of three section, listed below
1. Class loading Section
2. Run time data Section
3. Execution Engine
Section 1 : Class loading Section
This process of this section is divided in three parts.
Part 1 : Loading
This loads the .class byte-code to the memory
1.Bootstrap jar loader
Loading java’s Internal classes usually bundled in rt.jar which is distributed by JVM.
2.Extension class loader
Loading additional application jars which present in jre/lib/ext folder.
3.Application class loader
Loading classes based on CLASSPATH which is our own class files.
Part 2 : Link
It involves 3 process, listed below
1.Verify
This process looks the byte-code which is loaded by class loader and check the compatibility of the byte-code with the Software installed in the environment.
It also checks the byte-code is valid or not ( validity checks performed ).
2.Prepare
In this process the memory is allocated for the class variables.
Note : Only for class variables the memory is allocated and not for the instance variables.
3.Resolve
In this process all the symbolic references are resolved.
Here all the symbolic references are changed to actual reference inside the JVM.
Part 3 : Initialise
In this process all static variables are initialised.
Memory allocation for the static variables defined in the program is done and the variables are initialised.
Section 2 : Run time data area
This concept is explained 5 parts.
Part 1 : Method area
All class level data including static variables are stored in method area.
Only one method area will be present in JVM.
This is called the -XX:MaxPermSize or PermGenSpace by default 64MB is allocated for method area.
Part 2 : Heap area
All the Objects and their corresponding instance variables and arrays will be stored here.
There is also mentioned as one Heap Area per JVM. Since the Method and Heap areas share memory for multiple threads, the data stored is not thread safe.
This heap area is defined as -Xms , -Xmx
Part 3 : PC Registers
Contains Program counter which is the pointer to the next instruction to be executed per thread.
According to the number of threads running the program, the program counter holds the information about the thread which is executed.
It also holds the information about which thread to be to be executed next in the program chain.
Part 4 : Stack area
So when a threads started running, an runtime-stack is created first.
For every method call an entry is made in Stack memory.
All local variables will be created in stack memory.
1. Local Variable
This is the related to the method that how local variables are stored in stack memory.
2. Operand Stack
if in case any intermediate operation to be done, this operand stack acts as a runtime workspace to the operation.
3. Frame data
All symbols corresponding to the operation is stored in frame data.
All Exceptions block informations are stored in frame data.
Part 5 : Native Method
If any stack is opt to call any Native methods in java, those informations are stored in Native method stack.
Section 3 : Execution Engine
Once the Runtime data process is over and the program is ready for execution, then the program is moved to the Execution engine.
And this Execution engine reads the byte-code piece by piece, and it contains 2 process.
Process 1 : Interpreter
This interprets the byte-code inside the container.
Disadvantage is it will interpret the byte-code for class methods even though the interpreter is called multiple times.
Process 2 : JIT Just in time compiler
If repeated method codes are called multiple time, JIT converts the byte-code and changes to native code.
So if the same method code is called many times the native code is taken from the already generated code.
Hence reinterpreting the byte-code to native code is not repeated.
Performance is increased by calling the native codes for the repeated method calls.
Process Inside JIT
1. Intermediate Code generator
Produces intermediate code
2. Code Optimiser
Responsible for optimising the intermediate code generated above
3. Target Code Generator
Responsible for Generating Machine Code or Native Code
4. Profiler
A special component, responsible for finding hotspots, i.e. whether the method is called multiple times or not.
4 : Garbage Collector
What is Garbage collection in java ?
Its the process of collecting and removing the unused Object space used in memory, there by increasing the performance of execution.