java interview finishing two - JVM

1. The main structure of



JVM JVM is mainly composed of class loading subsystem, runtime data area, execution engine and native method interface.
2. JVM's class loading mechanism



a) Load
i. Obtain the binary byte stream that defines this class through the fully qualified name of a class
ii. Convert the static storage structure represented by this byte stream into the runtime data of the method area Structure
iii. Generate a java.lang.Class object representing this class in memory as an access entry for various data of this class in the method area
b) Verification
c) Preparation: The preparation stage is to formally allocate memory for class variables and set the class During the initial value stage of variables, the memory used by these variables will be allocated in the method area. At this time, only class variables (variables modified by static) are allocated for memory allocation. Note that the preparation phase only allocates variables in memory, the initial value is zero, and the variable assignment work will be done during initialization; but if the class variable is modified as final, the initial value will be initialized
d) Parsing
e) Initialization: According to the programmer Make a plan to initialize class variables and other resources
i. Load an instance of the class
ii. Load the static variable of the
class iii. Load the static method of the class
iv. Instantiate an object
v. Initialize the parent class first
3. In Java The assignment order of
a) static final (static final)
i. parent class static constant assignment
ii. sub class static constant assignment
b) static variable and code block (static)
i. parent class static variable
ii. Parent class code block
iii. Child class static variable
iv. Child class code block
c) Instantiation variable assignment of
parent class d) Construction instantiation of parent class
e) Instantiation variable assignment of
child class f) Construction of child class Instantiation
4. Memory area of ​​JVM
a) Thread private area
i. Program counter
ii. Java Stack (virtual machine stack): the memory model of java method execution, used to store local variables, operand stack, dynamic link, method exit, etc. Information
iii. Local method stack: serving native methods
b) Thread shared area
i. Heap (java heap) - storing various object instances
1. New generation
a) Eden area, From survivor, To survivor
2. Old generation
ii. Method area (method area) - what we usually call a permanent area, used to store class information, constants, static variables, compiled code, etc.
5. Direct memory - not part of the JVM runtime data area, but will also be frequently used. NIO introduced after JDK5 provides an IO method based on Channel and Buffer. It can directly allocate off-heap memory using the native method, and then use the directByteBuffer object as a reference to this memory for operation
6. GC object determination method (object survival determination method)
Mainly using the reachability algorithm (reference chain method), from a GC Roots object (object referenced in the virtual machine stack, object referenced by the static attribute of the method area class, object referenced by the constant pool in the method area, and object referenced by the local method stack) ) starts a downward search, if an object does not have any reference chain connected to the GC Roots, the object is unavailable. The first is a reprieve, and the second is immediate.
7. Java's garbage collection mechanism There is a thread (low priority) responsible for garbage collection
in the JVM. When the virtual machine is idle or the current heap memory is insufficient, the execution is triggered, and the objects that are not referenced are scanned and placed in the garbage collection. Recycling in the collection
8. What are the methods of garbage collection in Java
a) mark-sweep - not efficient, resulting in memory fragmentation
b) replication algorithm - the core algorithm of the new generation recycling. The memory is divided into 8:1:1 three parts, the larger part of the memory is handed over to the Eden area, and the rest are two smaller memory areas called the Survior area. The Eden area is used first. If the Eden area is full, the surviving objects are copied to the survivor area, and then the Eden area is cleared. If the survivor is not enough, it is copied to the old generation through the allocation guarantee mechanism.
c) Mark-Sweep - Addresses the memory fragmentation problem of the mark-sweep algorithm.
d) Banding collection - Most of today's virtual machines use this method for garbage collection. According to the life cycle of the object, the heap is divided into the young generation and the old generation. The new generation mainly adopts the replication algorithm; the old generation can use the mark sorting or mark clearing algorithm due to the high object survival rate.
9. The difference between Minor GC, Major GC and Full GC
a) Minor GC is to clean up the young generation
b) Major GC is to clean up the old generation, which is the same as full GC. In fact, full GC itself will not perform Minor GC first
c) Because many objects in the old generation will refer to objects in the new generation, performing Minor GC first can improve the speed of GC in the old generation
10. Java's garbage collector
a) Serial collector - a single-threaded collector -XX:+UseSerialGC
b) ParNew collector - multi-threaded version of Serial, parallel collector, new generation parallel, old generation serial; New generation replication algorithm, old generation mark compression algorithm-XX:+USeParNewGC
i. Short pause time, high recovery efficiency, high throughput requirements
ii. Suitable for large-scale applications, scientific computing, large-scale data collection, etc.
c) Parallel collector - Similar to the ParNew collector, the Parallel collector is more concerned with throughput. You can open the adaptive adjustment strategy through parameters; you can also control the GC time within the millisecond range through parameters; the new generation assignment algorithm, the old generation mark compression algorithm. -XX:+UseParallelGC
d) Parallel old collector - the old version of the Parallel scavenge collector, using multi-threading + mark sorting algorithm.
-XX:+UseParallelOldGC
e) CMS (concurrent-mark-sweep) collector - The concurrent collector is a collector whose goal is to obtain the shortest collection pause time. XX:+UseConcMarkSweepGC
i. The process is mainly divided into four steps:
1. Initial mark (will stop) just mark the objects that GC Roots can directly associate with, which is very fast
2. Concurrent marking
3. Remark (will stop) for correction During the
concurrent
marking period, the marked record of the part of the object that is marked with the movement caused by the user program continuing to operate high】
iii. Disadvantages: very sensitive to CPU resources, affecting throughput, processing floating garbage will cause Full GC, resulting in memory fragmentation
f) G1 collector - a garbage collector for server-side applications. The G1 collector divides the heap into 1M-32M regions and scans them in the background using multithreading. The G1 collector will preferentially scan areas that contain more garbage. -XX:UseG1GC
i. G1 operation steps
1. Initial marking
2. Concurrent marking
3. Final marking
4. Screening and recycling
ii. Parallel and concurrent - G1 collector can make full use of CPU resources to shorten stop time under the advantage of multi-core
iii . Generational collection – can handle newly created objects and objects that have been around for some time in a different way
iv. Spatial integration – G1 uses a mark-and-sort algorithm for collection as a whole, and a copy algorithm for collection in part
v. Possible Predictive Pause - Modeling predictable pause times, allowing the user to explicitly specify in a millisecond
timeframe 12. JVM common parameter configuration a ) -Xms: initial heap size b) -Xms: maximum heap size c) -XX: NewSize=n: set the young generation size






