What is JVM (Java Virtual Machine)?

What is JVM (Java Virtual Machine)?

--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.

JVM for various OS

Untitled.png

JVM configuration

If you look at it broadly, there are the following four.

JVM configuration

Untitled (1).png

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.)

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 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)

When the thread is created

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.

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 will be created in the Eden area.
  2. The first GC is done when the Eden area is full of objects.
  3. The memory of the Eden area is copied to the survivor1 area as it is. Then delete the objects in the other areas except the survivor1 area.
  4. When both the Eden area and the survivor1 area are full, search for the objects generated in the Eden area and the objects generated in the survivor1 area that are referenced.
  5. Leave the unreferenced objects as they are and copy only the referenced objects to the survivor2 area.
  6. Delete the objects in the other areas except the survivor2 area.
  7. Objects referenced more than a certain number of times in the above flow are moved from survivor2 to the Old area.

* 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 unreferenced objects and delete them all at once.

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

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. ** **

reference

Recommended Posts

What is JVM (Java Virtual Machine)?
What is java
What is Java <>?
What is Java
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
What is Java Assertion? Summary.
What is a Java collection?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java basics] What is Class?
What is java escape analysis?
What is thread safe (with Java)
[Java] What is Concurrent Modification Exception?
What is a lambda expression (Java)
What is a class in Java language (3 /?)
What is the best file reading (Java)
What is a class in Java language (1 /?)
What is Java and Development Environment (MAC)
What is a class in Java language (2 /?)
What is the main method in Java?
What is Cubby
[Java] JVM configuration
What is Docker?
What is null? ]
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Gradle?
What is POJO
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is the Java Servlet / JSP MVC model?
What is the volatile modifier for Java variables?
java state machine car
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?