[JVM Basics] JVM Basics

The location of the JVM

The application (Java application) runs on the JRE (JRE contains the JVM), the JRE runs on the operating system (Windows, Mac), and the operating system runs on the hardware architecture (Intel, Spac...).

Three JVMs

  • Sun: HotSpot is the most used (we use)
  • BEA:JRockit
  • IBM:J9VM

JVM architecture

JVM tuning: 99% are in the method area and heap, most of the time the heap is tuned. JNI (Java Native Interface): native method interface
insert image description here
insert image description here

class loader

Function: load the class file
For example: new Student();(the specific instance is in the heap, and the reference variable name is placed in the stack)

  • The loader that comes with the virtual machine
  • Start the class (root) loader
  • extension class loader
  • application loader

insert image description here
insert image description here

Parental Delegation Mechanism

concept

When a class loader needs to load a certain .class file, it first entrusts this task to its superior class loader, and recursively performs this operation. If the superior class loader does not load it, it will load the class itself.

example

Hello.classWhen such a file is to be loaded.
Regardless of our custom class loader, we will first check whether it has been loaded in AppClassLoader , and if so, there is no need to load it again. If not, then the parent loader will be obtained, and the loadClass method of the parent loader will be called .
In the same way, the parent class will first check whether it has been loaded, and if not, go up. Note that this recursive process, until it reaches the Bootstrap classLoader , is checking whether it has been loaded, and will not choose to load it by itself.
Until BootstrapClassLoader , there is no parent loader. At this time, I start to think about whether I can load it. If I can’t load it, I will sink to the child loader to load it, until it reaches the bottom. If there is no loader that can load it, it will throw ClassNotFoundException.

insert image description here

effect

  • 1. Prevent repeated loading of the same .class. Ask the above through entrustment, after loading, there is no need to load it again. Ensure data security.
  • 2. Ensure that the core.class cannot be tampered with. Through delegation, the core .class will not be tampered with, even if it is tampered with, it will not be loaded, and even if it is loaded, it will not be the same .class object. Different loaders load the same .class and are not the same Class object. This ensures that the Class execution security.

For example: If someone wants to replace the system-level class: String.java.
Tampering with its implementation, under this mechanism, the classes of these systems have been loaded by Bootstrap classLoader (why? Because when a class needs to be loaded, the first thing to try to load is BootstrapClassLoader), so other class loaders are not The opportunity to load again prevents the implantation of dangerous codes to a certain extent.

Sandbox Security Mechanism

insert image description here
insert image description here
insert image description here
insert image description here

Basic components that make up a sandbox

  • The bytecode verifier (bytecode verifier)
    ​​ensures that the Java class file .Class follows the Java language specification. This helps Java programs achieve memory protection. But not all class files will undergo bytecode verification, such as core classes.
  • Class loader (class loader)
    where the class loader works on the Java sandbox in three ways:
    • It prevents malicious code from interfering with good-faith code; //Parent delegation mode
    • It guards the boundaries of trusted libraries;
    • It puts the code into protection domains, which determines what the code can do.

The virtual machine provides different namespaces for classes loaded by different class loaders. The namespace consists of a series of unique names. Each loaded class will have a name. This namespace is created by the Java virtual machine for each maintained by the class loader, they are not even visible to each other.

The mechanism used by the class loader is the parental delegation model .

1. Start loading from the class loader of the innermost JVM, and the outer malicious class with the same name cannot be loaded and cannot be used;

2. Since the access domain is strictly distinguished by the package, the malicious class in the outer layer cannot gain permission to access the inner class through the built-in code, and the damaged code will naturally fail to take effect.

  • Access controller (access controller): The access controller can control the access authority of the core API to the operating system, and the policy setting of this control can be specified by the user.
  • Security manager (security manager): is the main interface between the core API and the operating system. To implement permission control, it has a higher priority than the access controller.
  • Security package: the classes under java.security and the classes under the extension package, allowing users to add new security features to their applications, including:
    • security provider
    • message digest
    • Digital signature keytools https (certificate required)
    • encryption
    • identify

Native

Those with the native keyword indicate that the scope of Java cannot be reached, and you have to go back to call the underlying C language library.
Any method with the native keyword will enter the local method stack , and the others are the Java stack

JNI: Java Native Interface (local method interface)

The function of calling the local method interface (JNI):

