Abstract: THE JVM is a specification for computing devices. It is a fictional computer, which is implemented by simulating various computer functions on a real computer.

This article is shared from huawei cloud community “[Cloud resident Co-creation]JVM Memory model exploration journey”, author: Domino’s ancient brand.

1. Introduce the JVM

1.1 What is the JVM?

The JVM, short for JavaVirtual Machine (JavaVirtual Machine), is a specification for computing devices. It is a fictional computer that emulates various computer functions on an actual computer.

1.2 Advantages of the JVM

1.2.1 Write once, run everywhere

The JVM allows Java programs to be written once and exported to run. Separate the underlying code from the runtime environment. Once you’ve written a piece of code, you don’t have to change it again, but you can automatically convert it by installing different JVM environments and run it seamlessly across a variety of 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. It was managed by manual operation, which was very troublesome and tedious.

At this time, Java came into being, in order to deal with the memory management aspect, specially designed garbage collection mechanism, to automatically carry out memory management. Greatly optimized the operation, so that programmers do not have to be scratching in the ocean of code generation, but also worry about memory will overflow these “affect our output” problems, immediately won tons of praise.

1.2.3 Array subscript out-of-bounds check

When Java was born, there was also a problem that bothered C and C++ leaders at the time. There was no checking mechanism for array subscripts out of bounds, which was another culprit for “our violent output”.

JVM again saw bosses worry, decisive provides an array subscript cross-border automatically check mechanism, in the array subscript is detected after crossing the line, will be automatically thrown at runtime the exception, “Java. Lang. ArrayIndexOutOfBoundsException” It impressed a lot of industry bigwigs at the time (I guess).

The polymorphism of 1.

The JVM also has a polymorphic capability, which is implemented through the same interface, different instances, to perform different business operations, such as: By defining an animal interface (with a way to eat), we can create a kitten (to eat fish), a dog (to eat meat), and a little helper (to eat snacks) from this animal.

If you think about it, what does it do to us? The benefits are much older, such as:

(1) Eliminate the coupling relationship between types;

(2) substitutability;

(3) scalability;

(4) interface;

(5) Flexibility;

(6) simplification;

1.3 Relationship between JVMS, JRE, and JDK

1.3.1 Introduction to the JVM

The JVM, short for JavaVirtual Machine, is a simulated virtual computer implemented by simulating computing functions in different computer environments.

With the introduction of the Java Virtual Machine, the Java language does not need to be recompiled when running on different platforms. Among them, The Java virtual machine (JVM) shields platform-specific information, enabling Java source programs to run on different platforms after compilation, achieving the purpose of “compile once, run everywhere”. One of the important features of the Java language is cross-platform, that is, platform-independent, and its key point is the JVM.

1.3.2 Introduction to JRE

JRE is short for JavaRuntime Environment, which enables the operating system to run Java applications. It contains THE JVM, that is to say, THE JRE is only responsible for running the existing Java source programs. It does not contain the DEVELOPMENT tool JDK. None of the JDK’s internal compilers, debuggers, and other tools are included.

1.3.3 Introduction to the JDK

JDK is short for JavaDevelopment Kit, is Java development Kit, is the core of the entire Java program development. It mainly contains JRE, Java system class library, and tools for compiling and running Java programs, such as javac.exe and java.exe command tools.

1.4 Common implementations of the JVM

Oracle (Hotspot, Jrockit), BEA (LiquidVM), IBM (J9), taobaoVM (dedicated to Taobao, deeply customized to Hotspot), Zing (garbage collection mechanism is very fast, to about 1 millisecond).

1.5 Memory structure diagram of the JVM

When the Java program is compiled as.class file ==, the ClassLoader loads the bytecode file into the JVM;

1.5.1 Method area and heap

The method area stores mainly class information (class attributes, member variables, constructors, etc.) and 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, the program counter, and the local method stack.

1.5.3 Execution Engine

When executing code in a method, the code is executed through the “interpreter” in execution engine execution; The frequently invoked code in the method, known as hotspot code, is executed by the just-in-time compiler, which is extremely fast.

1.5.4 GC (Garbage Collection Mechanism)

GC is a collection of objects that are not referenced in heap memory, either manually or automatically.

1.5.5 Local Method Interface

Because the JVM cannot call operating system functions directly, it can only call operating system functions through the native method interface.

2. JVM memory structure – program counters

2.1 Definition of program counter

The Program Counter Register, or Program Counter Register, is used to record the execution address of a Jvm instruction.

2.2 Procedure

Javap is mainly used to manipulate the JVM. Javap-c disassembs Java code. The following figure shows the output of demo after compiling demo. Java and executing Javap -c.

The first column is binary bytecode, or JVM instructions, and the second column is Java source code. The ordinal number in the first column is the execution address of the JVM instruction.

The JVM uses a program counter to record the address of a JVM instruction that needs to be executed (such as the 0 on the first line), which is then passed to the interpreter for parsing into machine code, and finally to the CPU (which recognizes machine code only) to complete the line.

To execute the next line, the JVM’s program counter continues to record an address (such as line 3 in line 2), which the interpreter interprets to the CPU, and so on.

2.3 the characteristics of

2.3.1 Thread private

2.3.2 No memory overflow

3. JVM memory structure – vm stack

3.1 define

Virtual machine stack is the memory space required by each thread to run. Each stack consists of multiple stack frames, and each thread can only have one active stack frame (corresponding to the currently executing method). All stack frames follow the principle of last-in, first-out, advanced, last-out.

The stack frame is the memory occupied each time the method is called, and the content parameters, local variables, and return address are stored in the stack frame.

Note 1: Garbage collection does not involve stack memory, because stack memory is generated by method calls and is ejected when the method call ends.

Note 2: Larger stack memory allocation is not better, because physical memory is fixed. Larger stack memory can support more recursive calls, but fewer threads can be executed.

Note 3: Method local variables are thread-safe when they do not escape the scope of the method; If it references an object (such as a static variable, a shared variable, a method that takes an object as an argument, a method that returns an object) and escapes the scope of a method, thread-safety concerns arise.

3.2 Stack memory overflow

3.2.1 Causes

(1) There are too many stack frames (infinite recursion) in the virtual machine stack, as shown in Figure 1.

(2) The occupation of each stack frame is too large, as shown in Figure 2.

3.2.2 Small experiment of stack memory overflow

3.2.2.1 Small experiment of too many stack frames

A little experiment with infinite recursive calls (stack frame overload), so what happens when the method1() method calls itself infinitely in the main method?

The obvious answer is that the program crashes, producing a stack overflow error, as shown below:

-xss: specifies the size of the vm stack for each thread.

Let’s see what happens by setting the size of a virtual machine stack to 256K.

We found that we ran out of memory 4315 times after resizing the vm stack, and 23268 times before resizing the vm stack. Obviously, we can control the running out of memory by resizing the vm stack with the -xSS parameter.

3.2.2.2 Thread running diagnostic mini-experiment

Imagine the scene, big brother in the crazy output, suddenly CPU burst table, show CPU occupancy too much, how to locate the problem of which line of code, yes, which line (big brother are very busy, of course, to be accurate, a minute tens of millions of up and down, O(∩_∩) haha ~)?

In Linux:

Run the Java bytecode (.class) file Stack_6 in the background:

Note: The output of the nohup command is appended to the nohup.out file in the current directory, whether or not you redirect it to the terminal.

(1) Through the top command, check the process (equivalent to the task manager), found a suspicious guy occupying 100% of the CPU, this is also good, hurry to see what happened, there is no law, this let other friends how happy to play, second speed error – ING…

Note: Run the top command to check which process occupies too much CPU and return the process number.

(2) through the ps H – eo pid, dar, % CPU | grep command to filter the contents of the task manager.

Note: ps H – eopid, dar, % CPU | grep, is through the ps command to see which thread CPU is too high, returns the process id, the pid is the process id, dar for thread id, % CPU as the CPU;

Found the culprit, this jumpy red…

(3) Check the specific situation of the faulty process 20389 through the JStack process ID.

