Abstract: JVM is a specification for computing devices. It is a fictitious computer that is realized by simulating various computer functions on an actual computer.
This article is shared from the HUAWEI CLOUD community "[[Cloud-based co-creation] JVM memory model exploration journey]( https://bbs.huaweicloud.com/blogs/300266?utm_source=segmentfault&utm_medium=bbs-ex&utm_campaign=other&utm_content=content) "Author: Domino's old brand.
1. Introduction to JVM
1.1 What is JVM?
JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine). It is a specification for computing devices. It is a fictitious computer that is realized by simulating various computer functions on an actual computer.
1.2 Advantages of JVM
1.2.1 Write once, run everywhere.
JVM allows java programs to be written once, exported and run. Separate the underlying code from the operating environment. After you write a code, you don’t need to modify the content again. You can run it by installing a different JVM environment and automatically converting, seamlessly connecting in various systems.
1.2.2 Automatic memory management, garbage collection mechanism.
When Java was born, C and C++ dominated the world, but there was no memory management mechanism in these two languages, and management was carried out by manual operation, which was very troublesome and cumbersome.
At this time Java came into being. In order to deal with memory management, a garbage collection mechanism was specially designed to automatically manage memory. The operation is greatly optimized, so that programmers do not have to worry about whether the memory will overflow or not when they are swimming in the ocean of code generation, which "affects our output" problems, and immediately received tons of praise.
1.2.3 Array subscript out-of-bounds check
When Java was born, there was also a headache for C and C++ bigwigs at that time. There was no checking mechanism for array subscripts out of bounds. This is okay, and it is another culprit that affects "our violent output". Therefore, JVM continued to hold the warm man's thoughts, and came again with a loving hug.
JVM once again saw the troubles of the big guys, and decisively provided an automatic check mechanism for array subscripts out of bounds. After detecting the array subscripts out of bounds, it will automatically throw the exception "java.lang.ArrayIndexOutOfBoundsException" at runtime. At that time, it moved many industry leaders (I guess).
1.2.4 Polymorphism
JVM also has a polymorphic function, which is implemented through the same interface and different instances to complete different business operations, such as: defining an animal interface (with a method of eating), we can create kittens through this animal (Eat fish), create another dog (eat meat), and create another assistant (eat snacks, O(∩_∩)O haha~).
Think about it carefully, what impact does it have on us? The benefits are much older, such as:
(1) Eliminate the coupling relationship between types;
(2) Replaceability;
(3) Scalability;
(4) Interface;
(5) Flexibility;
(6) Simplification;
1.3 The relationship between JVM, JRE, JDK
1.3.1 Introduction to JVM
JVM is the abbreviation of Java Virtual Machine, which is a kind of simulated virtual computer, which is realized by simulating computing functions in different computer environments.
After the introduction of the Java virtual machine, the Java language does not need to be recompiled when it runs on different platforms. Among them, the Java virtual machine shields the information related to the specific platform, so that the Java source program can be run on different platforms after the compilation is completed, achieving the purpose of "compiling once, running everywhere". One of the important characteristics of the Java language is The key point of the platform, that is, the independence of the platform, is the JVM.
1.3.2 Introduction to JRE
JRE is the abbreviation of Java Runtime Environment. It is the environment that allows the operating system to run Java applications. It contains JVM, which means that JRE is only responsible for the operation of existing Java source programs. It does not include The development tool JDK does not include the compiler, debugger and other tools inside the JDK.
1.3.3 Introduction to JDK
JDK is the abbreviation of Java Development Kit. It is a Java development kit and is the core of the entire Java program development. It mainly includes JRE, Java system class libraries, and tools for compiling and running Java programs, such as javac.exe and java.exe command tools.
1.4 Common implementation of JVM
Oracle (Hotspot, Jrockit), BEA (LiquidVM), IBM (J9), taobaoVM (Taobao dedicated, in-depth customization of Hotspot), zing (garbage collection mechanism is very fast, reaching about 1 millisecond).
1.5 JVM memory structure diagram
When the Java program is compiled into a .class file == "ClassLoader == "load the bytecode file into the JVM;
1.5.1 Method area, heap
The main information stored in the method area is the class information (class attributes, member variables, constructors, etc.), heap (created objects).
1.5.2 Virtual machine stack, program counter, local method stack
When an object in the heap calls a method, the method runs in the virtual machine stack, program counter, and local method stack.
1.5.3 Execution Engine
When the code in the method is executed, the code is executed by the "interpreter" in the execution engine; the frequently called code in the method, that is, the hot code, is executed by the "just-in-time compiler", and its execution speed is very fast.
1.5.4 GC (garbage collection mechanism)
GC is to reclaim objects that are not referenced in the heap memory, which can be manual or automatic.
1.5.5 Native method interface
Because the JVM cannot directly call the functions of the operating system, it can only call the functions of the operating system through the local method interface.
2. JVM memory structure-program counter
2.1 The definition of the program counter
Program Counter Register is the program counter (register), used to record the execution address of the next Jvm instruction.
2.2 Operation steps
javap is mainly used to operate JVM, javap -c is to disassemble java code. The following figure shows the output result of compiling demo.java and then executing javap -c demo:
The first column is binary bytecode, that is, JVM instructions, and the second column is java source code. The sequence number in the first column is the execution address of the JVM instruction.
The JVM will record the address of a JVM instruction that needs to be executed through the program counter (such as 0 in the first line), and then hand it to the interpreter to parse it into machine code, and finally hand it to the cpu (only machine code can be recognized) to complete the execution of one line .
If you want to execute the next line, continue to let the JVM's program counter record the next address (such as 3 in the second line), and then pass it to the interpreter for analysis and give it to the cpu, and so on.
2.3 Features
2.3.1 Thread private
2.3.2 There will be no memory overflow
3. JVM memory structure-virtual machine stack
3.1 Definition
The virtual machine stack is the memory space required by each thread to run. Each stack consists of multiple stack frames. Each thread can only have one active stack frame (corresponding to the method currently being executed), and all stack frames follow The principle of last in first out and first in last out.
The stack frame is the memory occupied every time a method is called, and the content parameters, local variables, and return address saved in the stack frame.
Note 1: Garbage collection does not involve stack memory, because stack memory is generated by method calls, and the stack will be popped up after the method call ends.
Note 2: The stack memory is not allocated as much as possible, because the physical memory is fixed, the larger the stack memory, it can support more recursive calls, but the number of executable threads will become less and less.
Note 3: The local variable of the method is thread-safe when it does not escape the scope of the method; if it references an object (such as a static variable, that is, a shared variable, the method that uses the object as a parameter, the return value is the method of the object) ), and escaped from the scope of the method, you need to consider the issue of thread safety.
3.2 Stack memory overflow
3.2.1 Cause
(1) In the virtual machine stack, there are too many stack frames (infinite recursion), as shown in Figure 1 There are too many stack frames;
(2) Each stack frame occupies too large, as shown in Figure 2, the stack frame is too large.
3.2.2 A small experiment on stack memory overflow
3.2.2.1 Small experiment with too many stack frames
A small experiment of infinite recursive calls (too many stack frames), the method1() method calls itself infinitely in the main method, so what will happen?
The answer is obvious, the program crashed and a stack memory overflow error occurred, as shown in the following figure:
-Xss: This parameter specifies the size of the virtual machine stack of each thread;
Then we try to set the size of a virtual machine stack to 256k, what will happen?
We found that when we adjusted the size of the virtual machine stack and executed the method 4315 times, the memory overflowed. Before adjusting the virtual machine stack, we were 23268 times. Obviously we can adjust the size of the virtual machine stack through the -Xss parameter. Control the overflow of memory.
3.2.2.2 Thread running diagnostic small experiment
In the imaginary scene, the boss is outputting like crazy. Suddenly the CPU bursts, showing that the CPU is occupying too much. How to locate the problem of which line of code, yes, which line (the boss is very busy, of course, it must be accurate Yes, tens of millions up and down in a minute, O(∩_∩)O haha~)?
Linux environment:
Run Stack_6 this java bytecode (.class) file in the background:
**Note: Regardless of whether the output of the nohup command is redirected to the terminal, the output will be appended to the nohup.out file in the current directory.
**
(1) Through the top command, check the process (equivalent to the task manager), and found a suspicious guy who occupies 100% of the CPU. How do other friends play happily, correct errors in seconds. . .
Note: The top command is used to check which process occupies too much CPU and return the process number.
(2) Use the ps H -eo pid,tid,%cpu | grep command to filter the content in the task manager.
Note: ps H -eo pid,tid,%cpu |grep is to use the ps command to check which thread is occupying the CPU too high, and return the process id, where pid is the process id, tid is the thread id, and %cpu is the CPU occupancy;
Found the culprit, this string of scary red. . .
(3) View the specific situation in the problematic process of 20389 through the jstack process id.
Note: The jstack process id is used to locate a specific piece of code that occupies too much CPU through the jstack command. Note that the thread id found by the jstack command is in hexadecimal and needs to be converted;
Found that there is a bunch of executed code in it, so how do we find out which guy is doing the thing? In the above picture, we can find that the thread that is doing things is 20441, then we use a calculator to convert 20441 to 4FD9 in hexadecimal and try again. There is only one truth.
By comparing nid (thread id) 4fd9, we found that this thread called thread1 has been running (RUNNABLE state), and checked that the location is located at the 11th line of the Stack_6.java file. . .
Now we go back to the source code, in the 11th line of the Stack_6 file, we found that there has been an endless loop here, and finally found you. Fortunately, I did not give up, Ness~
4. JVM memory structure-local method stack
4.1 Definition
Because Java itself sometimes cannot directly interact with the bottom layer of the operating system, but sometimes requires Java to call local C or C++ methods, local method stacks emerge at this time. They are a class of methods with the native keyword.
5. JVM memory structure-heap
5.1 Definition
Heap: The object created by the new keyword is stored in the heap
5.2 Features
5.2.1 Thread sharing
Objects stored in the heap are shared by threads, so thread safety issues need to be considered.
5.2.2 Has a garbage collection mechanism
Because the objects stored in the heap store a large number of objects, he is equipped with a small assistant-garbage collection mechanism (automatic and manual transmission can be adjusted).
5.3 Heap memory overflow experiment
5.3.1 A small experiment to modify the heap memory size parameter
Continue to imagine a scenario, when a big guy has developed a piece of code (of course, the big guys are very confident, how can the code I write have problems, it does not exist...), but the test can't run. To be safe, let's try the test silently, safety first. But the machine's memory is so big, the boss must have run many times, and there is no problem. This is not to find the fault. . . Let's try again by silently changing the parameters of the machine (if you want to go to the boss, you must show evidence~).
-Xmx: One of the JVM tuning parameters, this parameter represents the maximum value that the java heap can be expanded to. The following case:
After 26 executions, a heap memory overflow error was reported decisively in the background.
Next, use the -Xmx JVM tuning parameter to reduce the heap memory to 8m. What will happen if you try again?
The operation is basically the same as the case when the stack memory overflows, the number of times is obviously reduced, and the heap memory overflow error occurs after only 17 calls.
5.3.2 A small experiment for heap memory diagnosis
jps tool: view which java processes are in the current system
jmap tool: view the occupancy of heap memory jmap -heap process id
jconsole tool: Graphical tool with multi-functional monitoring function and continuous monitoring.
Below we run the code and use the jconsole visual graphics tool to view the heap memory usage.
As we can see in the above figure, when we create a 10mb array object, the memory usage has risen to a certain extent; then after we manually call the garbage collection mechanism, the memory has been greatly released.
6. JVM memory structure-method area
6.1 Definition
There is a method area in the Java virtual machine that is shared by all jvm threads. The method area is somewhat similar to a compiled code block in a traditional programming language or a code segment at the operating system level. It stores the construction information of each class, such as runtime constant pool, fields, method data, and method and construction method code, including some special methods used in class and instance initialization and interface initialization.
The method area is individually called non-heap (non-heap), which can be regarded as a memory space independent of the heap. It is a concept defined in the JVM specification for storing class information, constant pools, and static variables. After JIT compilation Code and other data, where to put it, different implementations can be put in different places.
6.2 Features
(1) The method area, like the java heap, is a memory area shared by each thread;
(2) The method area is created when the JVM starts;
(3) The size of the method area, the same as the heap space, can be fixed or expanded;
(4) The size of the method area determines how many classes the system can save. If the system defines too many classes, causing the method area to overflow, the virtual machine will also throw an overflow error OutOfMemoryError;
(5) Closing the JVM will release the memory in this area.
6.3 Schematic diagram of JVM memory structure
In the JVM memory structure 1.6, the method area is stored in the memory structure, called the permanent generation, which stores the runtime constant pool (including string pool StringTable), class information, and class loader;
In the JVM memory structure 1.8, the method area was stored as a concept in local memory, called meta space, which stored the runtime constant pool, class information, and class loader. At this time, the string pool (StringTable) was stored Among the piles.
Click to follow and learn about Huawei Cloud's fresh technology for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。