Expand the use of Java and integrate different programming languages ​​for Java. The
original intention of Java was to integrate C/C++ programs. C and C++ are rampant. If you want to gain a foothold, you must have programs that call C and C++. It is specially developed in the memory area city. Block Marking District City: Native Method Stack

Native Method Stack

Register the native method, when the execution engine (Execution Engine) executes. Load methods in **Native Libraries** via JNI (Native Method Interface).

Rare in enterprise-level applications, hardware-related applications: Java program-driven printers, system management production equipment, etc.

PC Register (Program Counter Register)

Program Counter: Program Counter Register

Each thread has a program counter, which is private to the thread , and is a pointer to the method bytecode in the method area (used to store the address pointing to the next instruction, which is also the instruction code to be executed), in the execution engine Reading the next instruction is a very small memory space, almost negligible.

Method Area

The method area is shared by all threads . All fields and method bytecodes, as well as some special methods, such as constructors, interface codes are also defined here. Simply put: all defined method information is stored in this area, which belongs to shared space;

Static variables, constants, class information (construction method, interface definition), runtime constant pool (such as: static, final, Class (class template), constant pool) are stored in the method area, but instance variables are stored in the heap memory . It has nothing to do with the method area.

Stack (Java Stack)

Why main() is executed first and ends at the end: (because main() is pushed onto the stack first)

Stack: stack memory, the running of the supervisor program, life cycle and thread synchronization.
When the thread ends, the stack memory is released. For the stack, there is no garbage collection problem.

Stack storage: 8 basic types + object reference + instance method.
Stack operation principle: stack frame (local variable table + operand stack) has a stack frame for each method called.
The stack is full and main() cannot end, an error will be thrown: stack overflowStackOverflowError

insert image description here

Stack + heap + method area: interactive relationship

insert image description here

Heap

A JVM has only one heap memory, and the size of the heap can be adjusted.
After the class loader reads the class file, it generally puts classes, methods, constants, variables , and real objects that save all reference types into the heap.

The heap memory is subdivided into 3 areas:

  • New District (Eden District) Young / new
  • retirement area old
  • Permanent area Perm, after JDK8, the permanent storage area has changed its name (metaspace)

GC garbage collection, mainly in Eden Park and retirement areas.
insert image description here
Assuming that the memory is full, an error OOM is reported: not enough heap memoryOutOfMemoryError:Java heap space

//-Xms8m -Xmx8m -XX:+PrintGCDetails
public static void main(String[] args) {
    
    
    String str = "javajavajavajava";

    while (true){
    
    
        str += str + new Random().nextInt(888888888)+ new Random().nextInt(21_0000_0000);
    }
}
//OutOfMemoryError:Java heap space 堆内存满了

insert image description here

Newborn area (Garden of Eden + Survivor area*2)

  • A place where classes are born and grow and even die
  • Garden of Eden, all objects are newly created in the Garden of Eden
  • Survivor area (from, to), the light GC regularly cleans up the Garden of Eden, and those who survive are put into the survivor area. After the survivor area is full, the heavy GC cleans up the Eden+survivor area , and the survivors are put into the retirement area. If they are full, report OOM.

Note: After research, 99% of the objects are temporary objects! directly cleaned up

Elderly area

The rest of the new area, light GC can not kill

permanent area

This area is resident in memory and is used to store the Class objects and Interface metadata carried by the JDK itself. It stores some environment or class information of the Java runtime. There is no garbage collection GC in this area . Shutting down the virtual machine frees this memory.

  • Before jdk1.6: the permanent generation, the constant pool is in the method area .
  • jdk1.7: permanent generation, but slowly degenerates (to the permanent generation) the constant pool is in the heap .
  • After jdk1.8: There is no permanent generation, and the constant pool is in the metaspace .

The constant pool has always been in the method area, and the string pool in it was saved in the heap after JDK1.7.

Permanent area OOM example: a startup class that loads a large number of third-party jar packages. Tomcat deploys too many applications, a large number of dynamically generated reflection classes. Constantly being loaded. Until the memory is full, OOM will appear.

The method area is also called non-heap (non-heap), which is still a heap in essence, just to distinguish concepts.

Metaspace exists logically, but not physically.

Heap memory tuning

