Description: Doing Java development of almost all know that the JVM this noun, but because of the JVM for simple practical development of associated or few, usually work a year or two (of course not including what love of learning and specialises in performance optimization), very few people can be a very good to learn and understand what is the JVM, and find out the working principle of the JVM I think it is very necessary to understand and learn seriously, especially just entry or entry soon Java development, this is the foundation of Java.

JVM(Java Virtual Machine)

The cross-platform feature of Java programs mainly means that bytecode files can be run on any computer or electronic device that has a Java virtual machine. The Java interpreter in the Java virtual machine is responsible for interpreting bytecode files into specific machine code for running. So at run time, Java source programs need to be compiled by the compiler into.class files. Exe is known as the Java class file execution program, but in reality, the java.exe program is just an execution shell, it will load the JVM. DLL. Libjvm.so, the dynamic connection library, is where the actual Java virtual machine operations are handled.

The JVM is part of the JRE. It is an imaginary computer by simulating various computer functions on an actual computer. The JVM has its own complete hardware architecture, such as processors, stacks, registers, and the corresponding instruction system. The most important feature of the Java language is that it runs across platforms. The PURPOSE of using the JVM is to enable cross-platform implementation independent of the operating system. So, the JAVA Virtual machine JVM belongs to the JRE, and now we install the JRE with the JDK (or you can install the JRE separately, of course).

JVM memory region partitioning

Roughly, the INTERNAL architecture of the JVM is divided into three parts: the ClassLoader subsystem, the runtime data area, and the execution engine.

Class loader

Each Java VIRTUAL machine is provided by a Class loader subsystem that takes care of the types (classes and interfaces) within the program and assigns them unique names. Each Java virtual machine has an execution engine that executes the instructions contained in the loaded class. The two types of JVM loaders are startup class loaders, which are part of the JVM implementation, and user-defined class loaders, which are part of Java programs and must be subclasses of the ClassLoader class.

Execution engine: It either executes bytecode or executes local methods

The main execution techniques are: interpretation, just-in-time compilation, adaptive optimization, and cha-level direct execution. Interpretation belongs to the first-generation JVM, just-in-time compilation belongs to the second-generation JVM, and adaptive optimization (currently Sun’s HotspotJVM uses this technology) combines the experience of first-generation and second-generation JVMS.

Adaptive optimization: Start with all code interpreted and monitored, then start a background thread for frequently invoked methods, compile them into native code, and carefully optimize them. If the method is no longer used frequently, the compiled code is cancelled and interpreted execution continues.

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

  • The method area and heap are shared by all threads

Heap: Holds objects created by all programs at run time

Method area: When the JVM’s classloader loads a. Class file and parses it, it puts the parsed type information into the method area.

  • The Java stack and PC registers are exclusive to the thread

The JVM stack is thread private. Each thread is created with the JVM stack, which holds variables of the local primitive types in the current thread. Boolean, char, byte, short, int, long, float, double), partial return results, and Stack frames. Objects of non-basic types only hold one address on the JVM Stack that points to the heap

  • Local method stack: Stores the state of local method calls



JVM runtime data area

Since the data area of the JVM runtime is a particularly important part of our development, we’ll take it apart.

  • Method Area

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

