当前位置:网站首页>Talking about JVM (frequent interview)

Talking about JVM (frequent interview)

2022-07-05 05:23:00 Pink Chi Ming

JVM brief introduction

JVM yes Java Virtual Machine For short , Meaning for Java virtual machine .
Virtual machine refers to a virtual machine with complete hardware functions simulated by software 、 A complete computer system running in a completely isolated environment .

Common virtual machines :JVM、VMwave、Virtual Box.
JVM And the other two virtual machines :

  1. VMwave And VirtualBox It simulates physics through software CPU Set of instructions , There are many registers in the physical system ;
  2. JVM Is simulated by software Java Bytecode instruction set ,JVM It is mainly reserved in PC register , All the other registers go into
    Cut it out .
    JVM It's a customized computer that doesn't exist in reality .

Daily development ,Java Programmers generally don't use JVM Things inside , I want to understand , You can read this book , There's a lot of dry goods
 Insert picture description here

1. JVM Memory partition

 Insert picture description here

JVM Memory is requested from the operating system , Divided into different areas , Different areas perform different functions

What is thread private ?
because JVM Multithreading is realized by switching threads in turn and allocating processor execution time , So at any given moment , A processor ( A multi-core processor is a kernel ) Will only execute an instruction in a thread . Therefore, in order to return to the correct execution position after switching threads , Each thread requires a separate program counter , Counters between threads do not affect each other , Independent storage . We call areas like this " Thread private " Of memory

1.1 Program counter ( Thread private )

The function of program counter : Used to record the line number executed by the current thread .

It's in memory The smallest area , Saved the next item to be executed Instructions Where is your address …

Instructions It's bytecode , If the program wants to run ,JVM You have to load the bytecode , Put it in memory , The program will take instructions out of memory one by one , Put it in CPU On the implementation , That is, you need to remember at any time , Which one is currently implemented .
CPU It is a parallel execution process , It does not serve only one process , It has to serve all the processes , Just because the operating system schedules and executes in the unit of threads , Each thread has to record its execution location , Program counter , Each thread has one .

1.2 Java Virtual machine stack ( Thread private )

Describes local variables and method call information , When the method is called , Each time a new method is called , Is involved " Push " operation , One method at a time , It's all about " Out of the stack " operation .
Stack space is relatively small , stay JVM The size of stack space can be configured in , But it's usually just a few M or Dozens of M, So the stack is likely to be full ( Normally, it's OK for us to write code , Afraid of recursion , Once the recursion condition is not set , There will be stack overflow :StackOverflowException)

Java The function of virtual machine stack :Java The virtual machine stack has the same lifetime as threads ,Java The virtual stack describes Java Method execution
Memory model : Each method creates a stack frame as it executes (Stack Frame) Used to store Local variable table 、 The stack of operands 、 Dynamic links 、 Methods the export Etc . We often talk about heap memory 、 Stack memory , Stack memory refers to virtual machine stack .
Java The virtual machine stack contains the following 4 part :
 Insert picture description here 1. Local variable table : It stores all kinds of basic data types known by the compiler (8 Big basic data types )、 Object reference . The memory space required for local variables is allocated during compilation , When entering a method , The amount of local variable space that this method needs to allocate in the frame is completely determined , The size of the local variable table will not be changed during execution . Simply put, it is to store method parameters and local variables .
2. Stack operation : Each method generates a first in and last out operation stack .
3. Dynamic links : Method reference to the runtime constant pool .
4. Method return address :PC The address of the register

1.3 Native Method Stack ( Thread private )

The native method stack is similar to the virtual machine stack , It's just Java The virtual machine stack is for JVM The use of , The local method stack is for local methods .

1.4 Pile up ( Thread sharing )

The role of the heap : All objects created in the program are saved in the heap

There is only one copy of a process , Multiple threads share a heap , It is also the largest area in memory , We new Out object , It's in the pile , Member variable of object , Naturally, it's in the pile .

Be careful : Variables of built-in type are on the stack , Variables of reference type are on the heap , This statement is wrong
Should be Local variables are on the stack , Member variables and new The objects of are on the heap
 Insert picture description here

1.5 Method area ( Thread sharing )

The role of the method area : Used to store class information loaded by virtual machine 、 Constant 、 Static variables 、 Real-time compiler compiled after the code and other data .

Method area , It's " Class object ", So-called " Class object ": What we wrote .java Such code will become .class( Binary bytecode ),.class Will be loaded into memory , That is to say JVM Constructed class object ( The process of loading is called " Class loading ")," Class object " It describes what this class looks like , What is the name of the class , Who's in it , What are the methods , What is the name and type of each member (public/private…), What is the name of each method , What kind of (public/private…), Method contains instructions …
" Class object " There is also a very important thing in it , Static members (static)

