[JVM] JVM memory model (details)

1. JVM overview

1. Introduction to jvm

JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine). JVM is a specification for computing devices. It is a fictitious computer that is realized by simulating various computer functions on an actual computer.

2. The role of jvm

All classes in Java must be loaded into the JVM to run. This loading work is done by the class loader in the jvm. The type of .class can run in the virtual machine, but it does not directly interact with the operating system and needs to be interpreted by the jvm For the operating system, a java class library is required for explanation, so that it can interact with the operating system.

3. The memory model of jvm

insert image description here




2. Class loader

1. The role of the class loader

Load the class bytecode content into the memory, and convert these static data into the runtime data structure of the method area, and then form a java.lang.Class object representing this class in the heap, as the access entry of the class data in the method area . The essence of the work done by the class loader is to read the class file from the hard disk into the memory.

2. Types of loaders

引导类加载器(Bootstrap ClassLoader): Written in c++, it is the class loader that comes with the JVM. It is responsible for the core library of the java platform and is used to load the core class library. The loader cannot be directly obtained.
拓展类加载器(Extension ClassLoader): responsible for the jar package under the jre/lib/ext directory or the jar package specified by -D java.ext.dirs into the working library.
系统类加载器(Application ClassLoader): Responsible for loading classes and jar packages under the directory pointed to by java-classpath or -D java.class.path, and is the most commonly used loader.
自定义类加载器(Custom ClassLoader): Defined by the developer himself.

insert image description here
insert image description here

3. The operation process of the parent delegation mechanism

① The class loader receives a class loading request.

② Delegate this request to the parent class loader to complete, and delegate it upward until the bootstrap class loader.

③ The bootstrap class loader checks whether the current class can be loaded, and ends when it can be loaded, and uses the current loader, otherwise, throws an exception, notifies the sub-loader to load, and loads down.

④ Repeat step ③.

4. Advantages and disadvantages of the parental delegation mechanism

Advantages: Ensure the safety of class loading. No matter which class is loaded, it will be entrusted to the bootstrap class loader. Only when the class loader cannot be loaded, will the subloader be loaded, so as to ensure that the final object is the same one. .
Disadvantage: The child loader can use the classes loaded by the parent loader, and the parent loader cannot use the classes loaded by the child loader.

5. Why destroy the parental delegation mechanism

Reason: Child loaders can use classes loaded by parent loaders, but parent loaders cannot use classes loaded by child loaders.
Example: To use JDBC to connect to the database, you need to use com.mysql.jdbc.Driver and DriverManager classes. However, DriverManager is loaded by the boot class loader, and com.mysql.jdbc.Driver is loaded by the loader of the current caller, which cannot be loaded by using the boot class loader, so the parental delegation mechanism must be broken.

6. Ways to break the parental delegation mechanism

(1) Customize the class loader and override the loadclss() method.
(2) Use the thread context class (ServiceLoader: a class that enables the parent loader to load the child loader).

3. JVM memory module

1. Method area (thread sharing)

The method area stores: constant pool, static variables (static), method information (modifiers, method names, return values, parameters, etc.), class information (class variables), etc.

2. Heap (thread sharing)

Concept: The heap is a memory area shared by threads, and it is the largest piece of virtual machine managed memory.

What the heap stores are: instance objects.
For example, A a1 = new A(); a1 is the instance object. A a2;a2 is the class object.

The specific schematic diagram of the heap (jdk8 and later)

GC主要在新生区(伊甸园区)、老年区
	新生区(伊甸园区(对象都是在这个区new出来的)、幸存区to、幸存区from:幸存区位置会互相交换,谁空谁是to)
	老年区
	永久区:存储的是java的运行环境或类信息,这个区域不存在垃圾回收,关闭jvm就会释放内存
			一个启动类加载大量的jar包。tomcat部署太多应用。内存满了就oom
			jdk1.6之前:永久代,常量池是在方法区
			jdk1.7去永久代,常量池在堆中
			jdk1.8之后:无永久代,常量池在元空间中

insert image description here

3. Stack (virtual machine stack-thread isolation)

Concept: Also known as the stack, the supervisor program runs, the life cycle is synchronized with the thread, and the stack memory is released when the thread ends. There is no garbage collection problem.

The virtual machine stack stores: 8 basic types + object reference + instance method

The specific schematic diagram of the stack
insert image description here

4. Stack (local method stack-thread isolation)

The local method stack stores: the method called in the local interface library is nativethe keyword modified method in java.
Anything with the native keyword means that the scope of java cannot be reached. Going back to call the library of the underlying c/c++ language, it will first enter the native method stack, and then go to the native method interface.

5. Program counter (thread isolation)

When each thread starts, a program counter is created, which saves the jvm instruction being executed, and the program counter always points to the address of the next instruction to be executed. The life cycle is consistent with the life cycle of the thread.

6. OutOfMemoryError memory overflow and StackOverFlowError stack overflow and solutions

Whether it is a memory overflow or a stack overflow, simply speaking, you have put too much space and there is not enough space, and the overflow has overflowed, so it is easier to understand.

(1) OutOfMemoryError memory overflow (OOM)

Reasons : ①It means that there is memory that cannot be reclaimed or too much memory is used in the application system, which eventually makes the memory used by the program running larger than the maximum memory that can be provided.
   ②Due to long-term references to certain resources, the garbage collector cannot reclaim them, so that the resources cannot be released in time, also known as memory leaks.

Solution : ① Set the heap parameters of the JVM ( -Xmx: JVM maximum memory -Xms: startup initial memory -Xmn: new generation size -Xss: virtual machine stack and stack size for each thread) For example: -Xms1024m -Xmx1024m -Xmn512m -Xss5m.
   ②Analyze the memory and see where there is a problem (professional tool: Jprofiler, MAT) analyze the Dump memory file, quickly locate the memory leak, how to find the dump file, directly find the folder of the file and open it to get a large object.

Create a heap overflow : the new object is ok if it keeps looping forever.

  • –Xms: The heap memory initially allocated by the JVM, which is 1/64 of the physical memory by default.
  • –Xmx: The maximum heap memory that the JVM allows to allocate, the default is 1/4 of the physical memory.
  • –Xmn: The size of the new generation in the heap. The size of the old generation can also be obtained through this value: -Xmx minus -Xmn.
  • –Xss: Specifies the size of each thread virtual machine stack and stack. Generally, 256k is enough. This configuration will affect the number of concurrent threads in this process.
  • More JVM tuning summary and commands

(2) StackOverFlowError stack overflow

Reason : The stack capacity allocated by the thread request exceeds the maximum capacity allowed by the Java virtual machine stack.

Solution : ① modify the code ② increase the thread stack size (-Xss).

Create a stack overflow : Just call the method in an endless loop.

(3) idea placement jvm

① Configure a project

insert image description here
insert image description here

②Global configuration

insert image description here

Another article: Detailed explanation of garbage collection mechanism

Guess you like

Origin blog.csdn.net/twotwo22222/article/details/127890270