How JVM Works - JVM Architecture?: Placements Practice Gate Cs Ide Q&A Geeksquiz
How JVM Works - JVM Architecture?: Placements Practice Gate Cs Ide Q&A Geeksquiz
How JVM Works - JVM Architecture?: Placements Practice Gate Cs Ide Q&A Geeksquiz
- GeeksforGeeks
1 of 6
Placements
GeeksQuiz
Practice
http://www.geeksforgeeks.org/jvm-works-jvm-architecture/
GATE CS
IDE
Q&A
Login/Register
JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM is the one that actually calls
the main method present in a java code. JVM is a part of JRE(Java Run Environment).
Java applications are called WORA (Write Once Run Everywhere). This means a programmer can develop Java
code on one system and can expect it to run on any other Java enabled system without any adjustment. This is
all possible because of JVM.
When we compile a .java file, a .class file(contains byte-code) with the same filename is generated by the Java
compiler. This .class file goes into various steps when we run it. These steps together describe the whole JVM.
Initialization
11/13/2016 11:32 PM
2 of 6
http://www.geeksforgeeks.org/jvm-works-jvm-architecture/
Loading : The Class loader reads the .class file, generate the corresponding binary data and save it in method
area. For each .class file, JVM stores following information in method area.
Fully qualified name of the loaded class and its immediate parent class.
Whether .class file is related to Class or Interface or Enum
Modifier, Variables and Method information etc.
After loading .class file, JVM creates an object of type Class to represent this file in the heap memory. Please
note that this object is of type Class predefined in java.lang package. This Class object can be used by the programmer for getting class level information like name of class, parent name, methods and variable information
etc. To get this object reference we can use getClass() method of Object class.
// A Java program to demonstrate working of a Class type
// object created by JVM to represent .class file in
// memory.
import java.lang.reflect.Field;
import java.lang.reflect.Method;
// Java code to demonstrate use of Class object
// created by JVM
public class Test
{
public static void main(String[] args)
{
Student s1 = new Student();
// Getting hold of Class object created
// by JVM.
Class c1 = s1.getClass();
// Printing type of object using c1.
System.out.println(c1.getName());
Run on IDE
Output:
11/13/2016 11:32 PM
3 of 6
http://www.geeksforgeeks.org/jvm-works-jvm-architecture/
Student
getName
setName
getRoll_no
setRoll_no
name
roll_No
Note : For every loaded .class file, only one object of Class is created.
Student s2 = new Student();
Class c2 = s2.getClass();
System.out.println(c1==c2); // true
Resolution : It is the process of replacing symbolic references from the type with direct references. It is
done by searching into method area to locate the referenced entity.
Initialization : In this phase, all static variables are assigned with their values defined in the code and static
block(if any). This is executed executed from top to bottom in a class and from parent to child in class hierarchy.
In general there are three class loaders :
Bootstrap class loader : Every JVM implementation must have a bootstrap class loader, capable of loading
trusted classes. It loads core java API classes present in JAVA_HOME/jre/lib directory. This path is popularly known as bootstrap path. It is implemented in native languages like C, C++.
Extension class loader : It is child of bootstrap class loader. It loads the classes present in the extensions
System/Application class loader : It is child of extension class loader. It is responsible to load classes from
application class path. It internally uses Environment Variable which mapped to java.class.path. It is also
implemented in Java by the sun.misc.Launcher$ExtClassLoader class.
// Java code to demonstrate Class Loader subsystem
public class Test
{
public static void main(String[] args)
{
// String class is loaded by bootstrap loader, and
// bootstrap loader is not Java object, hence null
System.out.println(String.class.getClassLoader());
Run on IDE
11/13/2016 11:32 PM
4 of 6
http://www.geeksforgeeks.org/jvm-works-jvm-architecture/
Output:
null
sun.misc.Launcher$AppClassLoader@73d16e93
Note : JVM follow Delegation-Hierarchy principle to load classes. System class loader delegate load request to
extension class loader and extension class loader delegate request to boot-strap class loader. If class found in
boot-strap path, class is loaded otherwise request again transfers to extension class loader and then to system
class loader. At last if system class loader fails to load class, then we get run-time exception java.lang.ClassNotFoundException.
JVM Memory
Method area :In method area, all class level information like class name, immediate parent class name, methods and variables information etc. are stored, including static variables. There is only one method area per JVM,
and it is a shared resource.
Heap area :Information of all objects is stored in heap area. There is also one Heap Area per JVM. It is also a
shared resource.
Stack area :For every thread, JVM create one run-time stack which is stored here. Every block of this stack is
called activation record/stack frame which store methods calls. All local variables of that method are stored in
their corresponding frame. After a thread terminate, its run-time stack will be destroyed by JVM. It is not a
shared resource.
PC Registers :Store address of current execution instruction of a thread. Obviously each thread has separate
PC Registers.
Native method stacks :For every thread, separate native stack is created. It stores native method information.
11/13/2016 11:32 PM
5 of 6
http://www.geeksforgeeks.org/jvm-works-jvm-architecture/
Just-In-Time Compiler(JIT) : It is used to increase efficiency of interpreter.It compiles the entire bytecode
and changes it to native code so whenever interpreter see repeated method calls,JIT provide direct native
code for that part so re-interpretation is not required,thus efficiency is improved.
Garbage Collector : It destroy un-referenced objects.For more on Garbage Collector,refer Garbage Collector.
It is a collection of the Native Libraries(C, C++) which are required by the Execution Engine.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
11/13/2016 11:32 PM
6 of 6
http://www.geeksforgeeks.org/jvm-works-jvm-architecture/
Related Posts:
HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
throw and throws in Java
Data types in Java
3.1
Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.
@geeksforgeeks, Some rights reserved
Contact Us!
About Us!
11/13/2016 11:32 PM