Ten Years of Technology: Do you understand the Java virtual machine?

Java virtual machine (java virtual machine, JVM), a virtual machine capable of running java bytecode. Virtual machine as a programming language, in fact

Not only is it dedicated to the Java language, any language can be compiled and run by the JVM as long as the generated compiled file matches the JVM's requirements for the format of the loaded compiled file.

Such as kotlin, scala, etc.

** There are many JVMs, not just Hotspot, but also JRockit, J9, etc. The basic structure of JVM ** JVM is composed of three main subsystems

Class loading subsystem

Runtime data area (memory structure)

Execution engine filefilefileruntime data area (memory structure) 1. Method Area

The bytecode of all fields and methods of the class, as well as some special methods such as constructors, interface codes are also defined here. Simply put, all the defined methods

Information is stored in this area, static variable constant class information (construction method/interface definition) runtime constant pool is stored in the method area, although Java virtual

The machine specification describes the method area as a logical part of the heap, but it has an alias called Non-Heap (non-heap), the purpose should be to match Java

Heap division

2. Heap

The virtual machine is automatically allocated and created when it starts to store the instance of the object. Almost all objects are allocated memory on the heap. When the object cannot be applied for in the space

The memory will throw OutOfMemoryError exception. It is also the main area managed by the garbage collector. file2.1 Young Generation

The area where classes are born, grow, and die, where a class is generated, applied, and finally collected by the garbage collector.

End of Life.

The Cenozoic is divided into two parts: Eden space and Survivor space. All classes are newly created in Eden space.

The surviving area is divided into From and To areas. When the space in the Eden area is used up, the program needs to create objects again, and the garbage collector of the JVM will garbage back the Eden area

Receive (Minor GC), destroy the objects in the Eden area that are no longer used by other objects. Then move the remaining objects in the Eden area to From

Survivor area. If the From Survivor area is also full, garbage collect the area and move to the To Survivor area.

2.2 Old Generation

The objects in the new generation that are still in stock after many GCs are moved to the old area. If the old age is also full, Major GC (also called Full GC) will occur at this time,

Clean up the memory of the elderly zone. If the elderly area executes Full GC and finds that the object still cannot be saved, it will throw

OOM (OutOfMemoryError) exception

2.3 Meta Space

After JDK1.8, the meta space replaced the permanent generation. It is an implementation of the method area in the JVM specification. The difference is that the metadata area is not in the virtual machine, but is used

The local memory of the permanent generation is in the virtual machine, and the permanent generation also belongs to the heap in the logical structure, but not physically.

Why was the permanent generation removed?

Refer to the official explanation http://openjdk.java.net/jeps/122

It probably means that the removal of the permanent code is an effort to integrate HotSpot and JRockit, because JRockit does not have a permanent code, and there is no need to configure a permanent code.

file

3. Stack

The memory model of Java thread execution method, a thread corresponds to a stack, each method will create a stack frame (used to store local variables

Table, operand stack, dynamic link, method export and other information) there is no garbage collection problem, as long as the thread ends, the stack will be released. The life cycle and thread

To

4. Native Method Stack (Native Method Stack)

The function is very similar to the stack, except that the Java stack serves the JVM to execute Java methods, while the local method stack serves the JVM to execute native methods. Register native

Method, load the local method library when Execution Engine executes 5. Program Counter Register

It is a pointer to the method bytecode in the method area (used to store the address of the starter instruction, which is the instruction code to be executed), which is executed by

The engine reads the next instruction, which is a very small memory space, almost negligible

JDK performance tuning monitoring tool Jinfo

View the extended parameters of the running Java program

View the parameters of the JVM

file

View java system properties

Equivalent to System.getProperties()

fileThe Jstatjstat command can view the usage of each part of the heap memory and the number of loaded classes. Command format:

jstat [-command options] [vmid] [interval time/ms] [number of queries]

Jmap

Can be used to view memory information

Heap object statistics are shown in the filefigure: fileNum: serial number Instances: number of instances Bytes: occupied space size Class Name: class name heap informationfilefile heap memory dumpfile

jmap -dump:format=b,file=temp.hprof

You can also automatically export the dump file when setting the memory overflow (when the project memory is large, it may not be exported)

1.-XX: HeapDumpOnOutOfMemoryError

2.-XX:HeapDumpPath=output pathfilefile

You can use the jvisualvm command tool to import file analysis

fileJstack

jstack is used to generate a thread snapshot of the java virtual machine at the current moment. fileDue to the limited space, the content of this document is too much, only a part of it can be shown to share with you, but through a short article, you can understand that this document can learn a lot of JVM-related knowledge points and work skills, you need to get this Friends of "In-Depth Understanding of the Java Virtual Machine" actual combat document can pay attention to the editor, the follow-up will continue to update, it is not easy to organize, please forward it, your forwarding is the biggest motivation for me to share, thank you all!

This article is published by OpenWrite , a multi- posting platform for blogs !

Guess you like

Origin blog.csdn.net/yueyunyin/article/details/103568983