NOTE: We look at HotSpot in this article, other JVM might behave differently.
What is JVM?
The Java Virtual Machine is the cornerstone of the Java platform. It is the component of the technology responsible for its hardware- and operating system-independence, the small size of its compiled code, and its ability to protect users from malicious programs.
The Java Virtual Machine is an abstract computing machine. Like a real computing machine, it has an instruction set and manipulates various memory areas at run time. The JVM doesn't understand Java typo, that's why you compile your *.java files to obtain *.class files that contain the bytecodes understandable by the JVM.
JVM control execution of every Java program. It enables features such as automated exception handling, Garbage-collected heap.
- Class Loader : Class loader loads the Class for execution.
- Method Area : Stores pre-class structure as constant pool.
- Heap : Heap is in which objects are allocated.
- Java Stack : Local variables and partial results are store here. Each thread has a private JVM stack created when the thread is created.
- Program Counter Register : Program register holds the address of JVM instruction currently being executed.
- Native Method Stack : It contains all native used in application.
- Executive Engine : Execution engine controls the execute of instructions contained in the methods of the classes.
- Native Interface : Native method interface gives an interface between java code and native code during execution.
- Native Libraries : Native Libraries consist of files required for the execution of native code.
JVM Memory Model
In this section, I'll introduce you Java memory model. Understanding JVM Memory Model is very important if you want to understand the working of Java Garbage Collection. Today we will look into different parts of JVM memory and how to monitor and perform garbage collection tuning.
As the figure shows, the memory of Java can be divided into three parts: Young Generation(YG), Old Generation(OG) and Permanent Generation(PG). At broad level, JVM Heap memory is physically divided into two parts – Young Generation and Old Generation.
Young generation is the place where all the new objects are created. When young generation is filled, garbage collection is performed. This garbage collection is called Minor GC. Young Generation is divided into three parts –Eden Memory and two Survivor Memory spaces.
Important Points about Young Generation Spaces:
- Most of the newly created objects are located in the Eden memory space.
- When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to one of the survivor spaces.
- Minor GC also checks the survivor objects and move them to the other survivor space. So at a time, one of the survivor space is always empty.
- Objects that are survived after many cycles of GC, are moved to the Old generation memory space. Usually it’s done by setting a threshold for the age of the young generation objects before they become eligible to promote to Old generation.
Old Generation memory contains the objects that are long lived and survived after many rounds of Minor GC. Usually garbage collection is performed in Old Generation memory when it’s full. Old Generation Garbage Collection is called Major GC and usually takes longer time.
Permanent Generation or “Perm Gen” contains the application metadata required by the JVM to describe the classes and methods used in the application. Note that Perm Gen is not part of Java Heap memory.
Perm Gen is populated by JVM at runtime based on the classes used by the application. Perm Gen also contains Java SE library classes and methods. Perm Gen objects are garbage collected in a full garbage collection.
In this section, I'll introduce you the garbage collector in Java briefly. Maybe in the next month, I'll explain it in more detail in another article.
Java Garbage Collection is the process to identify and remove the unused objects from the memory and free space to be allocated to objects created in the future processing. One of the best feature of java programming language is the automatic garbage collection, unlike other programming languages such as C where memory allocation and deallocation is a manual process.
Garbage Collector is the program running in the background that looks into all the objects in the memory and find out objects that are not referenced by any part of the program. All these unreferenced objects are deleted and space is reclaimed for allocation to other objects.
The above shortcomings with the simple approach is the reason that Java Garbage Collection is Generational and we have Young Generation and Old Generation spaces in the heap memory. I have already explained above how objects are scanned and moved from one generational space to another based on the Minor GC and Major GC.
(To be continued...)