d) -XX:NewRatio=n: Set the ratio of young generation to old generation. For example: 3 means that the ratio of the young generation to the old generation is 1:3 -XX:SurvivorRatio=n: The ratio of the Eden area to the two Survivor areas in the young generation. Note that the Survivor area has two. For example, 3 means Eden: 3 Survivor: 2, a Survivor area occupies 1/5 of the entire young generation
e) -XX:MaxPermSize=n: Set the persistent generation size
f) -XX:+UseSerialGC: Set the serial collector
g) - XX:+UseParallelGC:Set parallel collector
h) -XX:+UseParalledlOldGC:Set parallel old generation collector
i) -XX:+UseConcMarkSweepGC:Set concurrent collector
j) -XX:UseG1GC:Set G1 collector
k) - XX:+PrintGC output GC log
l) -XX:+PrintGCDetails output GC detailed log
m) -XX:+PrintGCTimeStamps output GC timestamp (in the form of reference time)
n) -XX:+PrintGCDateStamps output GC timestamp (in the form of a date, such as 2013-05-04T21:53:59.234+0800)
o) -XX:+PrintHeapAtGC prints out heap information before and after GC
p) -XX:+PrintGCApplicationStoppedTime // Output GC causes application pause time
q) -Xloggc:../logs/gc.log output path to log file parallel collector configuration
13. Parallel collector configuration
a) -XX:ParallelGCThreads=n: Set the number of CPUs used by the parallel collector for collection. Number of parallel collection threads
b) -XX:MaxGCPauseMillis=n: Set the maximum pause time for parallel collection (if the garbage collector still does not collect it, it will stop collecting)
c) -XX:GCTimeRatio=n: set Garbage collection time as a percentage of program runtime. The formula is: 1/(1+n)
d) -XX:+CMSIncrementalMode: Set to incremental mode. Applicable to single CPU case
e) -XX:ParallelGCThreads=n: Set the number of CPUs used when the concurrent collector young generation mobile phone mode is parallel collection. Number of parallel collection threads
14. Under what circumstances does Full GC occur
a) Insufficient space in the old generation - The space in the old generation will only be insufficient when the new generation objects are transferred and created as large objects and large arrays. When the Full GC is executed If there is still insufficient space after GC, java.lang.OutOfMemoryError: Java heap space is thrown.
b) Insufficient space in immortal area - when there are many classes to be loaded, reflected classes and methods to be called in the system, Permanent Generation may be occupied Full GC is performed even if it is not configured to use CMS GC. If it still cannot be recovered after Full GC, the JVM will throw java.lang.OutOfMemoryError: PermGen space
c) Promotion failed and concurrent mode failure appear during CMS GC
i. promotion failed is caused when the survivor space cannot be placed in the Minor GC, and the objects can only be placed in the old age, and the old age cannot be placed at this time;
ii. concurrent mode failure occurs during the execution of the CMS GC. Objects are to be placed in the old generation, and the old generation is caused by insufficient space (sometimes "insufficient space" is caused by too much floating garbage during CMS GC, causing temporary insufficient space to trigger Full GC).
15. New generation GC and old generation GC
a) New generation GC - serial GC (SerialGC), parallel recovery GC (ParallelScavenge) and parallel GC (ParNew)
b) old generation GC - serial GC (SerialMSC), parallel GC (parallelMSC) and concurrent GC (CMS)
c) The CMS collector used in the young generation also uses the same algorithm as the parallel collector
16. JVM optimization ideas
a) Selection of the size of the young generation
i. Response time priority applications: as much as possible The larger the better when it is close to the minimum response time limit of the system
ii. Applications with throughput priority: set as large as possible, garbage collection can be performed in parallel
b) Selection of the size of the old generation
i. Applications with priority in response time: use concurrent collectors in the old generation , so its size should be set carefully
ii. throughput-first applications: generally have a larger young generation and a smaller old generation
c) fragmentation problems caused by smaller heaps
i. If the old generation uses concurrent collectors ( CMS), you need to enable the compression of the old age and configure how many times of Full GC to compress the old age.
d) In general, a stable heap size is good for garbage collection. The way to get a stable heap size is to make the size of -Xms and -Xmx the same, that is, the maximum heap and the minimum heap (initial heap) are the same.
e) Try to use large memory paging –XX:+LargePageSizeInBytes: set the size of large pages
this This mechanism, in terms of data structure, ensures the efficient access to memory and enables the OS to support non-contiguous memory allocation.
f) The throughput-first scheme will minimize the total time for the system to perform garbage collection, so consider parallel collection collectors that focus on system throughput. On a computer with high performance, to optimize throughput first, you can use the parameters:
java –Xmx3800m –Xms3800m –Xmn2G –Xss128k –XX:+UseParallelGC
   –XX:ParallelGC-Threads=20 –XX:+UseParallelOldGC