By static Decorated member , Become " Class properties ", And ordinary members , be called " Instance attributes "

2. JVM Class loading mechanism

Class loading , It's actually a design Runtime environment An important core function of , What does class loading do ? He is a man of .class file , Load into memory , Build as class object

2.1 Class loading process

Class load life cycle :
 Insert picture description here The top 5 The steps are in a fixed order and are also the process of class loading , In the middle 3 We all belong to the connection , So for class loading , It is divided into three steps :Loading,Linking,Initialization( Try to answer others in English )

2.1.1 load (Loading)

“ load ”(Loading) The stage is the whole “ Class loading ”(Class Loading) A stage in the process , It and class loading Class Loading Is different , One is loading Loading The other is class loading Class Loading, So don't confuse the two

In the load Loading Stage ,Java There are three things virtual machines need to do :
1) Get the binary byte stream that defines this class by using the fully qualified name of a class .
2) Convert the static storage structure represented by the byte stream into the runtime data structure of the method area .
3) Generate a representation of this class in memory java.lang.Class object , As the access to all kinds of data of this class in method area .

Summary is to find the corresponding .class file , Then open and read .class file ( Byte stream ), At the same time, a class object is preliminarily generated

stay Loading A key link in ,.class What exactly is in the document ?
 Insert picture description here For more information, please refer to the official documents :https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html

According to the format in the above figure , Read and parse the information , Initially fill in the class object

2.1.2 Connect (Linking)

A connection is usually a connection between multiple entities

One : verification (Verification)
The main task is to verify whether the read content exactly matches the format specified in the specification , If you find that the data format read here does not conform to the specification , Class loading fails , And throw an exception

Two : Get ready (Preparation)
The preparation phase is formal For variables defined in a class ( That is, static variables , By static Decorated variable ) The stage of allocating memory and setting the initial value of class variables
such as :
public static int value = 123;
He is value Of int The value is 0, Instead of 123

3、 ... and : analysis (Resolution)
The parsing phase is Java The process of virtual machine replacing symbolic reference in constant pool with direct reference , That is, the process of initializing constants .
Because in .class In file , Constants are centrally placed , Each constant has a number ,.class The initial information in the structure of the document is only the number , So you need to find the corresponding content according to the number , Fill in class objects .

2.1.3 initialization (Initializing)

Initialization phase ,Java The virtual machine really starts to execute Java Program code , Transfer ownership to the application . The initialization phase is the process of executing class constructor methods , Is to initialize class objects , Especially for static members

Typical interview questions : When will the loading of a class be triggered ( The code for )?
 Insert picture description here
What is his printing order ?
result :
 Insert picture description here
As long as this class is used , You need to load this class first ( Like instantiation , Calling method , Call static methods , Be inherited … Are used )

Big principles :
1: The class loading phase executes static code blocks , To create an instance , You must load the class first ;
2: Static code blocks are only executed once during the class load phase
3: Construction methods and code blocks , Each instantiation will execute , The construction code block precedes the construction method
4: The parent class is executed before , Subclasses are executed after
5: Our procedure is from main Start execution ,main Here is Test Methods , Therefore, execute main You need to load TestDemo

Our procedure is from main Method started ,main Here is TestDemo Method of this class , Therefore, it is necessary to execute main, You need to load TestDemo, and TestDemo Inherit B, To load TestDemo, You have to load B,B also Inherit A, First load A

2.2 Parent delegation model

This thing is of little use in our work , But I'm often asked during the interview …
This thing is a link in class loading , This link is in Loading Stage ( Compare the front part ), Parent delegation model , In fact, that is JVM Medium Class loader , How to name according to the full limit (java.lang.String) To find the .class Documentation process

Class loader :JVM It provides special objects , It's called a class loader , Responsible for class loading , Of course , The process of finding files is also the responsibility of the classloader …
.class file , There are many possible positions , Some are put on JDK Directory , Some are in the project directory , Others are placed in other specific positions , therefore ,JVM It provides multiple class loaders , Each class loader is responsible for a partition …

Default classloader , There are mainly 3 individual :
1: BootstrapClassLoader
Responsible for loading classes in the standard library (String,ArrayList,Random,Scanner…)
2: ExtensionClassLoader
Responsible for loading JDK Extended classes ( It is rarely used nowadays )
3: ApplicationClassLoader
Be responsible for loading the classes in the current project directory