public static void main(String[] args) {
    
    
    //返回虚拟机试图使用的最大内存
    long max = Runtime.getRuntime().maxMemory(); //字节 1024*1024
    //返回jvm初始化的总内存
    long total = Runtime.getRuntime().totalMemory();

    System.out.println("max="+max+"字节\t"+(max/(double)1024/1024+"MB"));
    System.out.println("total="+total+"字节\t"+(total/(double)1024/1024+"MB"));
    /* 运行后:
    max=1866465280字节   1780.0MB
    total=126877696字节  121.0MB
     */
    //默认情况下,分配的总内存占电脑内存1/4 初始化1/64
}

How to report OOM?

  • 1. Try to expand the heap memory. If the error is still reported, it means that there is an infinite loop code or garbage code.
    Edit Configration>add VM option> Input: -Xms1024m -Xmx1024m -XX:+PrintGCDetails
    insert image description here
    newborn area + old age area: 305664K+699392K=1005056K = 981.5M, indicating that metaspace physics does not exist .

  • 2. Analyze the memory to see where there is a problem (professional tool)
    to see the error line of the code: memory snapshot analysis tool, MAT, Jprofiler
    MAT, Jprofiler function:

    • Analyze Dump memory files to quickly locate memory leaks;
    • get the data in the heap
    • get large object
//-Xms 设置初始化内存分配大小 默认1/64
//-Xmx 设置最大分配内存,默认1/4
//-XX:+PrintGCDetails 打印GC垃圾回收信息
//-XX:+HeapDumpOnOutOfMemoryError //oom DUMP
//-Xms1m -Xmx8m -XX:+HeapDumpOnOutOfMemoryError
public class Demo03 {
    
    
    byte[] array = new byte[1*1024*1024]; //1m

    public static void main(String[] args) {
    
    
        ArrayList<Demo03> list = new ArrayList<>();
        int count = 0;
        try {
    
    
            while (true){
    
    
                list.add(new Demo03()); //不停地把创建对象放进列表
                count = count + 1;
            }
        } catch (Exception e) {
    
    
            System.out.println("count: "+count);
            e.printStackTrace();
        }
    }
}

insert image description here
insert image description here

GC (garbage collection)

insert image description here
When the JVM performs GC, it does not uniformly recycle the three areas of the new generation, the survivor area, and the old area. Most of the time, the new generation is recycled

Two types of GC: light GC, heavy GC (Full GC, global GC)

reference counting

Generally JVM is not used, there are too many large project objects
insert image description here

copy algorithm

-XX:MaxTenuringThreshold=15Set the number of survival conditions for entering the old age.
insert image description here
insert image description here
Advantages: no memory fragmentation, high memory efficiency
Disadvantages: memory space is wasted (a survivor area is always empty); assuming that the object is 100% alive, the cost of copying is high.
The best use scenario for the replication algorithm: when the object survival is low, the new area.

mark clear

insert image description here
Advantages: No additional space is required, and the copy algorithm is optimized.
Disadvantages: Two scans, a serious waste of time, will generate memory fragmentation.

Tag Compression (Mark Cleanup): Reoptimization

Trilogy: Mark – Clear – Compress
insert image description here

Mark Sweep Compression: Reoptimization

Compress every time the mark is cleared, or if memory fragmentation accumulates to a certain extent.

Generational Collection Algorithm

According to the life cycle of the memory object, the memory is divided into several pieces, and the JVM generally divides the memory into the new generation and the old generation.
In the new generation, a large number of objects die and a small number of objects survive, so the copy algorithm can be used to complete the collection only by paying the cost of copying a small number of surviving objects; in the old generation, because the survival rate of objects is extremely high, there is no additional space for
them Allocation guarantee is carried out, so mark cleanup or mark sorting algorithm is used for recycling;
insert image description here

Summarize

Memory Efficiency: Replication Algorithm > Mark Sweep Algorithm > Mark Compression Algorithm (Time Complexity)

Memory uniformity: copy algorithm = mark compression algorithm > mark removal algorithm

Memory Utilization: Mark Compression Algorithm = Mark Clear Algorithm > Copy Algorithm

There is no best algorithm, only suitable algorithm (GC is also known as generational collection algorithm).

  • Young Generation: The survival rate is low, and the replication algorithm is used.
  • Old generation: high survival rate, large area, mark-clear-compress.

Guess you like

Origin blog.csdn.net/qq_44033208/article/details/132470273
JVM
JVM