--JVM is an abbreviation for Java Virtual Machine, which is software required to run Java programs. --A subject that can execute Java bite code.
It can be executed regardless of the type of CPU or OS. In other words, in the process running on the OS, the bytecode obtained by compiling the Java code is converted into a machine language that the corresponding OS (Windows, OS X, Linux, etc.) can understand (analyze) and executed.
If you look at it broadly, there are the following four.
Class Loader
In Java, when you create source code, a .java file like Test.java is generated. When the Java compiler compiles the java source, a .class file (bytecode) like Test.class is generated. The Class Loader plays the role of assembling the ** Class file generated in this way and loading the ** Runtime Data Area **, which is the memory area given by the OS.
Execution Engine
--The Execution Engine converts the class (bytecode) loaded by the Class Loader into machine language and executes it in units of instruction words. --There are two ways to use an interpreter expression that executes the instruction words one by one and a JIT (Just-In-Time) compiler.
Garbage Collector
--Garbage Collector (GC) searches for and deletes unreferenced objects among the objects created (loaded) in the Heap memory area. --I don't know exactly when the GC will take place. (Because we cannot guarantee that it will be released immediately after the reference disappears) --While GC is performed, threads other than the thread that performs GC will stop.
Runtime Data Area
--This area is used to load data used when executing Java applications in the memory area of the JVM. --Divided into Method Area, Heap Area, Stack Area, PC Register, Native Method Stack. (There are other representatives.)
Method area
--The method area is an area where class information is managed for each class used in the program. --Field information and method names such as class member variable names, data types, access controlr information, return types, parameters, Type information (Interface or class), Constant Pool, static variables, final class variables, etc. Is managed.
Heap area --The heap area is the area that manages the instance and the member fields of the instance. --Area to manage objects and arrays created with the new keyword.
--Only the class loaded in the method area can be generated, and the Garbage Collector scans and releases the memory that is not referenced.
Stack area --This is the area for each Thread. --Area for managing local variables, parameters, return values, arbitrary values used in operations, etc. --The stack area is not a shared resource, so it is thread-safe. --The stack frame is divided into three sub-entities. --Stores the number of local variables associated with the Local Variable Array method and their corresponding values. --Operand stack If an intermediate operation is required to execute, the operand stack functions as a runtime workspace (workspace) to execute the operation. --All symbols corresponding to the Frame Data method are stored here. In the case of exceptions, the catch block information is maintained in the frame data.
PC Register
--Holds a Program Counter, a pointer to the current statement being executed in a thread, in the area created each time a Thread is created. (* Different from CPU registers) --If the currently executing method is'native', the value of the program counter register will be undefined.
Native method stack
--A memory area for native code written in programming languages other than Java. --Usually a stack for using code such as C / C ++. (JNI)
All threads share the method area and heap area The stack area, PC registers, and native method stack are created for each thread and are not shared.
Heap area & Garbage Collector In this item, the Heap area is an important target of GC, so let's take a closer look. (The Stack area and Method area are also subject to GC.)
The Heap area consists of 5 areas (eden, survivor1, survivor2, old, permanent).
Until JDK7, the permanent area existed in the heap. From JDK8, the permanent area has disappeared and a part of it has been changed to the "meta space area". (The figure above is based on JDK7.) The meta space area is now included in the Native stack area.
(It is important that the numbers in the survivor area are meaningless and divided into two.)
The reason why the Heap area is divided into 5 is to make GC perform efficiently. I would like to explain the details while looking at the process in which GC is performed.
Checks unreferenced objects in the Old area and deletes all applicable objects. In that case, it will be deleted in the Heap memory area and free memory space will be created, but reconfiguration is performed to eliminate this free memory. (Memory organization) ** So all threads are stopped to prevent other threads from using the memory while reconfiguring the memory. ** **
Recommended Posts