What is JVM?

Description: Almost everyone who does java development knows the term jvm, but because jvm is not much related to actual simple development, it usually takes a year or two to work (of course, excluding those who love to learn and specialize in performance optimization) ), very few people can learn and understand what jvm is and how jvm works. Personally, I think this is still very necessary to understand and study carefully, especially for those who are just getting started or who are just getting started. For development, this is the cornerstone of java.

JVM (Java Virtual Machine, Java Virtual Machine)

The cross-platform feature of a Java program mainly means that the bytecode file can be run on any computer or electronic device with a Java virtual machine. The Java interpreter in the Java virtual machine is responsible for interpreting the bytecode file into a specific machine code for running. . Therefore, at runtime, the Java source program needs to be compiled into a .class file by the compiler. As we all know, java.exe is the execution program of the java class file, but in fact the java.exe program is just an execution shell, it will load jvm. , for: libjvm.so), this dynamic link library is where the actual operation of the java virtual machine is located.

JVM is part of JRE. It is a fictitious computer, which is realized by simulating various computer functions on an actual computer. JVM has its own complete hardware architecture, such as processor, stack, registers, etc., and also has a corresponding instruction system. The most important feature of the Java language is that it runs across platforms. The JVM is used to support operating system-independent, cross-platform. Therefore, the JAVA virtual machine JVM belongs to the JRE, and now when we install the JDK, the JRE is also installed (of course, the JRE can also be installed separately).

JVM memory area division

Roughly, the internal architecture of the JVM is divided into three parts, namely: the class loader (ClassLoader) subsystem, the runtime data area, and the execution engine.

class loader

Each Java virtual machine consists of a class loader subsystem (class loader subsystem), which is responsible for loading the types (classes and interfaces) in the program and assigning a unique name. Every Java virtual machine has an execution engine (execution engine) responsible for executing the instructions contained in the loaded classes. The two types of class loaders of JVM include: startup class loader and user-defined class loader. The startup class loader is a part of the JVM implementation, and the user-defined class loader is a part of the Java program and must be a subclass of the ClassLoader class. .

Execution engine: it is either executing bytecode or executing native methods

