[Java] JVM(Java Virtual Machine)とは?

5 minute read

What is JVM (Java Virtual Machine)

  • JVM is an abbreviation of Java Virtual Machine, which is software required to run Java programs.
  • An entity that can execute Java bite code.

It can be executed regardless of the type of CPU or OS. In other words, in a process that runs on the OS, bytecodes obtained by compiling Java code are converted into machine language that can be understood (parsed) by the OS (Windows, OS X, Linux, etc.) and executed.

JVM for various OS

Untitled.png

JVM configuration

There are the following four in a big look.

  • Class Loader
  • Execution Engine
  • Garbage Collector
  • Runtime Data Area

JVM configuration

Untitled (1).png

Class Loader

In Java, when you create the source code, a .java file is generated, like Test.java. When the Java source is compiled by the Java compiler, a .class file (byte code) is generated like Test.class. The Class Loader plays the role of loading the **Runtime Data Area, which is the memory area given by the OS by the JVM, by assembling the **Class file generated in this way.

Execution Engine

  • Execution Engine converts the class (byte code) loaded by Class Loader into machine language and executes it in units of command words.
  • There is a method of using an interpreter expression that executes instruction words one by one and a JIT (Just-In-Time) compiler.

  • The JIT compiler is a method to improve performance by changing the entire bytecode to native code at an appropriate time and executing the code that Execution Engine has natively compiled.

Garbage Collector

  • Garbage Collector (GC) searches and deletes unreferenced 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 is gone)
  • While GC is performed, all threads other than the GC thread are stopped.

*If Full GC is performed for a few seconds, if all threads are stopped, a fatal problem will occur that will lead to a failure.

Runtime Data Area

  • This area is used to load the data used when executing a Java application in the memory area of the JVM.
  • It is divided into Method Area, Heap Area, Stack Area, PC Register, Native Method Stack. (Typically there are others.)

Runtime Data Area structure

캡처.PNG

Method area

  • The method area is an area where class information is managed for each class used in the program. -Field name such as variable name of class member, data type, information of access controller and method name, return type, parameter, Type information (Interface or class), Constant Pool, static variable, final class variable etc. Is managed.

Heap area

  • The heap area is an area that manages the instance and instance member fields. -This area manages the objects and arrays created with the new keyword.

  • This is an area where only the class loaded in the method area can be generated and the memory that is not referenced by the Garbage Collector is scanned and released.

Stack area

  • This is the area for each thread.
  • An area that manages local variables, parameters, return values, and arbitrary values used in calculations.
  • The stack area is thread-safe because it is not a shared resource.
  • The stack frame is divided into 3 sub-entities. -Local Variable Array Contains the number of local variables associated with the method and the corresponding value. -Operand stack When an intermediate operation is required to execute, the operand stack functions as a runtime workspace (work area) to execute the operation. -All symbols corresponding to FrameData methods 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 executing in a thread, in the area created each time a Thread is created. (* Different from CPU register)
  • If the method currently being executed 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 C/C++ code. (JNI)

*JNI: JNI interacts with the native method library and provides the native library required for the execution engine.

When a thread is created

The method area and heap area are shared by all threads, The stack area, PC register, 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.)

Heap area consists of 5 areas (eden, survivor1, survivor2, old, permanent).

Until JDK7, the permanent area existed in heap. As of JDK8, the permanent area has been removed, and part of it has been changed to the “meta space area”. (The above figure 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 have no meaning and are divided into two.)

The purpose of dividing the Heap area into 5 is to perform efficient GC. I would like to explain the details while looking at the process of GC.

GC is divided into Minor GC and Major GC

*Minor GC: GC performed in the New area

  1. When the object is first created, it is created in the Eden area.
  2. When the Eden area is filled with objects, the first GC is performed.
  3. The memory of the Eden area is copied as is to the survivor1 area. Then, delete the objects in other areas except the survivor1 area.
  4. When both the Eden area and the survivor1 area are full, search for objects referenced in the objects created in the Eden area and the survivor1 area.
  5. Leave unreferenced objects as they are and copy only referenced objects to the survivor2 area.
  6. Delete objects in other areas except the survivor2 area.
  7. Move the objects that are referenced more than a certain number of times in the above flow from survivor2 to the old area.

*Repeat the above procedure to move to the old area before the survivor2 area is full.

*Major GC (Full GC): GC performed in the Old area

  1. Inspect all objects in the Old area to see if they are referenced.
  2. Collect the unreferenced objects and delete them all at once.

*It takes longer than Minor GC, and all threads other than GC are stopped while GC is running.

What if Major GC (Full GC) is performed? ?

Check the unreferenced objects in the Old area and delete all the corresponding objects. In that case, it will be deleted in the Heap memory area and free memory space will be created, but it will be reconfigured to eliminate this free memory. (Memory organization) ** So all threads are stopped to prevent other threads from using the memory while reconfiguring it. **

Reference

  • https://jeong-pro.tistory.com/m/148?category=793347
  • https://gakumon.tech/java/java_thread_jvm.html