当前位置:网站首页>Introduction to JVM basic concepts

Introduction to JVM basic concepts

2022-06-11 15:23:00 Evader1997

JVM What is it?

  JVM also called Java virtual machine , yes Java The runtime environment of the program . It is to use C Write a software , Like other software, it runs on the operating system (LInux,win,mac etc. ) Of . Where does the operating system run ?

   Before the birth of computer software , Our computer can only execute certain programs , To run a program by splicing circuits . If you need to run the second program, you need to splice a new set of circuits and run , This is the early computer hardware . Later, von Neumann led to the wave of computer software , It wants to **‘ preservation ’** get up , This is the beginning of computer software . So the operating system is running on the hardware .JVM, operating system , The relationship among computer hardware is :
 Insert picture description here

JVM Architecture of

  JVM The most common thing in is tuning , In fact, JVM Of the five areas of , The basic thing that involves tuning is heap memory tuning , This is determined by the mechanism of heap memory . For local method interfaces , Namely native Related methods of modification , These methods are C language-written . The local method information is stored in the local method interface , When called, the local method in the local method library will be called .
 Insert picture description here

Class loader

species

  • Start class loader :Bootstrap ClassLoader

  • Extend the classloader :Extension ClassLoader

  • Application class loader :Application ClassLoader

    Do not discuss custom class loaders

A simple loading process of a class

 Insert picture description here

Test code , You can understand at a glance

![1](1.gifpublic class ClassLoaderTest {
    
    public static void main(String[] args) {
    
        String str = new String("abc");
        System.out.println(str.getClass().getClassLoader());
// System.out.println(classLoader.getParent());
        System.out.println("====== I'm the divider ======");
        Person person = new Person();
        System.out.println(person.getClass().getClassLoader());
        System.out.println(person.getClass().getClassLoader().getParent());
        System.out.println(person.getClass().getClassLoader().getParent().getParent());
    }
}

 Insert picture description here
 Insert picture description here

   Ladies and gentlemen, just look at two moving pictures , We know String Class is JDK A self-contained class in , Is loaded by the boot class loader , Start class loader at Java You can't get it in . Therefore, the second dynamic graph generates a null pointer exception . Compare... In the first graph Peron Class is custom , So the loading is finally completed by the application loader . Why print three class loaders , Leave a suspense .

The role of the three class loaders

1. Start class loader : load <JAVA_HOME>\lib In the directory , Or be -Xbootclasspath The class of the path specified by the , And is JVM Identified class libraries , Users can't get .

2. Extend the classloader : load <JAVA_HOME>\lib\ext In the directory , Or be java.ext.dirs Class libraries in the path specified by the system variable . Users can get .

3. Application class loader : This loader is also known as the system loader (System), yes ClassLoader in getSystemClassLoader() Return value of method . Load user path (ClassPath) The specified class library . Users can use . Users can get

Parent delegate mechanism

  1. There is a need for class loading

  2. The subclass loader passes the request for class loading to the parent class , Layer by layer to start the class loader

  3. If the parent class loader fails to load, it will feed back to the child class to load itself

    The so-called parents , Is to extend the class loader and start the class loader . The advantage of parental delegation is that it doesn't make a class reload twice , For example, you can customize java.lang.String It's illegal .

Sandbox security mechanism

   A sandbox can be understood as a closed box , Our program can only run in this box . We develop locally , The development is completed and deployed to the remote server , stay Java Local code in is trusted by default , It's safe . But remote code is not trusted , So we need to add some security mechanisms to ensure the operation of the program , For example, when the remote code operates the operating system and local resources, the credit granting steps should be added ,jdk6 Then the domain is proposed (domain) The concept of , Make sandbox safer .

Native

 Insert picture description here

  native The delegate is a local method , take Thread Class, for example , First Thread Not an abstract class, not an interface , Why is there a way to realize without writing methods ? The reason is that this method is native Modify the . stay Java in , All are native The modification is the local method .Java Only its mark is stored , Use it to call the local method library .

PC register

  PC The register area is also called the program counter , We know that in the case of multithreading ,CPU It's a high-speed wheel , Each thread is allocated a time slice , There is ** Half the program is executed to execute other threads , Next time, go back to this thread ,CPU How to know where the program has been executed ?** The answer is the program counter , This can be understood as a pointer , It will point to the memory address of the next instruction .PC Registers are thread private , Every thread has a PC register , This ensures that threads are independent of each other .

Stack

   Stack also known as Java Virtual machine stack , It is characterized by first in and last out , This is related to its data structure . Inside the stack are stack frames , Each stack frame actually corresponds to a method to be executed . After each method is executed, it will pop up and disappear , The sign that a program has finished executing is that there is no stack frame in the stack . Usually, what is stored in the stack is some references , So the program is finished , There is basically no garbage left in the stack , So it basically won't appear OOM. The possible exception is that the stack frame loaded into the stack exceeds the maximum stack frame that the stack can hold , A stack overflow exception will be reported , Another case is that the memory requested by the current stack frame exceeds the maximum memory , Will appear OOM The problem of .
 Insert picture description here

Pile up

  Java in new All objects are stored in the heap , The so-called garbage collection is the collection object . So most of the tuning we talk about is actually heap tuning . The following is the structure diagram of heap memory .

 Insert picture description here

原网站

版权声明
本文为[Evader1997]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111518260189.html