–Xmx3800m –Xms3800m: Set the maximum and initial value of the Java heap.
-Xss128k: Reduce the size of the thread stack, so that the remaining system memory can support more threads;
-Xmn2g: Set the size of the young generation area to 2GB;
-XX:+UseParallelGC: The young generation uses a parallel garbage collection collector. GC time can be reduced as much as possible.
–XX:ParallelGC-Threads: Set the number of threads used for garbage collection, which can be set equal to the number of CPUs.
–XX:+UseParallelOldGC: Set the old generation to use the parallel collection collector.
g) use a non-occupying garbage collector
For applications with response time priority, the first consideration is to use the attention system to stop the CMS collector; secondly, in order to reduce the number of Full GCs, objects should be reserved in the young generation as much as possible, because the cost of the young generation Minor GC is much smaller than the old generation. Full GC.
java –Xmx3550m –Xms3550m –Xmn2g –Xss128k –XX:ParallelGCThreads=20
–XX:+UseConcMarkSweepGC –XX:+UseParNewGC –XX:+SurvivorRatio=8 –XX:TargetSurvivorRatio=90
–XX:MaxTenuringThreshold=31

–XX:ParallelGCThreads=20 : Set 20 threads for garbage collection;
–XX:+UseParNewGC: Use parallel collector for young generation;
–XX:+UseConcMarkSweepGC: Use CMS collector to reduce pause in old generation;
–XX:+SurvivorRatio: Set Eden area and Survivor area The ratio is 8:1. A slightly larger Survivor space can improve the possibility of reclaiming objects with a short life cycle in the young generation. If the Survivor is not large enough, some short-lived objects may directly enter the old generation, which is unfavorable to the system.
–XX:TargetSurvivorRatio=90: Set the available ratio of Survivor area. When the stored objects exceed this percentage, the objects will be compressed to the old generation. Therefore, this option is more helpful to keep objects in the young generation.
–XX:MaxTenuringThreshold: Set the age at which young objects are promoted to the old generation. The default value is 15 times, that is, if the object still survives after 15 Minor GCs, it will enter the old generation. The purpose of setting 31 here is to keep objects in the young generation area as much as possible.
17. Commonly used JVM recycling strategies The
new generation and the old generation indicate that
Serial CMS+Serial Old CMS is a concurrent GC, which realizes the concurrent work of GC threads and application threads, and does not need to suspend all application threads. In addition, when the CMS fails to perform GC, it will automatically use the Serial Old strategy for GC.
ParNew CMS is enabled with the -XX:+UseParNewGC option.
ParNew is a parallel version of Serial, you can specify the number of GC threads -XX:ParallelGCThreads option specifies the number of GC threads.
If the option -XX:+UseConcMarkSweepGC is specified, the new generation uses the ParNew GC strategy
ParNew Serial Old by default. Use the -XX:+UseParNewGC option to enable it. The new generation uses the ParNew GC strategy, and the old generation uses the Serial Old GC strategy by default. The
Parallel Scavenge Parallel Old Parallel Scavenge strategy mainly focuses on a controllable throughput, which is suitable for applications running persistently in the background, but not suitable for applications with many interactions. Program
G1GC G1GC -server -XX:+PrintGC
-Xms10240m -Xmx10240m
-XX:PermSize=128m -XX:MaxPermSize=128m
-Xss256k
-Xloggc:${LOG_HOME}/logs/gc.log
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=HeapOOMError.log
-XX:+UnlockExperimentalVMOptions
-XX:+UseG1GC -XX:MaxGCPauseMillis=10
-XX:GCPauseIntervalMillis=200
-XX:+DisableExplicitGC
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326224574&siteId=291194637