Deep understanding of JVM study notes

In-depth understanding of the study notes of the JVM virtual machine

After reading the JVM twice, many places still seem to understand

Combine some articles in the book and reorganize

It is good to understand the book of jvm in depth, but it feels that it has his limitations.

Organize the core

The talk is chaotic and there is no point.

The difference between jdk 1.7 and 1.8 is still very large.

General preview
Insert picture description here

1. Class loading mechanism

Class loading mechanism is always unclear, there are many interview questions like loading test class

5 processes

  • load

    1. To load a class, first find the binary byte stream of this class by its fully qualified name
    2. Convert the static structure of the byte stream into the runtime structure of the method area
    3. Generate a class object in memory as an entry to the method area access, such as reflection requires a class object
  • verification

    Is to verify the reasonableness including

    File format, version number and the like

    Metadata, are there any that do not conform to the Java language specification

    Bytecode, to ensure some conversion problems, the instructions are correct

  • ready

    Initialize the value of the static member variable to the default value

  • Parsing

    The symbol reference becomes a direct reference. Simply put, it is a pointer

    • Class and interface resolution

      Analysis of category C, divided into two cases

      1.C is not an array type and may need to load the parent class and interface

      2.C is an array type, and the element is an object, first load the object such as Integer

    • Class method analysis

      1. Check the name directly, and return to the reference

      2. Not checked in the parent class

      3. Check in the interface and the parent interface, if there is an abstract class

  • initialization

    Initialize member variables and static code blocks

    Combine the knowledge in JAVA programming ideas

    The order of execution is

    Parent class initialization> Child class initialization

    Static variables Static code blocks (see writing order at the same level) Initialization> Common member variables (code blocks)> Constructor

    Look at the code

    package test;
    
    public class Father {
        //------- 这两个顺序只和书写顺序有关
        private static String staticfiled = getStaticfiled();
        static {
            System.out.println("父类->静态代码块");
        }
        // -------
    
        private String field =getNormalfiled();
    
        public String getNormalfiled(){
            System.out.println("父类->普通成员变量初始化");
            return "hah";
        }
        public static String getStaticfiled(){
            System.out.println("父类->静态成员变量初始化");
            return "haha";
        }
        // 普通方法块
        {
            System.out.println("父类->普通方法块");
        }
        public Father(){
            System.out.println("父类->构造函数");
        }
    
    
    }
    
    
    package test;
    
    public class Son extends Father {
        //------- 这两个顺序只和书写顺序有关
        private static String staticfiled = getStaticfiled();
        static {
            System.out.println("子类->静态代码块");
        }
        // -------
    
        private String field =getNormalfiled();
    
        @Override
        public String getNormalfiled(){
            System.out.println("子类->普通成员变量初始化");
            return "hah";
        }
        public static String getStaticfiled(){
            System.out.println("子类->静态成员变量初始化");
            return "haha";
        }
        // 普通方法块
        {
            System.out.println("子类->普通方法块");
        }
        public Son(){
            System.out.println("子类->构造函数");
        }
        // 在子类中main函数 是仅此慢于静态代码块和静态变量
        public static void main(String[] args) {
            System.out.println("在子类中的main主函数");
            Son son = new Son();
        }
    }
    

    Look at the result graph:
    Insert picture description here

    Determine the order:
    Insert picture description here

2. Class loader

Differentiating whether two classes are the same depends on whether the class loaders are the same and the classloader

Insert picture description here

Parent delegation model

The parent delegation model is such a structure

It is said that parents are not parents, because they are not inherited, but combined.

The class loader receives the request to load the class,

First let the parent class loader try to load,

The parent class loader receives the request and cannot hand it over to the child class loader

This is the opposite of dns addressing. If dns cannot find the IP, it is handed over to the root dns server

The difference is that the parent class of the loader does not work on the subclass

dns local dns server can not be handed over to the root dns

This is also a reason why object is the parent class of all classes

It is worth noting that the classes that have been loaded will be cached and will not need to be loaded and used next time.

3. JVM runtime memory structure

The difference between JVM runtime memory structure and JMM (JAVA memory model)

JVM runtime memory structure is in JVM, JAVA memory model is to study the relationship of multi-thread memory sharing

Here we are discussing the JVM runtime memory structure

Insert picture description here
String interview questions like to test the string constant pool and reference questions

A few classic interview questions

  1. String str = new String ("11") created several strings

    Create one or two strings, if the string constant pool has this object, only create one on the heap

    Otherwise, create one on the heap and one in the string constant pool.

  2. String's intern () method returns a string in a constant pool. If the constant pool does not exist, join the constant pool and return

4. Object memory allocation and garbage collection

Garbage collection is mainly to recycle objects in heap heap

Object memory allocation is also allocated in the heap heap

The object in the recycling Eden area is MinorGC

Recycling the old generation is FullGC

How to determine whether an object is dead or should be recycled

  • 1. Reference counting method

    It should not be recycled as long as the reference is still there

  • 2. Reachability algorithm

    This algorithm is like a binary tree, traversing down from the root node GCroot

    Objects found are useful, those not found are useless

    GCroot objects have

    The object referenced in the stack frame is the method local variable

    Static objects in the method area

    Method area constant reference object

Garbage collection algorithm

  • Mark clear

    Will cause fragmentation of memory space is not easy to organize

  • Copy (applicable to death on the eve of life)

    Divide into several blocks, copy a certain block to the same block, then clear

    In the new generation

    From Eden + from space to to space is such a process

  • Marking up (for long-lived objects)

    Mark alive to move to one end of memory

  • Generational (usually this is the case)

Every cleanup requires a reachability algorithm,

Then this requires references to all objects, for speed,

The JVM uses the OopMap data structure to know there are references to objects

At the same time, the cleanup needs to suspend all threads, which will require STW (stop the world)

Pause the thread, it will cause a security impact, for safety, the JVM is only at the security point stw

Complete GC process

Garbage collector

There are many kinds of collectors, which require a combination of different new generation collectors and old generation collectors.

  • Serial

    Single-thread serial recovery of the new generation copy, old generation mark compression, will stw

  • ParNew

    Serial multi-threaded Cenozoic parallel old generation serial Cenozoic replication, old generation mark compression

    parameter:

    -XX: + UseParNewGC ParNew collector

    -XX: ParallelGCThreads limit the number of threads

  • Parallel

    Similar to Serial, with emphasis on throughput

  • Parallel old

    Old generation, use markup

  • CMS (Old Generation) focuses on response time

    4 steps concurrent mark sweep

    The initial mark (CMS initial mark) has STW mark gcroots can reach the object short time

    Concurrent mark (CMS concurrent mark) It takes a long time to search the child nodes of gcroot

    Remark (CMS remark) has short STW time

    Concurrent sweep (CMS concurrent sweep) takes a long time

  • G1 is currently the strongest (new and old can be)

    Marking, no memory space fragmentation, the new generation and old generation are not necessarily continuous in memory

    Divide the heap into regions of the same size

    Can predict the pause time

    The cleaning process is similar to CMS

    5 steps

    1. Marker triggers minorGC

    2. Region scan

    3. Concurrent scan of the entire heap

    4. Marked with stw again

    5. Concurrent removal has stw

Published 22 original articles · Likes2 · Visits 881

Guess you like

Origin blog.csdn.net/weixin_41685373/article/details/105037810