当前位置:网站首页>JVM interview
JVM interview
2022-07-02 02:14:00 【CV Engineer】
1.JDK、JRE、JVM Relationship ?
answer :JDK yes Java Standard development kit ,JRE yes Java Running environment ,JVM yes Java virtual machine .JDK Contained in the JRE,JRE Contained in the JVM.Java The language does not need to be recompiled when it runs on different platforms .Java Language use Java Virtual machines block information about specific platforms , bring Java Language compilers only need to be generated in Java Target code running on virtual machine ( Bytecode ), It can run unmodified on multiple platforms .
2. Memory structure
3. Program counter
answer : Used to hold JVM The address of the next instruction to be executed in . Program counters are private to each thread , When the time slice of another thread runs out , When you return to executing the code of the current thread , Through the program counter, you can know which instruction should be executed . There will be no memory overflow .
4. Virtual machine stack
answer : Memory space required for each thread to run , It's called the virtual machine stack . Each stack consists of multiple stack frames , Corresponds to the memory occupied each time the method is called . Each thread can only have one active stack frame , Corresponding to the currently executing method
5. Does garbage collection involve stack memory ?
Unwanted . Because the virtual machine stack is composed of stack frames , After the method is executed , The corresponding stack frame will be popped out of the stack . So there is no need to recycle memory through garbage collection mechanism .
6. The larger the stack memory allocation, the better ?
No . Because physical memory is certain , The larger the stack memory , Can support more recursive calls , But the fewer threads you can execute .
7. Whether local variables in methods are thread safe ?
If the local variable in the method does not escape the scope of the method , Is thread safe . If a local variable references an object , And escape the scope of the method , You need to consider thread safety .
8. When to throw out StackOverflowError?
If the stack depth of the thread request is greater than the depth allowed by the virtual machine , Throw out StackOverflowError. In the virtual machine stack , Too many stack frames ( Infinite recursion ) Or each stack frame takes up too much .
9. Native Method Stack
Some with native Keyword method is to need JAVA To call the local C perhaps C++ Method , because JAVA Sometimes you can't directly interact with the underlying operating system , So you need to use local methods .
10.JAVA Pile up
yes Java The largest chunk of memory managed by a virtual machine .Java A memory area shared by all threads , When the virtual machine starts
establish . The sole purpose of this memory area is to hold object instances , Almost all object instances allocate memory here . adopt new The objects created by keyword will be placed in heap memory .
11.Java7 and Java8 What's the difference in the memory model ?
Java8 Permanent generation cancelled , Use Metaspace (Metaspace) Instead of , Meta space is the existence of local memory (Native memory) in .
12. Constant pools and runtime constant pools
Constant pool
It's just a watch , The virtual machine instruction finds the class name to execute according to this constant table 、 Method name 、 Parameter type 、 Literal information
Runtime constant pool
The constant pool is .class In the document , When the class is loaded , Its constant pool information will be put into the runtime constant pool , And change the symbolic address into the real address
13. String pool
- Strings in the constant pool are just symbols , Only when used will it be converted to an object
- Using the mechanism of string pool , To avoid creating string objects repeatedly
- The principle of string variable splicing is StringBuilder
- The principle of string constant splicing is compiler optimization
- have access to intern Method , Take the initiative to put string objects that are not in the string pool into the string pool
- Used to put string objects, and the elements in them are not repeated
public class StringTableStudy {
public static void main(String[] args) {
String a = "a"; // Put it into the string pool
String b = "b"; // Put it into the string pool
String ab = "ab"; // Put it into the string pool
String ab2 = a+b; // Put in heap memory
// Use the method of splicing strings to create strings
String ab3 = "a" + "b"; // Put it into the string pool
System.out.println(ab == ab2);//false
System.out.println(ab == ab3);//true
}
}
14. Direct memory
Direct memory is the operating system and Java An area that code can access , There is no need to copy code from system memory to Java Heap memory , So it improves efficiency . Direct memory recycling is not through JVM Garbage collection to release , But through unsafe.freeMemory To manually release
15. How to judge whether an object can be recycled
1. Reference counting :
Example object a, As long as any object references a, be a The reference counter of is added 1, When the reference fails , The reference counter subtracts 1, When it comes to
The counter is 0 when , You can recycle it .
disadvantages : When quoting circularly , The count of both objects is 1, As a result, both objects cannot be released .
2. Reachability analysis algorithm
Scan objects in the heap , See if you can follow GC Root Object as the starting point of the reference chain to find the object , If you can't find it , It means that... Can be recycled
It can be used as GC Root The object of
- Virtual machine stack ( Local variables in stack frames ) Object referenced in .
- Object referenced by a class static property in a method area
- The object referenced by a constant in the method area
- Local method stack JNI( In other words Native Method ) Referenced object
3. Five quotations
Strong citation
Only GC Root When the object is not referenced , Will recycle strong reference objects
Pictured above B、C Objects do not reference A1 Object time ,A1 The object will be recycled
Soft citation
When GC Root When pointing to a soft reference object , When there is not enough memory , Objects referenced by soft references will be recycled
As shown in the figure above, if B Object no longer references A2 Object and out of memory , Soft reference refers to A2 The object will be recycled
Weak reference
Only weak references refer to this object , During garbage collection , Whether the memory is sufficient or not , Will recycle the objects referenced by weak references
Virtual reference
When the object referenced by the virtual reference object is recycled , The virtual reference object will be put into the reference queue , Call the method of virtual reference
16. Garbage collection algorithm
1. Mark - eliminate
As the name suggests , It refers to the process of garbage collection in the virtual machine , Firstly, the recyclable object is determined by marking algorithm , Then the garbage collector clears the corresponding content according to the identification , Make room for heap memory
shortcoming : It is easy to produce a large number of memory fragments
2. Mark - Arrangement
Mark - Arrangement Will not be GC Root Referenced object recycling , Know the memory space it occupies . Then sort out the remaining objects , It can effectively avoid problems caused by memory fragmentation , But because the whole takes some time , So it's less efficient
3. Copy
Divide the memory into two areas of equal size ,FROM and TO(TO Empty in middle ). First will be GC Root The referenced object is from FROM Put in TO in , Recycling is not GC Root Referenced object . Then exchange FROM and TO. This can also avoid the problem of memory fragmentation , But it takes up double the memory space .
4. Generational recycling
The newly created objects are placed in the new generation of Eden
When there is not enough memory in the garden of Eden , There will be a garbage collection , The recycling at this time is called Minor GC
Minor GC The garden of Eden and the surviving area FROM The surviving objects are copied to Survival zone TO in , And let its life increase 1, Exchange two more surviving areas
If the new generation of Eden is full again , Will trigger again Minor GC( Will trigger stop the world, Pause other user threads , Just let the garbage collection thread work ), At this time, we will not only recycle the garbage in the garden of Eden , And recycle the garbage in the surviving areas , Then copy the active object to the surviving area TO in . After recycling, two surviving areas will be exchanged , And increase the lifetime of objects in the surviving area 1
If the lifetime of the object in the surviving area exceeds a certain threshold ( The maximum is 15,4bit), Will be put into the older generation
If the memory of the new generation and the old generation is full , It will trigger first Minor GC, Trigger again Full GC, Scan all objects that are no longer used in the new and old generations and recycle
When you encounter a large object , Even if the new generation of Eden is empty , When the object cannot be accommodated , Will directly promote the object to the older generation
17. Garbage collector
safer : Let other threads stop at this point , To avoid moving object addresses during garbage collection , So that other threads cannot find the moved object
1.Serial The collector ( Serial collector )
Single thread 、 A simple and efficient ( Compared with the single thread of other collectors ), Using replication algorithm . For a limited single CPU In terms of environment ,Serial The collector has no overhead of thread interaction , Focusing on garbage collection can naturally achieve the highest single thread mobile phone efficiency . When the collector is recycling , All other worker threads must be suspended , Until it's over (Stop The World)
2.ParNew The collector
ParNew The collector is essentially Serial Multithreaded version of collector
characteristic : Multithreading 、ParNew The number of collection threads opened by the collector by default is the same as CPU Same number of , stay CPU In a lot of environments , have access to -XX:ParallelGCThreads Parameter to limit the number of threads for garbage collection . and Serial There are collectors Stop The World problem
3.Serial Old The collector
Serial Old yes Serial Old age version of collector
characteristic : It's also a single thread collector , Using tag - Sorting algorithm
4.Parallel Scavenge The collector ( Throughput first collector )
Closely related to throughput , It is also called throughput first collector
characteristic : Belong to the new generation collector is also the collector using replication algorithm ( Used the surviving areas of the Cenozoic ), It's also a parallel multithreading collector ( And ParNew Collector similar )
4.Parallel Old The collector
yes Parallel Scavenge Old age version of collector
characteristic : Multithreading , Using tag - Sorting algorithm ( There is no surviving area in the old age )
5.CMS The collector ( Response time first )
Concurrent Mark Sweep, An old age collector with the goal of obtaining the shortest recovery pause time , Try to make a single STW Shorter time .
characteristic : Based on tags - Clear algorithm implementation . Concurrent collection 、 Low pause , But it will cause memory fragmentation
CMS The collector runs as follows 4 Step :
Initial marker : Mark GC Roots Objects that can be reached directly . It's fast but it's still there Stop The World problem
Concurrent Tags : Conduct GC Roots Tracing The process of , Find the living object and the user thread can execute concurrently
Re label : In order to fix the tag record of the part of the object whose tag changes because the user program continues to run during the concurrent tag . There is still Stop The World problem
Concurrent elimination : Clean up and recycle marked objects
6.G1
- Pay attention to throughput and low latency at the same time ( response time )
- Huge amount of memory ( Big memory ), The heap memory is divided into multiple equal sized areas
- The whole is marked - Sorting algorithm , Between the two regions is the replication algorithm
边栏推荐
- SQLite 3 of embedded database
- 321. Chessboard segmentation (2D interval DP)
- JMeter (I) - download, installation and plug-in management
- Webgpu (I): basic concepts
- How to solve MySQL master-slave delay problem
- How to execute an SQL in MySQL
- leetcode2305. 公平分发饼干(中等,周赛,状压dp)
- 2022 Q2 - 提升技能的技巧总结
- Deep learning: a solution to over fitting in deep neural networks
- Pat a-1165 block reversing (25 points)
猜你喜欢
Infix expression to suffix expression (computer) code
Deployment practice and problem solving of dash application development environment based on jupyter Lab
SQLite 3 of embedded database
321. Chessboard segmentation (2D interval DP)
The wave of layoffs in big factories continues, but I, who was born in both non undergraduate schools, turned against the wind and entered Alibaba
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
CSDN article underlined, font color changed, picture centered, 1 second to understand
花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案
leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
随机推荐
CSDN insertion directory in 1 second
剑指 Offer 62. 圆圈中最后剩下的数字
C language 3-7 daffodils (enhanced version)
Sword finger offer 62 The last remaining number in the circle
How to debug apps remotely and online?
How to solve MySQL master-slave delay problem
MySQL constraints and multi table query example analysis
剑指 Offer 31. 栈的压入、弹出序列
Logging only errors to the console Set system property ‘log4j2. debug‘ to sh
Bash bounce shell encoding
2022 Q2 - résumé des compétences pour améliorer les compétences
【带你学c带你飞】2day 第8章 指针(练习8.1 密码开锁)
[graduation season] graduate seniors share how to make undergraduate more meaningful
The middle element and the rightmost element of the shutter
1069. Division of convex polygons (thinking, interval DP)
Exception handling of class C in yyds dry goods inventory
Pytest testing framework
MySQL中一条SQL是怎么执行的
【毕业季】研究生学长分享怎样让本科更有意义
MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent