Brief on JVM Heap Memory
let’s take a look at how the JVM organizes memory into different areas. This has been helping several garbage collection algorithms and memory management to work efficiently. These areas are called Generations which are categorized based on the life of the objects.
1. There are separate pools that hold objects of different age. They are
a. Young Generation
b. Old Generation
c. Permanent Generation
2. Young Generation:
a. Most of the objects are initially placed in the young generation space.
b. Young Generation Garbage collection occurs frequently and operates at high speed since the young generation space contains lots of objects that are less referenced.
c. In Java HotSpot VM, Young Generation again categorized into
i. Eden Space : Most objects are initially allocated in the Eden space
ii. Survivor Space: Objects that survive atleast one young garbage collection is moved to survivor space.
3. Old Generation:
a. Objects that survived Garbage Collection (From Survivor Space) are tenured or Promoted to the Old Generation
b. Garbage Collection algorithm that works on this space is designed to be more space efficient since Old generation occupies most of the heap.
4. Permanent Generation (PermGen) :
a. Permanent Generation usually contains the objects that describe classes & methods and also classes & methods.
let’s take a look at how the JVM organizes memory into different areas. This has been helping several garbage collection algorithms and memory management to work efficiently. These areas are called Generations which are categorized based on the life of the objects.
1. There are separate pools that hold objects of different age. They are
a. Young Generation
b. Old Generation
c. Permanent Generation
2. Young Generation:
a. Most of the objects are initially placed in the young generation space.
b. Young Generation Garbage collection occurs frequently and operates at high speed since the young generation space contains lots of objects that are less referenced.
c. In Java HotSpot VM, Young Generation again categorized into
i. Eden Space : Most objects are initially allocated in the Eden space
ii. Survivor Space: Objects that survive atleast one young garbage collection is moved to survivor space.
3. Old Generation:
a. Objects that survived Garbage Collection (From Survivor Space) are tenured or Promoted to the Old Generation
b. Garbage Collection algorithm that works on this space is designed to be more space efficient since Old generation occupies most of the heap.
4. Permanent Generation (PermGen) :
a. Permanent Generation usually contains the objects that describe classes & methods and also classes & methods.
HelloWorld In JVM’s View – How Java Program executed internally in JVM
Every java developer come across this “Hello World” that kick start us in objectville trip. But while startup, we are more concentrating in understanding language rather than thinking how the internal works. It may be true in most case. So i thought to show Hello world in Jvm’s view.
package com.wordpress.kkarthikeyanblog;
public class HelloWorld {
public static String HELLOWORLD = "Hello World";
public void print() {
System.out.println(HELLOWORLD);
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.print();
}
}
After compiled the above code using javac. I am running the HelloWorld using the following command. Now JVM triggered.
java com/wordpress/kkarthikeyanblog/HelloWorld
JVM Explanation :
hey Guys, its me JVM .. If you dont know about me, just get a heads up in link
I am going to explain, how i(jvm) handle the class file and execute it.
As i start, BoostrapperClassLoader load the essential java.lang package and “System Class Loader” inside me find the class “Hello world” by searching the classpath given. After locating the binary class file(HelloWorld.class) , it will transfer this binary stream to Me(JVM). I(JVM) extracts the following information from the binary stream of HelloWorld.class
After loading that information, i as jvm(thread) will try to find out “public static void main(String [] args)” method.
As you know, every thread in me(JVM), apart from shared “Method area” and “Heap Space“, there will be individual “stack” and “pc register”. That will help every thread of me in execution further.
i will push that main() method in stack as one stack frame by getting those info from Method Area’s method bytecode. Program Counter(pc register) will tell me(jvm thread) which line to execute next.
so as PC instructing me, i(jvm thread) start with the line
HelloWorld helloWorld = new HelloWorld();
I will get the symbolic reference(fully qualified name) of HelloWorld from constant pool and i do the lookup in method area.From Method area ,i get the class info and create the object with default initial value in “Heap space”. After creating the object, i will feed the object reference to stack method local variable.
Now PC will point to
helloWorld.print();
i(jvm) will take the local variable “helloWorld” reference in my stack and find the method print(). After getting byte code info from Method Area, i will push method “print()” in the stack as another stack frame, now i will start executing print() method.
Once the print() method is executed as before, that method will be pop up and it continue executing main() method. Once main() method ends , that method also pops up from the stack and I am done with my execution.
In Summarizing the above, In JVM, we have
Method area – to store the class metadata [as i mentioned before]
Heap Space – to hold only the objects
Specific to each Thread :
Stack – consists of stack frames[i.e Methods] – it will also hold the local variables specific to the method
Program Counter Registers – To guide what to be executed next
Apart from these, we have “Garage Collector” to free unreferenced objects.
Every java developer come across this “Hello World” that kick start us in objectville trip. But while startup, we are more concentrating in understanding language rather than thinking how the internal works. It may be true in most case. So i thought to show Hello world in Jvm’s view.
package com.wordpress.kkarthikeyanblog;
public class HelloWorld {
public static String HELLOWORLD = "Hello World";
public void print() {
System.out.println(HELLOWORLD);
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.print();
}
}
After compiled the above code using javac. I am running the HelloWorld using the following command. Now JVM triggered.
java com/wordpress/kkarthikeyanblog/HelloWorld
JVM Explanation :
hey Guys, its me JVM .. If you dont know about me, just get a heads up in link
I am going to explain, how i(jvm) handle the class file and execute it.
As i start, BoostrapperClassLoader load the essential java.lang package and “System Class Loader” inside me find the class “Hello world” by searching the classpath given. After locating the binary class file(HelloWorld.class) , it will transfer this binary stream to Me(JVM). I(JVM) extracts the following information from the binary stream of HelloWorld.class
- constants( like literals, constants, symbolic references to types, fields, and methods) will be stored in constant pool [in this case, symbolic reference to HelloWorld class and the fields, methods, constants associated with it]
- package,modifiers,static variables [in this case "HELLOWORLD" attribute],
- field information(name,type,modifiers) -
- methods information(name,return type,method parameters,modifiers, method’s bytecode) – in this case , its [print, void, public and byte code]
- reference to ClassLoader [ which classloader loads this class ]
- reference to class Class
After loading that information, i as jvm(thread) will try to find out “public static void main(String [] args)” method.
As you know, every thread in me(JVM), apart from shared “Method area” and “Heap Space“, there will be individual “stack” and “pc register”. That will help every thread of me in execution further.
i will push that main() method in stack as one stack frame by getting those info from Method Area’s method bytecode. Program Counter(pc register) will tell me(jvm thread) which line to execute next.
so as PC instructing me, i(jvm thread) start with the line
HelloWorld helloWorld = new HelloWorld();
I will get the symbolic reference(fully qualified name) of HelloWorld from constant pool and i do the lookup in method area.From Method area ,i get the class info and create the object with default initial value in “Heap space”. After creating the object, i will feed the object reference to stack method local variable.
Now PC will point to
helloWorld.print();
i(jvm) will take the local variable “helloWorld” reference in my stack and find the method print(). After getting byte code info from Method Area, i will push method “print()” in the stack as another stack frame, now i will start executing print() method.
Once the print() method is executed as before, that method will be pop up and it continue executing main() method. Once main() method ends , that method also pops up from the stack and I am done with my execution.
In Summarizing the above, In JVM, we have
Method area – to store the class metadata [as i mentioned before]
Heap Space – to hold only the objects
Specific to each Thread :
Stack – consists of stack frames[i.e Methods] – it will also hold the local variables specific to the method
Program Counter Registers – To guide what to be executed next
Apart from these, we have “Garage Collector” to free unreferenced objects.