The main execution technologies are: interpretation, just-in-time compilation, adaptive optimization, and direct execution at the chip level. Interpretation belongs to the first-generation JVM, just-in-time compilation JIT belongs to the second-generation JVM, and adaptive optimization (currently Sun's HotspotJVM uses this technology) Learn from the experience of the first-generation JVM and the second-generation JVM, and use a combination of the two.

Adaptive optimization: Start by interpreting all the code and monitor the code execution, then start a background thread for those methods that are called frequently, compile it to native code, and do careful optimization. If the method is not used frequently, the compiled code is canceled, and it is still interpreted and executed.

Runtime data area: mainly includes: method area, heap, Java stack, PC register, local method stack

  • Method area and heap are shared by all threads

Heap: Stores all objects created by the program at runtime

Method area: When the class loader of the JVM loads the .class file and parses it, the parsed type information is put into the method area.

  • Java stack and PC registers are exclusively shared by threads

The JVM stack is private to the thread. When each thread is created, a JVM stack is created. The JVM stack stores the variables of the local basic type in the current thread (eight basic types defined in java: boolean, char, byte, short, int, long, float, double), partial return results and Stack Frame, objects of non-basic types only store an address pointing to the heap on the JVM stack

  • Native method stack: stores the state of native method calls

JVM runtime data area

Because the data area of ​​the jvm runtime is still very important to our development, so let's talk about it.

  • Method Area

In Sun JDK, this area corresponds to PermanentGeneration, also known as persistent generation.

The method area stores the loaded class information (name, modifier, etc.), static variables in the class, constants defined as final types in the class, Field information in the class, and method information in the class. When obtaining information through the methods such as getName and isInterface in the Class object, these data all come from the method area, and the method area is also shared globally. Under certain conditions, it will also be GCed. When the method area needs to use the memory When it exceeds its allowable size, an OutOfMemory error message will be thrown.

  • Heap

It is the area used by the JVM to store object instances and array values. It can be considered that the memory of all objects created by new in Java is allocated here, and the memory of the objects in the Heap needs to wait for the GC to be reclaimed.

The heap is shared by all threads in the JVM, so allocating object memory on it needs to be locked, which also leads to a relatively large overhead for new objects.

In order to improve the efficiency of object memory allocation, Sun Hotspot JVM allocates an independent space TLAB (Thread Local Allocation Buffer) for the created thread. Locking, so the JVM will try to allocate memory on TLAB when allocating memory to thread objects. In this case, the performance of allocating object memory in JVM is basically as efficient as C, but if the object is too large, it is still Use heap space allocation directly.

TLAB only acts on the Eden Space of the new generation, so when writing Java programs, it is usually more efficient to allocate multiple small objects than large objects.

  • JavaStack (java stack): The virtual machine will only perform two operations directly on the Javastack: push the stack or pop the stack in units of frames

Each frame represents a method. Java methods have two return methods, return and throw an exception. Both methods will cause the frame corresponding to the method to pop out of the stack and release memory.

The composition of the frame: local variable area (including method parameters and local variables, for instance method, the this type must be saved first, in which method parameters are strictly placed in the order of declaration, and local variables can be placed arbitrarily), operand stack, frame data area ( Used to help support constant pool parsing, normal method returns and exception handling).

  • ProgramCounter (program counter)

Each thread has its own PC register, which is also created when the thread starts. The contents of the PC register always point to the address of the next instruction to be executed, where the address can be a local pointer or an offset in the method area corresponding to the start instruction of the method.

If the thread executes the Java method, the PC saves the address of the next executed instruction. If the thread executes the native method, the value of Pc is undefined

  • Nativemethodstack (native method stack): saves the address of the native method entry area

Depends on the implementation of the native method. For example, if the native method implemented by a JVM uses the C connection model, the native method stack is the C stack. It can be said that when a thread calls a native method, it enters a field that is not restricted by the JVM. That is, the JVM can dynamically extend itself using native methods.

JVM garbage collection

Sun's JVMGenerationalCollecting (garbage collection) principle is as follows: objects are divided into young generation (Young), old generation (Tenured), persistent generation (Perm), and different algorithms are used for objects with different life cycles. (Based on object life cycle analysis)

Usually, the JVM memory reclamation we are talking about always refers to the heap memory reclamation. Indeed, only the contents of the heap are allocated dynamically. Therefore, the young and old generations of the above objects refer to the JVM's Heap space, while the persistent generation is It is the MethodArea mentioned earlier, not a Heap.

The basic principle of GC: reclaim objects that are no longer used in memory. The method used for reclamation in GC is called collector. Since GC needs to consume some resources and time, Java analyzes the life cycle characteristics of objects. Collect objects according to the new generation and the old generation to shorten the pause caused by GC to the application as much as possible

(1) The collection of objects in the new generation is called minor GC;

(2) The collection of objects in the old generation is called Full GC;

(3) The GC enforced by actively calling System.gc() in the program is Full GC.

Different object reference types are collected by GC in different ways. JVM object references are divided into four types:

(1) Strong reference: By default, the object uses a strong reference (the instance of this object has no other object references, and it will only be recycled during GC)

(2) Soft reference: Soft reference is an application provided in Java that is more suitable for caching scenarios (it will be GC only when the memory is not enough)

(3) Weak reference: it will be recycled by GC during GC

(4) Virtual reference: Since virtual reference is only used to know whether the object is GC

  • Young (young generation)

The young generation is divided into three districts. One Eden area, two Survivor areas. Most objects spawn in the Eden area. When the Eden area is full, the surviving objects will be copied to the Survivor area (one of the two). When the Survivor area is full, the surviving objects in this area will be copied to another Survivor area. When it is full, the objects copied from the first Survivor area and still alive at this time will be copied to the Tenured area. It should be noted that the two areas of the Survivor are symmetrical and have no relationship, so the same area There may be objects copied from Eden and objects copied from the previous Survivor at the same time, and only the objects copied from the first Survivor are copied to the old area. Moreover, one Survivor area is always empty.

  • Tenured (Old Generation)

The old generation holds objects that survive from the young generation. Generally speaking, the old generation stores objects with long lifespans.

  • Perm (Persistent Generation)

Used to store static files, now Java classes, methods, etc. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, etc. In this case, a relatively large persistent generation space needs to be set to store these newly added classes during operation. The persistent generation size is set with -XX:MaxPermSize=.

I have a WeChat public account, and I often share some dry goods related to Java technology; if you like my sharing, you can use WeChat to search for "Java Head" or "javatuanzhang" to follow.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324838022&siteId=291194637