The method area stores information about the loaded Class (name, modifiers, 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 the developer obtains information through getName, isInterface and other methods in the Class object in the program, This data comes from the method region, which is also globally shared and is GC under certain conditions. An OutOfMemory error message is raised when the method region needs more memory than it allows.

  • Heap (Heap)

It is the area where the JVM stores object instances as well as array values, and it can be considered that memory for all objects created by New in Java is allocated there, while memory for objects in Heap is waiting for GC to collect.

The heap is shared by all threads in the JVM, so allocating object memory on it requires locking, which also causes the overhead of new objects to be high.

The Sun Hotspot JVM allocates an independent TLAB (Thread Local Allocation Buffer) to the threads it creates in order to improve the efficiency of object memory Allocation. The TLAB size is calculated by the JVM according to the running situation. There is no lock required to allocate objects on TLAB, so the JVM tries to allocate memory on TLAB when allocating objects on thread. In this case, the PERFORMANCE of allocating object memory in JVM is almost as efficient as that of C, but if the object is too large, it still uses heap space allocation directly.

TLAB only works with the new generation of Eden Space, so when writing Java programs, it is often more efficient to allocate multiple small objects rather than large ones.

  • JavaStack(Javastack) : The virtual machine will only perform two operations directly on the JavaStack: push or push on a frame-by-frame basis

   

Each frame represents a method, and a Java method has two ways to return it, return and throw an exception. Both ways cause the frame corresponding to the method to go off the stack and free memory.

Frame: the local variable area (including the method parameters and local variables, for instance methods, but also the first save this type, including the method parameters strictly in declaration order placed, local variables can be arbitrarily placed), the operand stack, the frame data area (to help support the constant pool parsing, returned to normal method and exception handling).

  • ProgramCounter

    

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

If Thread executes a Java method, the PC saves the address of the next execution instruction. If thread executes native methods, the value of Pc is undefined

  • Nativemethodstack: Stores the address of the native method into the region

    

Depending on the implementation of a local method, such as the local method of a JVM implementation using the C-connection model, the local method stack is the C stack, so to say that when a thread calls a local method, it enters a jVM-free realm, that is, the JVM can use local methods to dynamically extend itself.

JVM garbage Collection

Sun JVMGenerationalCollecting (recycling) principle is this: the object is divided into Young generation (Young), the old generation (Tenured), lasting generation (Perm), objects of different life cycle using different algorithms. (Based on object lifecycle analysis)

It is true that only the contents of the Heap are allocated dynamically, so both the young and old generation of the above objects refer to the Heap space of the JVM, while the persistent generation is the MethodArea mentioned earlier, which is not part of the Heap.

The basic principle of GC: memory object is no longer used in recycling, for recycling in the GC method known as collector, because the GC need to consume some resource and time, Java in through analysis of the characteristics of the object’s lifecycle, according to the new and the old generation of way to collect objects, by as much as possible to shorten the GC to suspend application

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

(2) Collection of old generation objects is called Full GC;

(3) The program actively calls system.gc () to force the gc to be Full GC.

Different types of object references are collected in different ways by GC. References to JVM objects fall into four types:

(1) Strong references: By default, objects are strongly referenced (instances of this object are not referenced by other objects, and will only be recycled during GC).

(2) Soft references: Soft references are an application provided in Java that is suitable for caching scenarios (GC only when memory is low).

(3) Weak references: must be collected by the GC

(4) Virtual references: Since virtual references are only used to tell if an object is GC

  • Young

    

The younger generation is divided into three sections. One Eden zone, two Survivor zones. Most objects are generated in the Eden zone. When Eden is full, the surviving objects will be copied to the Survivor zone (one of the two). When this Survivor zone is full, the surviving objects in this Survivor zone will be copied to another Survivor zone. When this Survivor zone is full, Objects copied from the first Survivor zone that are still alive at this time will be copied to the old zone (Tenured). It should be noted that the two Survivor zones are symmetric and have no sequence relationship, so there may be objects copied from Eden and the object copied from the previous Survivor in the same zone, while only the object copied from the first Survivor to the old zone. Also, one of the Survivor zones is always empty.

  • Tenured (old generation)

The tenured generation holds objects that survive from the younger generation. In general, the old generation stores objects with a long life span.

  • Perm (persistent generation)

Used to store static files, nowadays Java classes, methods, etc. Persistent generation has no significant impact on garbage collection, but some applications may generate or call classes dynamically, such as Hibernate, etc. In such cases, a large persistent generation space needs to be set up to store the classes added during the run. The persistent generation size can be set by -xx :MaxPermSize=.

I have a wechat official account, and I often share some Java technology-related dry goods. If you like my share, you can follow me by searching “Java leader” or “Javatuanzhang” on wechat.