Besides , Programmers can also customize the class loader , To load classes in other directories , image Tomcat The class loader is customized , Used to specifically load webapps Inside .class

Our parental delegation model , This describes the process of finding directories , That is, how the above class loaders work together …
 Insert picture description here

This set of search rules , It's called " Parent delegation model "( This is transliteration ,parent It can be either a father or a mother , By the rules , Call him " Single parent delegation model " It's not impossible , Of course , We can't decide the name )

Why? JVM Design like this ?
The reason is , Once programmers write their own classes and classes in the standard library , The fully qualified class name is repeated , It can also smoothly load classes into the standard library !!
like java.lang.String Such a class , We defined it ourselves , If the program is loaded, it is still Classes in the standard library , So there's no conflict , Security is guaranteed

If the custom classloader , Whether to follow the parental delegation model ?
Can be observed , Or not , See the demand .
It's like Tomcat load webapps Class in , Did not comply ( Because it doesn't make sense to abide by it )

3. JVM Garbage collection mechanism (GC)

3.1 What is garbage collection

Garbage collection (GarbageCollection,GC), When we were writing code , Often apply for memory , Create variables ,new object , Load class … These are all applications for memory , All applications are from the operating system , Now that you've applied for memory , Then we are When not in use Must also return the memory .

Generally speaking , The timing of applying for memory is clear ,( Some data needs to be saved , You need to apply for memory ), And the period of memory release , It's not so clear . We don't know whether we still use this memory

for instance : Suppose you throw away your clothes when you get home in the afternoon , Just ignore it , Then your mother found out , Just tidy up your clothes , Put it in the wardrobe , the second day , It's ok if you don't wear it , But you still have to wear this dress to go out , You go to the original location to find , EH ? Be missing , It's embarrassing …( This is memory release early )
Is it OK to release later ? Not so good , It's like you occupy a seat in the Library , You took your place early in the morning , As a result, you didn't go all day , This is also very embarrassing , Occupy a position without , Others can't use it ( This is memory release late )
And what we want is to be able to do it sooner or later

3.2 Why does garbage collection mechanism appear

image C Language : " I don't care about memory release , You programmers can do it by yourself , Anyway, I won't deduct my money …" therefore , stay C In language , You will encounter a common headache => " Memory leak "( After applying , Forget to release ) => Less and less memory is available , Finally, no memory is available !! therefore ," Memory leak " yes C/C++ The headache of programmers , Some leaks quickly , Some leaks slowly , The timing of exposure is uncertain , If it does , It's hard to check .C++ Later, I put forward a Intelligent pointer ( Maybe it's just a simple dependence C++ Medium RAII Mechanism , Actually, it's not intelligent at all ..) Such a mechanism , Through it, we can reduce " Memory leak " The risk of … But it's in java In front of many mechanisms, there are two brothers ( funny )

therefore , image Java,GO,PHP… Most of the major programming languages on the market today , Have adopted a plan , It's the garbage collection mechanism !!
There is probably a runtime environment ( image JVM,Python Interpreter ,Go Runtime …) To determine whether memory can be reclaimed through more complex strategies , And execute the recycling action … Garbage collection , Essentially, it depends on the runtime environment , A lot of extra work has been done , To automatically release memory , It greatly reduces the mental burden of programmers

