当前位置:网站首页>JVM_ Common [interview questions]
JVM_ Common [interview questions]
2022-07-06 20:06:00 【Boyun V】
One 、JVM The concept of
Learn and use the topic by yourself ,` It's still improving , At present, the first draft is not !! I haven't recited yet ^^`, Hey !!!
List of articles
1、JVM The specific role of
jvm What is it? ?
jvm yes java virtual machine Java Virtual Machine Abbreviation .
[ 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-J1kl1bzW-1657075259410)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_12,color_FFFFFF,t_70,g_se,x_16.jpeg)]
What is? java virtual machine ?
Virtual machine is an abstract computer , It is realized by simulating various computer functions on the actual computer .Java Virtual machine has its own perfect hardware framework , Such as processor 、 Stack 、 Register, etc , Also has the corresponding instruction system .Java Virtual machine shields information related to specific operating system platform , bring Java The program only needs to be generated in Java Target code running on virtual machine ( Bytecode ), It can run unmodified on multiple platforms . Simply speaking JVM It's for parsing and running Java programmatic .
java The benefits of virtual machines ?
java The main reason for cross platform operation is that java virtual machine , meanwhile jvm You can shield system differences and invent some new languages to use jvm To complete . That's how it's done 《 A compilation runs everywhere 》 Characteristics .
2、JVM Partition of memory area in
Want to know JVM Partition of memory area in , First of all, you need to know JVM Where does the memory area in come from ,JVM The memory in is JVM Request a piece of memory from the operating system , Divide an area for this memory .
[ 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-guEKxMiG-1657075259412)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_15,color_FFFFFF,t_70,g_se,x_16.webp)]
Pile up ( Runtime constant pool ) | new The object will be placed in the heap |
---|---|
Method area | The loaded classes are placed in the method area , Static members |
Stack (JVM Stack and local method stack ) | local variable |
Program counter | The address stored describes where the next instruction to be executed by the current thread is |
** Be careful :** There are multiple threads in a process , Each thread has its own stack and program counters , A process shares a heap and a method area .
Determine whether a variable is on the heap or on the stack , It has nothing to do with whether this variable is a basic type or a reference type , And this variable is a local variable 、 Static variables or member variables .
3、 Common interview questions
1) How to understand basic data type or reference data type
The corresponding memory space in the basic data type stores specific values
[ 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-5RJNZrpV-1657075259413)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16.png)]
The memory space corresponding to the reference data type stores new Address of the object
[ 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-U9q9JRLh-1657075259414)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144037183.png)]
2) How to understand references and objects
[ 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-y6AM1zaD-1657075259415)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144037184.png)]
Reference or store the address ,new Objects are ontologies .
3) How to understand local variables, member variables and static variables
Member variables are on the heap
Local variables are on the stack
Static variables are in the method area
public class Test01 {
// Member variables
Test01 t1=new Test01();
// Static variables
static Test01 t2=new Test01();
public static void main(String[] args) {
// local variable
Test01 t3=new Test01();
}
}
[ 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-suEESvcl-1657075259415)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038185.png)]
4) Recursive execution
[ 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-4qM9xnMP-1657075259416)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038186.png)]
Although these variables a, But they are variables in different stack frames .
5)static The difference between method and ordinary method
Common methods include this( Instance related ), There is no in the static method this( Class related ) Common methods are also called instance methods , Static methods are also called class methods .
Two 、 Dynamic memory manager (GC)
1、GC The specific role of
GC It's the garbage collection mechanism ,java The memory requested in can be recycled by the garbage collection device ,GC It can avoid memory leakage to a certain extent , But there will be some extra overhead .
2、GC Characteristics of recycling
GC The main recyclables in Heap and method area Memory in , The memory in the stack is released until the thread ends or the stack frame is destroyed , The address stored in the program counter does not need to be released .
The basic unit of the recycling object :
about GC The basic unit recycled in is not bytes but object
[ 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-kT2HSOIA-1657075259416)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038187.png)]
GC The recycled memory is generally the memory in the unused location
The basic idea of recycling objects :
1) Mark : Find out whether this object needs recycling , And mark it
2) Recycling : Recycle this object
3、 Mark
1) Reference counting
Each object will be assigned a special counter variable. When a new reference points to this variable, the counter will increase by one , When a reference does not point to this variable, the counter will be decremented by one , When the reference counter is 0 This object tag will be recycled .
[ 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-LMJhSE17-1657075259417)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038188.png)]
But this quotes Circular reference The problem cannot be solved :
public class Test01 {
public static void main(String[] args) {
Test a=new Test();
Test b=new Test();
a.t=b;
b.t=a;
a=null;
b=null;
}
}
class Test{
Test t=null;
}
When you want to use objects a, find a References to , This reference is in the object b in , Looking for someone b Citation in a in
[ 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-6iI5iDqP-1657075259417)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038189.png)]
2) Accessibility analysis
stay java in GC Accessibility analysis is used to describe
There are certain associations between objects in the code , They constitute a “ Directed graph ”, Then we go through this “ Directed graph ” If the object traverses, it is not garbage , The opposite is rubbish .
Generally, where to start traversal ?
1、 Local variable table of stack frame in each stack
2、 Objects referenced in constant pools
3、 Objects referenced by static variables in the method area
When traversing, you don't start from one place like a binary tree , But traverse from multiple places , Such things are collectively referred to as GCRoot.
3) Recycling rules for class objects in the method area
1、 All instances of this class have been recycled
2、 Load the class ClassLoader It has also been recycled
3、 This kind of object is not used in code
4、 The type of citation
** Strong citation :** You can find an object and decide its life and death
** Soft citation :** You can find the object and decide the life and death of the object to a certain extent
** Weak reference :** You can find the object, but you can't decide the life and death of the object
** Virtual reference :** If you can't find an object, you can decide whether it's alive or dead
5、 Recycling
1) Mark clear
[ 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-JgWAKNzv-1657075259418)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038190.png)]
Mark clearing refers to directly releasing the memory in the marked area
advantage : A simple and efficient
shortcoming : It is easy to cause memory fragmentation
2) Mark copy
[ 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-shyDOEhx-1657075259418)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038191.png)]
Divide the memory into two areas and directly copy objects that are not garbage into another area
advantage : It can solve the problem of memory fragmentation , There will be no debris
shortcoming : Need extra memory space ( If there are many living objects, it will be inefficient )
3) Tag to sort out
[ 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-DOR90JIx-1657075259419)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038192.png)]
Similar to deleting data from an array
advantage : Do not want to copy the same need for additional memory space, there is no memory fragment
shortcoming : The efficiency of handling is low, which is not suitable for frequent use
6、 Generational recycling
We can use generational recycling to better clean up memory
We divide the memory in the heap into new generations ( Eden area and survival area ) And the older generation
[ 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-WaVe5iJN-1657075259419)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038193.png)]
The object was born in Eden , The memory of new objects is the memory in the new era
After the first round GC The marked object will be destroyed during scanning , Those that have not been killed will be copied to the survival area 01
Objects entering the survival zone will also be scanned and copied to the survival zone 02( The objects in the living area come from another living area and Eden )
When an object is not recycled after several copies in the living area, it means that the object will survive for a long time and be copied to the old age
The objects in the old generation also have to go through GC scanning , Because the objects in the elderly generation survive for a long time , The scanning cycle in the elderly generation will be longer
7、 Introduction to garbage collector
[ 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-bjOcebKb-1657075259420)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038194.webp)]
Garbage collectors do two things Marking and recycling , The application thread will stop working when recycling STW.
CMS and G1 The difference between :
a) Initial marker ( Just look for GCRoot Directly associated objects )
b) Concurrent Tags ( Concurrent execution with application thread , To traverse all objects )
*cms It's going to be carried out all the time 、G1 After finding that there are no surviving objects in the old age, they will be recycled directly .*
c) Final marker ( To fix b Error generated )
d) Screening and recovery
3、 ... and 、 Class loader
1、 The basic process of class loading
[ 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-AAYtZhDC-1657075259421)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038195.png)]
We find the file according to the class name , And read file structure parsing , Read the contents into memory , And construct the corresponding class object , This process is called load .
If this class has other associated classes , Other dependent content will be introduced , This process is called link .
Initial static members and execute static code blocks , This process is called initialization .
2、 When to trigger class loading
1) Construct an instance of this class
2) Call the static properties and static methods of this class
3) Using a subclass will trigger the loading of the parent class
class Test{
static {
System.out.println("Test.static{}");
}
{
System.out.println("Test{}");
}
public Test() {
System.out.println("Test Construction method ");
}
}
class Test0 extends Test{
static {
System.out.println("Test0.static{}");
}
{
System.out.println("Test0{}");
}
public Test0() {
System.out.println("Test0 Construction method ");
}
}
public class Test01 extends Test0{
public static void main(String[] args) {
System.out.println(" Start ");
new Test0();
new Test0();
System.out.println(" end ");
}
}
Execute static code block —— Execute code block —— Execute construction
[ 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-J339Zcl3-1657075259421)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_16,color_FFFFFF,t_70,g_se,x_16.png)]
3、 Common class loaders
Built in three loaders | Load content | Location |
---|---|---|
BootstrapClassLoader | Load the classes in the standard library (String ArrayList…) | jdk\jre\lib\rt.jar( The implementation of classes in the standard library is in this rt.jar in ) |
ExternsionClassLoader | Load a special class | jdk\jre\lib\ext All of the jar Find in the bag |
ApplicationClassLoader | Load the classes written by the application itself | CLASS_PATH environment variable java -cp Current directory |
4、 Parent delegation model
The essence of the parental delegation model is to find the class name in the process of class loading .class File search process , Content in the loading process .
[ 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-LZrviCyc-1657075259422)(…/…/…/images/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm93IGp1c3QgZG8gaXQ=,size_20,color_FFFFFF,t_70,g_se,x_16-1657075144038196.png)]
When a class starts loading , We'll start with AppClassLoader Start , But it won't find it immediately, and it will give it to its parent class first ,ExtClassLoader It will also be given to its own parent , then BootstrapClassLoader After you get the class name, go to rt.jar Start looking for , Find and return , If you don't find it, you'll be ExtClassLoader Medium ext Start looking in the directory , Find and return , If you don't find it, you'll be AppClassLoader Medium CLASS——PATH In the environment variables 、-cp Specify... In the directory 、 Find three places in the current directory , If found, load , Throw one if you don't find it ClassNotFoundException abnormal .
** Be careful :** The classloader written by yourself can violate the parental delegation model (Tomcat The classloader in does not follow the parental delegation model )
边栏推荐
- Cesium Click to draw a circle (dynamically draw a circle)
- PHP与EXCEL PHPExcel
- 深入浅出,面试突击版
- AsyncHandler
- 社招面试心得,2022最新Android高频精选面试题分享
- Oceanbase Community Edition OBD mode deployment mode stand-alone installation
- After solving 2961 user feedback, I made such a change
- Standardized QCI characteristics
- Qinglong panel white screen one key repair
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
猜你喜欢
某东短信登录复活 安装部署教程
[infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)
Hudi vs Delta vs Iceberg
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
Introduction to enterprise lean management system
5. Wireless in vivo nano network: top ten "feasible?" problem
Example of applying fonts to flutter
Cesium 点击绘制圆形(动态绘制圆形)
Tencent T3 teaches you hand in hand. It's really delicious
A5000 vgpu display mode switching
随机推荐
Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
信息系统项目管理师---第八章 项目质量管理
POJ 3207 Ikki' s Story IV – Panda' s Trick (2-SAT)
Database specific interpretation of paradigm
[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer
Poj3617 best cow line
微信小程序常用集合
技术分享 | 抓包分析 TCP 协议
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
Blue Bridge Cup microbial proliferation C language
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
使用ssh连接被拒
New generation garbage collector ZGC
[play with Linux] [docker] MySQL installation and configuration
部门树递归实现
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
PowerPivot - DAX (first time)
It's enough to read this article to analyze the principle in depth
夏志刚介绍
Example of applying fonts to flutter