Note: The jstack process ID is used to locate the specific code that occupies too much CPU by using the jstack command. Note that the thread ID searched by the jstack command is hexadecimal and needs to be converted.

There’s a bunch of executing code, so how do we figure out which guy is doing it? In the figure above we can see that the thread is 20441, so let’s use our calculator to convert 20441 to hexadecimal 4FD9 and try again. There is only one truth.

By comparing nID (thread ID) 4fd9, we see that the thread called thread1 is running (RUNNABLE state) and that the location is line 11 of the stack_6.java file.

Now back in the source code, at line 11 of the Stack_6 file, we see that we have been executing an endless loop

4. JVM memory structure – local method stack

4.1 define

Sometimes Java itself cannot interact with the bottom layer of the operating system directly, but sometimes Java needs to call local C or C++ methods, so the local method stack comes into being at this time, which is a class of methods with native keywords.

5. JVM memory structure – heap

5.1 define

Heap Heap: Objects created by the new keyword are stored in the Heap

5.2 the characteristics of

5.2.1 Thread Sharing

Objects stored in the heap are shared by threads, so there are thread safety concerns.

5.2.2 Garbage collection mechanism

Because objects in the heap can hold a large number of objects, it is equipped with a small helper – garbage collection mechanism (can be set to automatic and manual transmission oh ~).

5.3 Small experiment of overflow of heap memory

5.3.1 Small experiment to modify heap memory size parameters

Continue to imagine a scenario, when a big guy developed a piece of code (of course, the general big guy is very confident, how can THERE be problems with my code, there is no…). But the test won’t go away. Just to be on the safe side, let’s try it quietly. Safety first. But the machine’s memory is so big, big guy certainly ran many times, did not appear the problem, this is not to find fault… Let’s change the parameters and try again.

-xmx: One of the JVM tuning parameters that represents the maximum size to which the Java heap can be extended. Here is the case:

After 26 executions, a bold background heap overflow error was reported.

Now, what happens if you try scaling the heap down to 8m with the -xmx JVM tuning parameter?

The operation is basically the same as when the stack ran out of memory case, the number of times is significantly smaller, only 17 calls before the heap ran out of memory error.

5.3.2 Small experiment of heap memory diagnosis

JPS tool: View which Java processes exist in the current system

Jmap: Check the heap memory usage. Jmap-heap process ID

Jconsole tool: graphical tool with multifunctional monitoring capabilities for continuous monitoring.

Let’s look at heap memory usage by running the code and using the JConsole visual graphical tool.

As we can see in the figure above, memory usage goes up when we create a 10MB array object; Then, after we manually invoked the garbage collection mechanism, the memory was freed significantly again.

6. JVM memory structure – method area

6.1 define

The Java Virtual machine has a method area that is shared by all JVM threads. Method areas are somewhat similar to compiled code blocks in traditional programming languages or operating system-level code segments. It stores construction information for each class, such as runtime constant pools, fields, method data, and method and constructor code, including special methods used during class and instance initialization and interface initialization.

A method area, also known as a non-heap, is an area of memory that is independent of the heap. It is a concept defined in the JVM specification for storing data such as class information, constant pools, static variables, and JIT-compiled code.

6.2 the characteristics of

(1) The method area, like the Java heap, is an area of memory shared by each thread;

(2) The method area is created when the JVM starts;

(3) The size of the method area, like the heap space, can be fixed or expanded;

(4) The size of the method area determines how many classes the system can hold. If the system defines too many classes, resulting in an overflow of the method area, the VM will also throw an OutOfMemoryError;

(5) Shutting down the JVM frees memory in this area.

6.3 Schematic diagram of JVM memory Structure

In JVM memory structure 1.6, the method area was stored in a memory structure called the persistent generation, which stored the constant pool (including the string pool, StringTable) at runtime, the class information, and the classloader.

In JVM memory structure 1.8, the method area was a concept stored in local memory, called a meta-space, which stored the constant pool, class information, and class loaders at run time. In this case, the StringTable pool was stored in the heap.

Click to follow, the first time to learn about Huawei cloud fresh technology ~