I heard that wechat search “Java fish” will change strong oh!

This article is posted on Github and Gitee, and contains my entire Java series of articles for study and interview purposes

(1) Introduction to basic concepts

The JVM’s memory model can be divided into method areas, virtual machine stacks, local method stacks, heaps, and program counters.

First, the basic concepts

Program counter:

A program counter can be thought of as a line number indicator of the bytecode being executed by the current thread. The bytecode interpreter works by changing the value of the counter to select the next bytecode instruction to execute. Multithreading in the Java virtual machine is implemented by switching threads in turn to allocate processor execution time, and each thread needs an independent program counter in order to return to the correct execution position after switching.

Java Virtual Machine stack: The Java virtual machine stack thread is private, and each method executed creates a stack frame to hold information about local variables, operand stacks, dynamic links, method exits, and so on.

Local method stack: The function of the local Method stack is similar to that of the virtual machine stack. The local Method stack serves the Native methods used by the VIRTUAL machine. A Native Method is a Java interface to call non-Java code

Java heap The Java heap is arguably the largest chunk of memory managed in the Java Virtual machine. The Java heap is shared by all threads and is created when the virtual machine starts. An array of object instances held in the Java heap. Almost all object instances and arrays are allocated on the heap. The Java heap is also the main area managed by the garbage collector. The Java heap can be in a physically discontinuous memory space, as long as it is logically continuous, much like disk space. OutOfMemoryError is thrown when a heap can no longer be extended.

Methods area: the area is also a thread Shared memory region, used for storing virtual machine has been loaded the class information, static variables, constants, and the real-time compilers compiled code data, such as the Java virtual machine is very easy to approach the limit, as well as the Java heap does not require continuous memory, also can choose not to realize garbage collection.

(2) Java virtual machine stack and program counter

The main content stored in the virtual machine stack is the stack frame, and each method call pushes one stack frame onto the virtual machine stack. For example, in our last article on JVM bytecode, we used code like this:

public class Main {
    public static int calculate(a){
        int a=1;
        int b=2;
        int c=(a+b)*10;
        return c;
    }
    public static void main(String[] args) { System.out.println(calculate()); }}Copy the code

In the JVM stack, the structure looks like this:

The local variable table holds the eight basic data types, object references, and returnAddress types known to the compiler (which refer to the address of a bytecode instruction).

The operand stack is mainly used to perform a series of numerical operations in and out of the stack.

For a more detailed application of local variable tables, operand stacks, and program counters, see my previous article: two diagrams for quick reading of JVM bytecode instructions

Dynamic linking: Dynamic linking is a value that converts symbolic references to direct references during program execution. In a piece of code, class names, constant names, modifiers, object names and so on are symbolic references, and how to find specific references through symbols requires a layer of dynamic linking to convert symbolic references into direct references. For example, the JVM can link to real objects in the heap by object name.

Dynamic linking works the same as parsing during class loading, except that parsing replaces static methods or variables (such as main() methods, static variables) with direct references to the memory in which the data is stored.

Method exit: When a method is executed, the return may be either at the end of normal execution or as an exception thrown. Either way, you need to go back to the method invocation after the method exits, so this information is stored in the method exit.

In JVM mode, the stack size is 1 MB per thread. You can also adjust the stack size using -xss.

(3) Method area

A method area is an area of memory shared by threads that holds information about classes that have been loaded by the virtual machine, constants, static variables, and code compiled by the just-in-time compiler. Many people are confused about the concept of method areas. Does everyone know the difference between a method region, a permanent generation, and a metaspace?

The method area is a concept in the JVM specification and has its own implementation across different Java virtual machines and versions of the Java Virtual machine. In the case of the most popular HotSpot virtual machine, prior to JDK1.8, the method area implementation was called the persistent generation and was placed in JVM memory. In JDK1.8, the method area implementation becomes a meta-space and is placed in direct memory.

(4) Local method stack

The native method stack is used to invoke non-Java code in Java and functions like a virtual machine stack. The existence of the local method stack should be a legacy of history. When Java first came out, C and C++ were the absolute rulers at that time, so it was inevitable to call this kind of code in Java, and therefore the need for the local method stack.

(5) of the heap

The heap is the core part of the JVM, and most garbage collection is done in the heap.

The JVM heap is divided into new generation and old generation, and the default ratio is 1:2. The new generation is divided into Eden region and two survivor regions, and the ratio is 8:1:1. I’ve covered most of the heap in the garbage collection section.

(6) Summary

When it comes to really understanding the JVM, it’s not as difficult as you might think. A JVM actually contains a few of these things. Finally, draw a graph to illustrate where common JVM parameters are targeted.