NOTE: The behaviors in this article are for HotSpot, 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 controls the execution of every Java program. It enables features such as automated exception handling, Garbage-collected heap.
JVM Architecture
- Class Loader: Class loader loads the Class for execution.
- Method Area: Stores pre-class structure as a constant pool.
- Heap: Heap is in which objects are allocated.
- Java Stack: Local variables and partial results are stored here. Each thread has a private JVM stack created when the thread is created.
- Program Counter Register: The program register holds the address of the JVM instruction currently being executed.
- Native Method Stack: It contains all native used in the application.
- Executive Engine: The execution engine controls the execution 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 a broad level, JVM Heap memory is physically divided into two parts – Young Generation and Old Generation.
Young Generation
The young generation is the place where all the new objects are created. When the 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 moves them to the other survivor space. So at a time, one of the survivor spaces is always empty.
- Objects that 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 the Old generation.
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 a longer time.
Permanent Generation
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.
Garbage Collector
In this section, I’ll introduce you to 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 unused objects from the memory and free space to be allocated to objects created in future processing. One of the best features 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 a program running in the background that looks into all the objects in the memory and finds 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 are 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.
The Disqus comment system is loading ...
If the message does not appear, please check your Disqus configuration.