however , Garbage collection also has disadvantages :
1: Consume extra expenses ( It consumes more resources )
2: May affect the smooth operation of the program ( Garbage collection often introduces STW(Stop The World, It's like time stands still ) problem )

Garbage recycling is so fragrant , Why? C++ Do not introduce GC?
In fact, some leaders have proposed this plan , But it was not implemented , because C++ Language has two high-voltage lines , Is his core principle :
1: and C Language compatibility , It can also maximize compatibility with various hardware and operating systems
2: The best performance …
Like artificial intelligence , The game engine , High performance servers , Operating system kernel … For compatibility / Scenes with extremely high performance requirements still have to C/C++ Come to do

3.3 What should we recycle in garbage collection

What is recycled is memory , But memory includes : Program counter , Stack , Heap and method , Some recycling , Some are not recycled :

Program counter : Fixed size , Release is not involved , I don't need to GC
Stack : The function is finished , The corresponding stack frame is automatically released , I don't need to GC
Pile up : need GC, A lot of memory in the code is on the heap
Method area : Class object , Class loaded , Conduct " Class unload " You need to free memory , Unloading is actually a very low-frequency operation ( Garbage collection is rarely involved )

Let's talk about garbage collection on the heap
First look at this picture :
 Insert picture description here

The above picture can be understood as three factions : Activists , Negatives , Middle swing ,

Activists : The memory in use is not released
Negatives : Memory that is no longer used must be released
Middle swing : The one between red and blue represents a part in use , Part of it is not needed , In this case It's not released , Wait until you use it up and don't use it anymore

GC There will be no " Half object " The situation of , Mainly to make garbage collection more convenient , It's simpler , remember : The basic unit of garbage collection is " object ", Not bytes

3.4 Specifically, how to realize garbage collection

It can be divided into two major stages , The first stage : Look for trash / Judging garbage .., The second stage : Release the garbage ..

It's like cleaning the room , First, clean all the garbage into the garbage can , Then throw them out of the room …

3.4.1 How to find garbage / Judging garbage

Our current mainstream thinking has two solutions :
1: Based on reference count ( No Java The scheme adopted in , This is another language , image Python The plan adopted )
2: Based on reachability analysis ( This is Java The plan adopted )
Pay attention to people asking you :
1: Talk about how to determine whether it is garbage in the garbage collection mechanism
2: Talk about Java How to determine whether it is garbage in the garbage collection mechanism of
These two problems are flawed , This is based on Reachability Analysis Java Of , Don't ask others Java Of What you said is Reference count based

① Based on reference count

For each object , Will introduce a small additional memory , Save how many references this object has to it

for example : Test t = new Test(); t Is a reference to this object , therefore Test Object has a reference , The reference count is 1
 Insert picture description here
If you write another :Test t2 = t , Then it means t and t2 Are references to this object , At this point our reference count becomes 2
 Insert picture description here

When the reference count is 0 When , It's not in use , Just think it's rubbish , Release the memory
 Insert picture description here

Disadvantages of reference counting :
1: Low space utilization !! Every new All objects must be matched with a counter ( The counter assumes 4 Bytes ), If the object itself is large ( Hundreds of bytes ), Come out more 4 Bytes , It's nothing , But if the object itself is small ( I just 4 Bytes ), More 4 Bytes , Equivalent to half the space being wasted
2: There's a problem with circular references
 Insert picture description here

② Based on reachability analysis

Is through additional threads , Regularly scan objects in the entire memory space , There are some starting positions ( be called GCRoots), It will be similar to depth first traversal , Mark all the accessible objects .( A marked object is a reachable object ), Objects that are not marked , It's just not accessible , It's rubbish …

GCRoots: Refers to local variables on the stack , The object pointed to by the reference in the constant pool , The object pointed to by the static member in the method area …

for instance :
 Insert picture description here

advantage : Two disadvantages of reference counting are overcome , Low space utilization , The problem of circular quotation
shortcoming : The system costs a lot , If there are many objects in memory , Traversal may be slow , Consume time and system resources

All in all , Look for trash , The core is to confirm whether the object will be used in the future , Then what is not used ? There is no reference point , Don't use

3.4.2 Garbage collection algorithm

① Mark - eliminate Algorithm

Tagging is the process of reachability analysis , Clearing is to release memory directly
 Insert picture description here
At this time, if you directly release , Although the memory is returned to the system , But we found that the released memory is discrete , Discontinuous , The problem brought to us is " Memory fragments "

There is a lot of free memory , If we assume that the total memory is 1G, If we apply 500M Memory , He may also fail to apply ( Because of the application 500M Is continuous memory ), And every time you apply , Memory must be continuous space , And here it is 1G Free memory may just " Memory fragments ", Together, there is 1G

② Copy Algorithm

In order to solve " Memory fragmentation problem ", Replication algorithm is introduced , On the whole , Namely " Use half , Lose half "
 Insert picture description here

Directly put the garbage , Copy to the other half , Release the original space as a whole !!
 Insert picture description here

advantage : It's solved " Memory fragments " The problem of
shortcoming : 1. Low memory space utilization 2. If there are many objects to keep , There are few objects to release , At this point, the replication cost is very high .

③ Mark - Arrangement Algorithm

 Insert picture description here

advantage : Space utilization is high
shortcoming : Replication is still not resolved / The problem of high overhead in moving elements

Although all the above have defects , But in JVM In the implementation of , Be able to combine various schemes

④ Generational recycling Algorithm

Classify objects ( Based on the " Age " classification ), An object survived a round GC scanning , It's called " One year old ", For objects of different ages , Take different solutions …

 Insert picture description here

Be careful : There may be a saying on the Internet : 98% My new target is not going to survive a round GC Of ,2% New objects will enter the surviving area , This number is actually unreliable , If others ask, it's best not to say so , Say that most objects can't survive a round GC That's it

原网站

版权声明
本文为[Pink Chi Ming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050521074384.html