当前位置:网站首页>JVM details
JVM details
2022-07-05 23:39:00 【Archie_ java】
JVM To explore the
- Please talk about it JVM The understanding of the ? java8 Virtual machines and previous changes update
- What is? OOM( out of memory )? What is a stack overflow (stackOverFlowError)? How to analyze ?
- JVM What are the commonly used tuning parameters ?
- How to grab memory snapshots ? How to analyze Domp file ? Do you know? ?
- Talk about JVM in , Class loader your understanding ?
1.jvm The location of
jvm Running on the operating system ,java The program runs in jvm On ,jvm use C Written
jre contain jvm
2.jvm Architecture of
The method area belongs to a special heap , It belongs to a piece in the pile .
jvm Tuning is mostly about tuning the heap .
3. Class loader
effect : load Class file
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-MkszdQEC-1644763500620)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\1623550908971.png)]
public class Car {
public static void main(String[] args) {
// Class is a template , The object is concrete
Car car1=new Car();
Car car2 = new Car();
Car car3 = new Car();
System.out.println(car1.hashCode()); //356573597
System.out.println(car2.hashCode()); //1735600054
System.out.println(car3.hashCode()); //21685669
Class<? extends Car> aClass1 = car1.getClass();
Class<? extends Car> aClass2 = car2.getClass();
Class<? extends Car> aClass3 = car3.getClass();
System.out.println(aClass1); //class Car
System.out.println(aClass2); //class Car
System.out.println(aClass3); //class Car
ClassLoader classLoader = aClass1.getClassLoader();
System.out.println(classLoader); //AppClassLoader
System.out.println(classLoader.getParent()); //ExtClassLoader
System.out.println(classLoader.getParent().getParent()); //null
//null Two possible 1. non-existent 2.Java The program cannot get
}
}
Class is a template , The object is concrete
Class loader :
1. The loader of the virtual machine
2. Start class ( root ) loader
3. Extend the classloader ExtClassLoader
4. Application Loader AppClassLoader
4. Parent delegate mechanism
Parent delegate mechanism : Security
1.APP---->EXC---->B0OT( Final execution )
B0OT
EXC
APP
1. Class loader received a class load request
2. Delegate this request up to the parent loader to complete , One Straight up , Until you start the class loader
3. Start the loader to check whether the current class can be loaded , If you can load it, it's over , Use current loader , otherwise , Throw an exception , Notify the child loader to load
4. Repeat step 3
Class Not Found That's how anomalies come about
Java Early names :C++ Java = C+±-: Get rid of cumbersome things , The pointer , memory management ~
Easy to understand parental delegation mechanism
When you surpass others a little , Others will envy you ; When you surpass others by a big margin , Others will envy you
You have to know first
In introducing the parental delegation mechanism , Have to mention ClassLoader( Class loader ). say ClassLoader Before , We need to know something about Java Basic knowledge of .
Java Is running on the Java Virtual machine (JVM) Medium , But how it works in JVM I won ? We are IDE( Integrated development environment ) Writing in the Java The source code is compiled by the compiler into **.class** Bytecode file . Then we have to ClassLoader Be responsible for putting these class The file is loaded to JVM To perform .
JVM Three layers of ClassLoader:
Bootstrap classLoader: Mainly responsible for loading the core class library (java.lang.* etc. ), structure ExtClassLoader and APPClassLoader.
ExtClassLoader: Mainly responsible for loading jre/lib/ext Some of the extended jar.
AppClassLoader: It is mainly responsible for loading the main function class of the application
Well, if there's one we wrote Hello.java Compiled into Hello.class file , How it is loaded into JVM What about the middle ? take it easy , Please read on .
Parent delegate mechanism
I opened my AndroidStudio( It's one of Google's Android Integrated development tools , be based on IntelliJ IDEA ), Under search “ClassLoader”, Then open the “java.lang” Under bag ClassLoader class . Then turn the code to loadClass Method :
public Class<?> loadClass(String name) throws ClassNotFoundException {
return loadClass(name, false);
}
// -----??-----
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
// First , Check whether it has been loaded by the class loader
Class<?> c = findLoadedClass(name);
if (c == null) {
try {
// There is a parent loader , Recursive handover to parent loader
if (parent != null) {
c = parent.loadClass(name, false);
} else {
// Up to the top Bootstrap Class loader
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
c = findClass(name);
}
}
return c;
}
In fact, this code has well explained the parental delegation mechanism , To make it easier for everyone to understand , I made a diagram to describe the flow of the above code :
It's easier for us to understand from the above picture , When one Hello.class When such a file is to be loaded . Regardless of our custom class loader , First of all, it will be in AppClassLoader in Check to see if , If so, there is no need to load . without , Then you'll get parent , Then the parent loader is invoked. loadClass Method . Similarly, in the parent class, you will first check whether you have loaded , If you don't go up again . Notice this recursive process , Until arrival Bootstrap classLoader Before , They are all checking whether they have been loaded , You don't choose to load it yourself . until BootstrapClassLoader, There is no parent loader , At this time, I began to consider whether I could load , If you can't load , Will sink to the child loader to load , All the way to the bottom , If no loader can load , Will throw ClassNotFoundException. Then some people have the following questions ?
Why design this mechanism
One advantage of this design is , If someone wants to replace a system level class :String.java. Tamper with its implementation , Under this mechanism, the classes of these systems have been Bootstrap classLoader Loaded ( Why? ? Because when a class needs to be loaded , The first thing to try to load is BootstrapClassLoader), So other class loaders don't have a chance to load , To a certain extent, it prevents the implantation of dangerous code .
A brain map is summarized as follows :
5. Sandbox security mechanism
Sandbox mechanism is to make Java Code is limited to virtual machines (JVM) Within a specific operating range , And strictly restrict code access to local system resources , Through such measures to ensure the effective isolation of the code , Prevent damage to the local system .
Java The core of the security model is Java The sandbox (sandbox) , What is a sandbox ? Sandbox is an environment that limits the running of programs . The sandbox mechanism is take Java Code is limited to virtual machines (JVM) Within a specific operating range , And strictly restrict code access to local system resources , Through such measures to ensure the effective isolation of the code , Prevent damage to the local system .
Sandbox mainly restricts access to system resources , What are the system resources ? CPU、 Memory 、 file system 、 The Internet . Different levels of sandbox have different restrictions on access to these resources .
be-all Java The sandbox can be specified when the program is running , You can customize the security policy . stay Java The execution program is divided into local code and remote code , Native code is considered trustworthy by default , Remote code is considered untrusted . Local code for credit , Access to all local resources . And for non credit remote code in the early Java In the implementation , Security depends on sandbox Sandbox) Mechanism . As shown in the figure below JDK1.0 Security model
But such a strict security mechanism also brings obstacles to the function expansion of the program , For example, when a user wants remote code to access files on the local system , It can't be achieved . So in the following Java1.1 In the version , The security mechanism has been improved , Added security policy , Allows users to specify code access to local resources . As shown in the figure below JDK1.1 Security model
stay Java1.2 In the version , Once again, the security mechanism has been improved , Added code signature . Whether it's local code or remote code , Will be set according to the user's security policy , It is loaded by the class loader into the runspace with different permissions in the virtual machine , To achieve differentiated code execution authority control . As shown in the figure below
At present, the latest security mechanism is implemented , The domain is introduced (Domain) The concept of . Virtual opportunity loads all the code into different system domains and application domains , The system domain part is responsible for interacting with key resources , And each application domain part can access all kinds of needed resources through partial proxy of system domain . Different protected domains in the virtual machine (Protected Domain), Corresponding to different permissions (Permission). Class files in different domains have all the permissions of the current domain , As shown in the figure below, the latest security model (jdk 1.6)
The basic components that make up the sandbox
● bytecode verifier (bytecode verifier) : Make sure Java Class files follow Java language norm . This can help Java Program to achieve memory protection . But not all class files are bytecode checked , For example, core classes .
● Class loader (class loader) : The class loader is in 3 In every respect Java Sandbox works
It prevents malicious code from interfering with well meaning code ; // Parent delegate mechanism
It guards the trusted class library boundary ;
It puts the code in the protection domain , Determine what the code can do .
(new Robot(); Robot man , Can operate the computer )
Virtual machines provide different namespace for classes loaded by different class loaders , A namespace consists of a series of unique names , Each loaded class will have a name , This namespace is created by Java The virtual machine maintains for each class loader , They're not even visible to each other .
The mechanism adopted by the class loader is the parental delegation mode .
1. From the innermost layer JVM The built-in class loader starts loading , The outer malicious class with the same name cannot be loaded and cannot be used ;
2. Because access domains are strictly distinguished by packages , The outer malicious class can't access the inner class through the built-in code , Breaking the code doesn't work .
● Access controller (access controller) : The access controller can control the core API Access to the operating system , And this control strategy setting , It can be specified by the user .
● Safety manager (security manager) : Is the core API The main interface to the operating system . Realize the authority control , Higher priority than access controller .
● Security package (security package) : java.security And the classes under the extension package , Allow users to add new security features to their applications , Include :
Security providers
A summary of the news
digital signature keytools https
encryption
To identify
6.Native*
Write a multithreaded class to start
public static void main(String[] args){
new Thread(()->{
},"your thread name").start();
}
Some in the start Method source code
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
boolean started = false;
try {
start0();// Called a start0 Method
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
}
}
}
// This Thread Is a class , Isn't this method definition weird here ! Look at this keyword natie
private native void start0();
Native keyword call Native Method Stack ----> JNI (java Native Interface Local method interface ) Call external language program
native : I've brought native Keywords , explain java It doesn't reach the scope of , Go back and call the underlying c The library of languages !
It goes to the local method stack
Call local methods, local interfaces JNI (Java Native Interface)
JNI effect : Open up Java Use , Integrate different programming languages into Java used ! first : C、C++
Java When it was born C、C++ transverse , Want to have a foothold , There has to be a call C、C++ The program
It creates a special mark area in the memory area : Native Method Stack, registration native Method
In the final execution , Load the methods in the local method library through JNI
for example :Java Program driven printer , Management system , Master it , It's rarely used at the enterprise level
private native void start0();
// Call other interfaces :Socket. . WebService~. .http~
Native Method Stack
Its concrete method is Native Method Stack Registered in native Method , stay ( Execution Engine ) Load when the execution engine is executing Native Libraies.[ Local library ]
Native Interface Local interface
The function of the local interface is to integrate different programming languages into Java used , Its original intention is to integrate C/C++ Program , Java When it was born C/C++ When you're walking around , Want to have a foothold , There must be calls C、C++ The program , So a special block area is opened in memory to process the mark as native Code for , Its specific approach is to Native Method Stack Registered in native Method , stay ( Execution Engine ) Load when the execution engine is executing Native Libraies. At present, this method is used less and less , Except for hardware related applications , Such as through Java Program driven printer or Java System management production equipment , It is rare in enterprise applications . Because now the communication between heterogeneous fields is very developed , You can use Socket signal communication , You can also use Web Service wait , No more introductions !
7.PC register
Program counter : Program Counter Register
Each thread has a program counter , Threads are private , It's a pointer , Point to method bytecode in method area ( Used to store addresses that point to something like an instruction , Also about to execute the instruction code ), The execution engine reads the next instruction , It's a very small memory space , Almost negligible
8. Method area Method Area
Method area is shared by all threads , All field and method bytecodes , And some special methods , Such as constructor , The interface code is also defined here , In short , Information about all defined methods is stored in this area , This area belongs to the shared area ;
Static variables 、 Constant 、 Class information ( Construction method 、 Interface definition )、 The runtime constant pool exists in the method area , But instance variables exist in heap memory , It has nothing to do with the method area
static final Class Constant pool
public class Test{
private int a;
private String name="qinjiang";
}
new Test();
9. Stack
Program = data structure + Algorithm : Continuous learning
Program = frame + Business logic : It's easy to be eliminated having dinner
Stack overflow :
Stack : First in, then out , Last in, first out
queue : fifo ( FIFO : First Input First Output )
Stack :** Stack memory , In charge of the running of the program , Life cycle and thread synchronization ; **
Thread end , Stack memory is to release , For stacks , There is no garbage collection problem
Once the thread ends , Stack on Over!
What is put in the stack :8 The big basic type + Object reference + Example method
How the stack works : Stack frame
The stack is full : StackOverflowError
Stack frame diagram
Stack + Pile up + Method area : Interaction relationship
Homework : Draw the instantiation process of an object in memory
Java The process of instantiating an object in memory
Talking about Java Before the instantiation of the object in memory , First, in the instantiation process of the class , Three areas of memory will be used : The stack area 、 Heap area 、 Method area .
Heap area :
All stored are objects , Each object contains a corresponding class Class information .
jvm There's only one heap area (steap), It will be shared by all threads , Basic data types and object references are not stored in the heap , It only stores the object itself .
The stack area :
Each thread contains a stack area , Only the values and objects of basic data types and references of basic data are saved in the stack .
The data in each stack ( References to basic data types and objects ) It's all private , Other stacks are inaccessible .
The stack is divided into three parts : Basic type variable area 、 Execution context 、 Operation instruction area ( Store operation instructions ).
Method area :
Also known as static area , It's like a pile , Shared by all threads , The method area contains all the class Information and static Decorated variable .
The method area contains elements that are always unique in the whole program , Such as :class、static Variable .
A brief talk on JDK8 Before and after JVM Memory changes
JDK8 Previously, the memory was divided into : The new generation (YOUNG)— Old age (Tenured)— Forever (PermGen)
The new generation :
The Cenozoic is subdivided into Eden (Eden) Survival zone (Survivor), Among them, the survival area is divided into two areas with the same size and space s0、s1, and s0 and s1 Can transform each other , What is preserved in the survival area must have been preserved in Eden for a long time , And after several small GC Objects that still survive , There must be two equal sized spaces in the survival area . The purpose is to promote a living area in the future , Another survival area is for the recycling of objects . It should be noted that : One of these two survival areas must be empty .
Cenozoic GC:
The size of the new generation (PSYoungGen total 9216K)=eden size (eden space 8192K)+1 individual survivor size (from space 1024K)
HotSpot JVM Divide the young generation into three parts :1 individual Eden District and 2 individual Survivor District ( Respectively called from and to). The default scale is 8(Eden):1( One survivor), In general , Newly created objects are assigned to Eden District ( Special treatment for some large objects ), These objects go through the first time Minor GC after , If it's still alive , Will be moved to Survivor District . The object is Survivor Every time I get through the area Minor GC, Age will increase 1 year , When it reaches a certain age , It will be moved to the older generation .
Because the objects of the young generation are basically living and dying (80% above ), So the garbage collection algorithm in the younger generation uses Copy algorithm , The basic idea of replication algorithm is to divide the memory into two blocks , Just use one at a time , When this block of memory runs out , Copy the living object to another piece . The replication algorithm does not generate memory fragmentation .
stay GC At the beginning , Objects only exist in Eden The district and the name are “From” Of Survivor District ,Survivor District “To” It's empty. . Then go on GC,Eden All living objects in the zone will be copied to “To”, And in the “From” In the area , Those who are still alive will decide where to go according to their age . Reaching a certain age ( Age threshold , Can pass -XX:MaxTenuringThreshold To set up ) The object of will be moved to the old generation , Objects that do not reach the threshold are copied to “To” Area . After this time GC after ,Eden Area and From The zone has been cleared . This is the time ,“From” and “To” They will exchange their roles , That's new “To” Last time GC Before “From”, new “From” Last time GC Before “To”. No matter what , Will guarantee the name of To Of Survivor The area is empty .Minor GC It will be repeated all the time , until “To” The area is filled ,“To” When the area is filled , Will move all objects to the older generation .
Why set up two Survivor District ?
Set the two Survivor The biggest advantage of the zone is that it solves fragmentation ;
Suppose there is only one survivor District , Let's simulate the process :
The newly created object is Eden in , once Eden Full of , Trigger once Minor GC,Eden Live objects in will be moved to Survivor District . This continues to cycle , The next time Eden When it's full , The problem is coming. , At this time Minor GC,Eden and Survivor Each has some surviving objects , If you put Eden The survival objects of the zone are placed in Survivor District , Obviously, the memory occupied by these two parts of objects is not continuous , It also leads to memory fragmentation .
The risk of fragmentation is enormous , serious influence Java The performance of the program . Heap space is occupied by scattered objects ** Discontinuous memory ,** The immediate result is , There is not enough contiguous memory space in the heap , Next, if the program needs to allocate memory to an object with a large memory demand ... The picture is too beautiful to see ... It's like putting everything in our backpacks next to each other when we go to school , Finally, it is possible to save a complete space for the lunch box . If everything is placed randomly with a little space between them , It's likely that you'll have to carry it all the way in the end .
1.1、Java Data types in
Java There are two types of data in :
1、 Basic types (primitive types): share 8 Kind of , namely :int、short、long、byte、char、float、double、boolean( Note that there is no String Basic type ), this 8 The definition of type in is through such as :int a = 5;long b = 22L; To define in the form of , It's called an automatic variable .
Be careful : Automatic variables store literal values , Not an instance of a class , Is not a reference to a class , There is no class here ;
Such as :int a = 5; there a It's a point int Type references , Point to 5 This is the literal value , These literal values are known by their size , The survival time is known ( These literals are defined in a block , After the block exits , The field value disappears ), For the sake of speed , These literals exist in the stack ;
in addition , Stack has a very important particularity , Data stored in the stack can be shared . Suppose we define it at the same time
int a = 3;
int b = 3;
The compiler processes int a = 3; First, it creates a variable in the stack as a References to , Then find out if there is a literal value of 3 The address of , Did not find , Just open up a store 3 The address of this face value , And then a Point to 3 The address of . And then deal with int b = 3; After creating b After referencing variables for , Because there are already 3 This is the literal value , It will be b Direct to 3 The address of . such , And that's what happened a And b At the same time, they all point to 3 The situation of .
Special note : The reference to this literal is different from the reference to a class object . Suppose two class object references point to one object at the same time , If an object reference variable modifies the internal state of the object , Then another object reference variable immediately reflects the change . contrary , Modify the value of a literal by reference to it , Does not cause the value of another reference to this literal to change . As in the above example , We're done defining a And b After the value of , Re order a=4; that ,b It's not equal to 4, Or is it equal to 3. Inside the compiler , encounter a=4; when , It will re search the stack for 4 The face value of , without , Reopen address storage 4 Value ; If there are already , Will directly a Point to this address . therefore a Changes in values do not affect b Value .
2、 Packaging data : Such as :String、Integer、Double And so on will be the corresponding basic data type wrapped up in the class , All these data are stored in Pile up in , Java use new() Statement to show the compiler , Create dynamically at run time as needed , So it's more flexible , But the disadvantage is that it takes more time .
1.2、 Changes that occur in memory when a class is instantiated
First, let's analyze the following code :
public class People{
String name; // Define a member variable name
int age; // Member variables age
Double height; // Member variables height
void sing(){
System.out.println(" The name of the person :"+name);
System.out.println(" The age of people :"+age);
System.out.println(" People's height :"+height);
}
public static void main(String[] args) {
String name; // Define a local variable name
int age; // local variable age
Double height; // local variable height
People people = new People() ; // Instantiate objects people
people.name = " Zhang San " ; // assignment
people.age = 18; // assignment
people.stuid = 180.0 ; // assignment
people.sing(); // Calling method sing
}
}
Code parsing :
This code first defines three member variables :String name、int age、Double height These three variables are only declared without initialization , Then define a member method sing();
stay main() Three same variables are also defined in the method , Only these are local variables ;
stay main() Instantiate object in function people , In memory, objects will be instantiated in the heap people Assign an address , Then we instantiate the object people It's assigned .people Calling a member method sing() .mian() Function to print the name of the input person , People's age and height , System execution completed .
The following shows the changes of memory in the process of instantiating an object by graphical method :
During the execution of the program , First, the member variables and methods in the class will enter the method area , Pictured :
The program runs to main() When the method is used ,main() The function method will enter the stack area , This process is called stacking ( Pressing stack ), Defines a for pointing to Person Instance variables person. Pictured :
The program runs to Person person = new Person(); It will open up a memory interval in the heap memory , To hold Person Instance object , Then put the member variables and member methods in new In instances, member variables are taken & Address value of member method Pictured :
And then to person Object to assign , person.name = “ waiter ” ; perison.age = 13; person.height= 180.0;
Find... In the stack area first person, Then find... According to the address value new Person() Perform assignment operation .
Pictured :
When the program arrives sing() When the method is used , Go to the stack area first and find person This reference variable , Then find... In the heap memory according to the address value new Person() Make method calls .
In the method body void speak() After being called , Will pop out of the stack immediately ( Departure )
Last , stay main() When the function finishes ,main() Functions will also come out of the stack Pictured :
That's all Java The whole process of object instantiation in memory .
10. Three JVM
Look at your version :java -version
What we learn is HotSpot
11. Pile up
Heap, One JVM There's only one heap memory , The size of heap memory can be adjusted .
After the class loader reads the class file , What do you usually put in the pile ? class , Method , Constant , Variable ~, Save real objects of all our reference types ;
The heap memory is subdivided into three areas :
● New Area ( Eden Park ) Young/New
● Retirement area old
● The permanent zone Perm jdk8 And then it's called “ Meta space ”
GC Garbage collection , Mainly in Eden Park and pension area ~
Suppose the memory is full ,OOM Not enough memory ! java.lang.OutOfMemoryError: java heap space
12. New Area 、 Old age area
New Area
class : The place of birth and growth , Even death ;
The garden of Eden , All the objects are in Eden Park new Coming out
Survival zone (0,1)0 District 1 District
When Eden is full, it triggers light GC, After light GC The survivors arrive at the survivor area , When the survivor area is full, it means that the new area is also full , Then trigger the reset GC, After heavy GC Then those who survived went to the nursing area .
truth : After study ,99% All objects are temporary objects !|
13. The permanent zone
This area is resident in memory . For storage jdk It's self-contained Class object .Interface Metadata , What's stored is java Some environment or class information at runtime , There is no garbage collection in this area ! close vm The virtual will free up the memory in this area .
A startup class , Loaded a large number of third parties jar package .Tomcat Too many applications deployed , A large number of dynamically generated reflection classes . Constantly being loaded . Until the memory is full , Will appear OOM(OutOfMemoryError Indicates heap memory overflow ).
- jdk1.6 Before : Forever , The constant pool is in the method area ;
- jdk1.7 : Forever , But it slowly degenerated ,
To the eternal generation
, The constant pool is in the heap ; - jdk1.8 after : There is no permanent generation , Constant pool in Metaspace ;
Meta space : Logically there is , There is no Physics
14. Heap memory tuning
By default : Total memory allocated It's computer memory 1/4, And the initialized memory : 1/64
OOM: Heap memory overflow terms of settlement
Try expanding the heap memory and see what happens
-Xms1024m -Xmx1024m -xx:+PrintGCDetails (idea Middle runtime VM options Parameters )
jvm Parameters
-Xss(StackSpace) Stack space
-Xms ,-Xmx(heap memory space) Heap space :Heap It is the most familiar area , He is jvm The area used to store object instances ,Heap stay 32 The maximum number of bits in the system is 2G, Its size passes through -Xms and -Xmx To control ,-Xms by jvm Minimum requested at startup Heap Memory , Default to physical memory 1/64, But less than 1G,-Xmx by jvm The largest one that can be applied for Heap Memory , Default to physical memory 1/4, Generally less than 1G.
Analyze memory , Look where there's a problem ( Professional tools )
( light GC heavy GC Errors occur only when they are full )
In a project , Pop up OOM fault , So how to eliminate ~ Study why something went wrong
- You can see the error in the first few lines of the code : Memory snapshot analysis tool ,MAT,Jprofiler
- Dubug, Analyze the code line by line
MAT It was the first eclipse Integrated use , Don't learn here
MAT,Jprogiler effect :
- analysis Dump Memory files , Quickly locate memory leaks
- Get the data in the heap
- Get big objects
Jprofile Use
Jprofiler setup script
idea Install plug-ins in Plugins Mid search Jprofiler
restart idea
Install client jprofiler
Go online to find the installation package , Easy to install
register ( The first two lose casually , Activation code online search )
idea Middle configuration
jprofiler Use
-Xms Set the initialization memory allocation size 1/64
-Xmx Set the maximum allocated memory , Default 1/4
-XX:+HeapDumpOnOutOfMemoryError oomDump
Look at this
1. stay idea Download jprofile plug-in unit
2. Download Online jprofile client
3. stay idea in VM Write parameters in parameters -Xms1m -Xmx8m -XX: +HeapDumpOnOutOfMemoryError
4. After running the program, in jprofile Error found when opening in client Tell me where the error is reported
Command parameter details
-Xms Set the initialization memory allocation size 1/64
-Xmx Set the maximum allocated memory , Tacit 1/4
-XX: +PrintGCDetails Print GC Garbage collection information
-XX: +HeapDumpOnOutOfMemoryError oom DUMP
The generated file is found in the current directory
Yes 6 Errors occurred times
Exception Can't capture ,Error Can capture
dump Many files will be generated later , Just delete it
15. GC Garbage collection
GC: Area of action Pile up + Method area
JVM It's going on GC when , It's not a unified recycling of these three areas . Most of the time , Recycling is a new generation ~
- The new generation
- Survival zone (form , to)
- Old age area
GC Two kinds of : light GC( Ordinary GC), heavy GC( overall situation GC)
GC subject
- JVM Memory model and partition of ~ Details on what to put in each area ?
- What are the partitions in the heap ? Eden,form,to, Old age area , Talk about their characteristics ?
- GC What are the algorithms of ? Mark removal method , Tag to sort out , Copy algorithm , Reference counter , How to use the ?
- light GC And heavy GC When do they happen ?
GC Commonly used algorithm
Reference counting ( Not commonly used )
object A It was used 1 Secondary addition 1, object B It was used 2 Secondary addition 2, object C by 0 Eliminate
Copy algorithm
When an object experiences 15 Time GC, Not dead yet , Into the retirement area .
-XX:MaxTenuringThreshold Set the age threshold , Default 15( Number of times the object was copied )
The upper age is set to 9999, The object basically won't enter the nursing area , It's all dead .
Copy algorithm diagram
- benefits : No memory fragmentation
- Disadvantage : Wasted memory space , Half more space is always empty to. Suppose the object 100% Survive ( In extreme cases )
- Copy the best use scenario of the algorithm : When the survival of the object is low , New Area ; The new area uses this algorithm
There are several situations , The object will be promoted to the old age :
Oversized objects will go straight into the old age ( Affected by virtual machine parameters -XX:PretenureSizeThreshold Parameter influence , The default value is 0, That is, do not open ( The default value is 0, It means that no matter how much, most of them are first eden Allocate memory in ), Unit is Byte, for example :3145728=3M, So over 3M The object of , Will be promoted directly to the older generation )
If to The district is full , The extra objects will also be directly promoted to the older generation
Copy 15 Time (15 year ) after , Still alive , Will also enter the older generation
here eden Area and from The area is full of garbage objects , It can be cleared directly .
Copy algorithm Will divide the memory space into two pieces , Use only one block of memory at a time . The replication algorithm also uses reachability analysis to mark garbage removal objects , When GC Execution time , Non garbage objects are copied to another memory space , And ensure continuity in memory , Then directly empty the previously used memory space . And then it goes back and forth .
Let's call these two memory areas from Area and to District .
As shown in the figure below ,r1 and r2 As GC Root object , After the accessibility analysis , Mark the Yellow object as garbage object .
The replication process is as follows ,GC Five surviving objects will be copied to to District , And make sure that to Continuity in memory space of the area .
Last , take from Garbage objects in the area are cleared .
In summary , The algorithm has less inventory objects , When there are many garbage objects , Very efficient . The advantage is that there is no memory fragmentation , But the disadvantages are also obvious , Is the direct loss of half of the available memory .
Tag clearing algorithm
advantage : No need for extra space
shortcoming : Two scans , Serious waste of time , Memory fragmentation will occur
Label compression algorithm ( Optimize mark removal )
Trilogy : Mark , eliminate , Compress
summary :
How to choose four algorithms :
Memory efficiency : Copy algorithm > Tag clearing algorithm > Label compression algorithm ( Time complexity )
Memory neatness : Copy algorithm = Label compression algorithm > Tag clearing algorithm
Memory utilization : Label compression algorithm = Tag clearing algorithm > Copy algorithm
reflection : Isn't there an optimal algorithm ?
answer : No, , More specifically, there is no best algorithm , Only the most appropriate algorithm ---->GC:( Also known as ) Band collection algorithm
The younger generation :
The survival rate is low
use Copy algorithm
Old age :
Large area : The survival rate is high
use Mark clear ( Memory fragmentation is not too much )+ Mark compression hybrid implementation
JMM
1. What is? JMM?
JMM :(java Memory Model abbreviation )java Memory model
2. What does he do ?
Solve the problem of shared object visibility :volilate
3. How should he learn
JMM : Abstract concept , theory
store To give properties Add a new key value pair .
JMM brief introduction
Java Memory model JMM(Java Memory Model)
JMM Mainly to stipulate Threads and Memory Some of the relationships between . according to JMM The design of the , The system has a main memory (Main Memory),Java All instance variables in are stored in main memory , Shared for all threads . Each thread has its own working memory (Working Memory), Working memory consists of cache and stack , The cache holds copies of variables in main memory , The cache may not always synchronize with main memory , In other words, changes to variables in the cache may not be immediately written to main memory ; In the stack are the local variables of the thread , Threads cannot directly access variables in the stack with each other .
JMM What is it?
JMM (Java Memory Model) yes **Java Memory model ,**JMM It defines the access rules of each shared variable in the program , That is, the underlying details of storing variables into memory and reading variables from memory in the virtual machine . Why design JMM Mask memory access differences between various hardware and operating systems , In order to make Java The program can achieve consistent memory access effect under various platforms .
Why understand JMM
Main memory , The interaction between working memory and thread
JMM It stipulates that shared variables are stored in main memory . Each thread has its own working memory , The working memory of the thread holds a copy of the main memory , The operation of variables is carried out in the working memory , You can't directly manipulate variables in main memory . Different threads cannot directly access each other's working memory variables , It needs to be completed through main memory . Here's the picture :
JMM Study
JMM(Java Memory Model)
JMM What is it? ?
JMM yes Java The concurrency of is based on the shared memory model , What does not exist is a concept and agreement .
About JMM The agreement of has the following points
- Locking and unlocking of threads are the same lock
- The thread must read the latest value in main memory into working memory before locking
- The shared variables must be flushed into main memory before the thread is unlocked
JMM What does it look like ?
As can be seen from the above figure JMM Defines the abstract relationship between thread and memory , Shared variables between threads are stored in memory , Each thread has a private local memory , Store a read-write copy of the thread in local memory . And threads do not directly manipulate memory , You can only operate the variables in your working memory and refresh them into memory . Memory is shared by multiple threads, and the working memory of threads is exclusively shared by threads . When the thread needs to communicate, it needs memory to complete .
You can see that in the diagram 8 This is an interactive operation with memory , Each of them Atomic , Indivisible
- lock( lock ): For main memory , It marks a variable as a thread exclusive state ;
- read( Read ): For main memory , It transfers the variable value from main memory to the working memory of the thread , For subsequent load The action to use ;
- load( load ): Working memory , It is the read The value of the operation is placed in a copy of the variable in working memory ;
- use( Use ): Working memory , It passes values from working memory to the execution engine , Whenever the virtual machine encounters an instruction that needs to use this variable , Will perform this action ;
- assign( assignment ): Working memory , It assigns values from the execution engine to variables in working memory , Whenever the virtual machine encounters an instruction to assign a value to a variable , Do this ;
- store( Storage ): Working memory , It transfers a variable from working memory to main memory , In case write Operation and use ;
- write( write in ): For main memory , It is the store Transfer values to variables in main memory .
- unlock( Unlock ): For main memory , It releases a locked variable , The released variables can only be locked by other threads
JMM(Java Memory model ) It also stipulates the implementation of the above 8 The following rules must be met for basic operations :
- Don't allow read and load、store and write One of the operations occurs separately ( That is, a variable is not allowed to be read from main memory, but working memory does not accept , Or start a write from the working memory, but the main memory does not accept it ), The above two operations must be performed in sequence , But there is no guarantee that it must be carried out continuously , in other words ,read And load Between 、store And write You can insert other instructions between .
- A thread is not allowed to discard its latest assign operation , That is, after the variable changes in working memory, it must be synchronized back to main memory .
- A thread is not allowed for no reason ( It didn't happen assign operation ) Synchronize data from the thread's working memory back to main memory .
- A new variable can only be retrieved from main memory “ Be born ”, It is not allowed to use an uninitialized one directly in working memory (load or assign) The variable of , In other words, it's a variable implementation use and store Before the operation , Must be executed first assign and load operation .
- Only one thread is allowed to execute a variable at the same time lock operation , but lock The operation can be repeated multiple times by the same thread , Multiple execution lock after , Only execute the same number of times unlock operation , Variables will be unlocked .
- If you execute on a variable lock operation , Will empty the value of this variable in working memory , Before the execution engine uses this variable , Need to be re executed load or assign The operation initializes the value of the variable .
- If a variable implementation is not lock Locking operation , It is not allowed to be executed unlock operation , I'm not allowed to go unlock A variable that is locked by another thread .
- Execute on a variable unlock Before the operation , You must first synchronize this variable back to main memory ( perform store and write operation ).
The problem is coming. ,JMM How to solve the visibility problem ?
problem 1: When two threads operate on a shared variable at the same time , After one thread is modified, does the other thread know ?
Let's take a look at the code
package com.kowk.jmm;
import java.util.concurrent.TimeUnit;
/** * @ClassName VolatileTest * @Description * * Ideas : * Define two threads and a shared static variable , * Threads A Infinite loop according to variables , Threads B Change the variable in a few seconds * If the program stops, the thread A You can know the thread B Make changes to variables , If you don't end, you don't know * * @Author kwok * @Version 1.0 */
public class VolatileTest01 {
// Define a static variable
private static int num = 0;
public static void main(String[] args) {
// Create a new thread to enter the infinite loop
new Thread(()->{
while (num == 0) {
}
}).start();
// Delaying the main thread by two seconds makes the effect obvious
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Change variables , Make the thread A Judge the state change
num = 1;
// Output variables , Confirm the modification
System.out.println(num);
}
}
The result is output 1 The post procedure is not over yet , Description thread A Don't know about threads B Made a change .
How to solve this invisibility problem is also a synchronization problem
This is our volatile The key words come out
that volatile What is it? ?
volatile yes java The lightweight synchronization mechanism provided by virtual machine
volatile There are three characteristics :
- Guaranteed visibility
- There is no guarantee of atomicity
- Prohibit command rearrangement
Guaranteed visibility
Common variables vs volatile The difference between variables is :volatile The special rule of ensures that the new value can be synchronized to the main memory immediately , And every thread in every use volatile Variables are immediately refreshed from main memory . Oh, this is equivalent to notifying other threads using this variable after the variable changes “ feed , This variable has changed , What's in your hand out 了 , Go and get another one ”. Next test , Change ordinary variables into volatile Just go
private volatile static int num = 0;
Run again and solve perfectly .
There is no guarantee of atomicity
volatile Since visibility is guaranteed, why not guarantee atomicity ? Need to know java There are only basic operations in, such as num = 1; This is atomic operation . And like i++ This kind of operation , The process can be divided into three steps , First of all get i Value , Second, yes i Add... To the value of 1, Finally, write the new value to the cache . So it becomes not an atomic operation , But what's wrong with this ? Let's use the code to explain
/** * @ClassName VolatileTEst02 * @Description * * Ideas : * use 10 Thread 100 Time ++ See the result after the operation , * If the result is as expected 1000 Guarantee atomicity , Otherwise, there is no guarantee * * @Author kwok * @Version 1.0 */
public class VolatileTEst02 {
// Define a volatile Decorated static variables
private volatile static int num = 0;
public static void main(String[] args) {
// use 10 Threads are paired with num Conduct ++ operation
for (int i = 0; i < 10; i++) {
new Thread(()->{
for (int i1 = 0; i1 < 100; i1++) {
// ++ The operation is not atomic , From the bytecode file, we can see that it is divided into 3 Step
num++;
}
}).start();
}
// Here we judge whether the surviving thread has only the main thread , If it is not the main thread that gives way to other threads
while (Thread.activeCount() > 2) {
Thread.yield();
}
// Print the results
System.out.println(num);
}
}
The result is different every time you run , explain volatile No guarantee , What's inside ?
It says num++ Operation Division 3 The first step is to get num Value , Second, yes num Add... To the value of 1, Finally, write the new value to the cache . When a thread A Access to this num Get the latest value when, but haven't had time to modify , It's blocked at this time , Threads B Obtained and modified num And brush back the memory , however ! Threads A Read num The atomic operation of has ended, so the thread A in num It won't fail , Oh, that's the problem . Here is only an explanation for the fact that atomicity is not guaranteed , If you want to solve it, you can refer to juc In bag atomic Method in package .
Prohibit command rearrangement
What is instruction rearrangement ?
Reordering is a means by which compilers and processors rearrange instruction sequences in order to optimize program performance . A code will go through the following stages .
- Compiler reorder : The compiler rearranges the lines of code without affecting the execution results of a single thread .
- Instruction reordering : The operation of a computer is composed of instructions , Our code operation is ultimately instructions , This is the same as the compiler .
- Memory : Because there is a read-write cache , Loading storage may be out of order .
Here's an example
int a = 1;
int b = 2;
a = a + 5;
b = a + a;
// The normal result is a = 6; b = 12;
// After reordering, the order becomes 1243 It becomes a = 6; b = 2;
Of course, single thread will not happen normally , Let's look at multithreading
Changing the order of threads in themselves does not affect their results , But other threads will be changed after refreshing .
This actually involves the problem of data dependency .
What is data dependency ?
Data dependency is the connection between two data, such as the appeal example , It can also be said that two related operations, including write operations, have data dependencies , At this time, they can't be reordered .
Here is a as-if-serial semantics
- meaning : No matter how reorder ( Compiler and processor to improve parallelism ),( Single thread ) The execution result of the program cannot be changed
- compiler 、runtime And the processor must comply as-if-serial semantics
- Compilers and processors do not reorder operations that have data dependencies , Because this reordering will change the execution result
- If there are no data dependencies between operations , These operations can be reordered by compilers and processors
volatile How to do it?
- Keep memory visible , If A The event occurred in B After the event , that B It must be right A so .
- volatile In this statement volatile There is a layer of memory barrier for the operation up and down , Generally speaking, the memory barrier is to tell the computer that the instructions here cannot be exchanged and sorted , This solves the possible problem of reordering
Here are four types of memory barriers :
Barrier type | Instruction example | explain |
---|---|---|
LoadLoadBarriers | Load1;LoadLoad;Load2 | Load1 and Load2 Represents two read instructions . stay Load2 Before the data to be read is accessed , Guarantee Load1 The data to be read is read . |
StoreStoreBarriers | Store1;StoreStore;Store2 | Store1 and Store2 Represents two write instructions . stay Store2 Write before execution , Guarantee Store1 Is visible to other processors |
LoadStoreBarriers | Load1;LoadStore;Store2 | stay Store2 Before being written , Guarantee Load1 The data to be read is read . |
StoreLoadBarriers | Store1;StoreLoad;Load2 | stay Load2 Before the read operation is executed , Guarantee Store1 Is visible to all processors |
StoreLoad Barriers It's an all powerful barrier , At the same time, it has other 3 The effect of a barrier . The cost of executing the barrier is more expensive , Because the processor usually flushes the contents of the current write buffer to memory (Buffer Fully Flush).
volatile Make sure the common writing is visible before writing , After writing, make sure it is visible to the follow-up
volatile Reading ensures complete reading
JMM(JAVA Memory Model) Study
JMM(java Memory model JAVA Memory Model, abbreviation JMM) Itself is an abstract concept It doesn't really exist
, He describes a set of rules or norms , Through this set of specifications , Defines the variables in the program ( Include instance fields , Static fields and the elements that make up the array object ) Access to .
JMM About the regulation of synchronization :
- Before thread unlocking , The value of the shared variable must be flushed back to main memory
- Before thread locking , You must read the latest value of main memory to your working memory
- Lock and unlock are the same lock
because JVM The entity running the program is the thread , And when each thread is created JVM Will create a working memory for it ( Some places are called stack spaces ), Working memory is the private data area of each thread , and Java The memory model states that all variables are stored in Main memory
, Main memory is a shared memory area , All threads have access to , But thread operations on variables ( Read assignments, etc ) Must be done in working memory , First, copy the variables from the main memory to your own working memory space , Then operate on variables , Write the variables back to main memory after the operation
, You can't directly manipulate variables in main memory , The working memory in each thread stores the... In the main memory Copies of variables
, So different threads can't access each other's working memory , Communication between threads ( Pass value ) It has to be done in main memory , Its brief visit process is as follows :
JMM characteristic
- visibility
- Atomicity
- Orderliness
Characteristic explanation
- visibility : A thread operates on an object , After modifying the value , The rest of the threads know , This is called visibility
The operation of each thread on the shared variables in the main memory is that each thread copies them to its own working memory for operation, and then writes them back to the main memory .
There may be a thread AAA Modified shared variables X Value , But it has not been written back to the main memory , Another thread BBB For the same variable in main memory X To operate , But this time A Shared variables in thread working memory X For threads B It's not visible
This synchronization delay between working memory and main memory causes visibility problems
- Atomicity :
When a thread operates on an object , The value of the object changes , And update it back to main memory , The remaining threads have not received the updated message , Update the value back to main memory as well , A situation that causes data loss , That is, atomicity is not guaranteed
- Prohibit command rearrangement
When a computer executes a program , To improve performance , Compilers and processors often respond to The instructions are rearranged
Source code ==》 Compiler optimized rearrangement ==》 A parallel rearrangement of instructions ==》 Memory system rearrangement ==》 The final instruction to be executed
In the single thread environment, ensure that the final execution of the program is consistent with the result of the sequential execution of the code
When the processor reorders , We have to consider the relationship between instructions Data dependency
In multithreading environment, threads execute alternately , Because of compiler optimization rearrangement , Whether the variables used in the two threads can guarantee consistency is uncertain , The result is unpredictable
Understand a concept , Memory barrier (Memory Barrier) Also called memory fence , It's a CPU Instructions , He has two functions :
- One is to ensure the execution sequence of specific operations
- But to ensure the memory visibility of some variables ( Use this feature , Realization volatile Memory visibility )
Because both compiler and processor can perform instruction rearrangement optimization , If you insert a Memory Barrier, The compiler and CPU, No matter what the order is, it can't be with this Memory Barrier Instruction reordering , in other words
By inserting a memory barrier , Prohibit reordering optimization of instructions before and after the memory barrier
. Another function of the memory barrier is to force out all kinds of CPU Cache data , So anything CPU All threads on can read the latest version of these data .
边栏推荐
- When to use useImperativeHandle, useLayoutEffect, and useDebugValue
- It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
- regular expression
- Rsync remote synchronization
- In C#, why can't I modify the member of a value type instance in a foreach loop?
- 4点告诉你实时聊天与聊天机器人组合的优势
- Practice of concurrent search
- MySQL (1) -- related concepts, SQL classification, and simple operations
- CIS基准测试工具kube-bench使用
- poj 2762 Going from u to v or from v to u? (infer whether it is a weak link diagram)
猜你喜欢
98. Verify the binary search tree ●●
el-cascader的使用以及报错解决
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
如何让同步/刷新的图标(el-icon-refresh)旋转起来
Redis高可用——主从复制、哨兵模式、集群
[classical control theory] summary of automatic control experiment
GFS分布式文件系统
《牛客刷verilog》Part III Verilog企业真题
保研笔记一 软件工程与计算卷二(1-7章)
rsync远程同步
随机推荐
TS type declaration
Difference between out of band and in band
424. 替换后的最长重复字符 ●●
How to enable relationship view in phpMyAdmin - how to enable relationship view in phpMyAdmin
Spire Office 7.5.4 for NET
【原创】程序员团队管理的核心是什么?
4点告诉你实时聊天与聊天机器人组合的优势
regular expression
Code farmers to improve productivity
STM32__06—单通道ADC
(4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
Différence entre hors bande et en bande
MySQL delete uniqueness constraint unique
The interface of grafana tool displays an error, incluxdb error
How to insert data into MySQL database- How can I insert data into a MySQL database?
哪些偏门项目可以做到?自媒体做到月赚一万以上很难吗?
There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
Scala concurrent programming (II) akka
The PostgreSQL column reference 'ID' is ambiguous - PostgreSQL column reference'id'is ambiguous