preface
This article analyzes the JVM memory region distribution, JVM memory overflow analysis, JVM garbage collection algorithm/garbage collector, JVM performance tuning tools and techniques, class loading and other high-frequency questions.
Use XMind to draw a map (source file for some nodes have detailed remarks and reference materials, welcome to follow my public account: to win the world with Java architecture background send [JVM] to get download link, after improving the increase will be updated in time) :
First, the JVM memory region is dependent
1. The memory model of THE JVM and partitioning conditions and functions
As shown below:
The yellow part is common to threads, and the blue part is private to threads.
Methods area
Used to store data such as class information, constants, and static variables loaded by virtual machines.
The heap
Store object instances, and all objects and arrays are allocated on the heap. Is the largest area of memory managed by the JVM.
The stack
Java method execution memory model: stores information about local variables, operand stacks, dynamic linking, method exits, and so on. The life cycle is the same as that of a thread.
Local method stack
The function is similar to that of the VIRTUAL machine stack. The local method stack serves native methods, and the virtual machine stack serves Java methods executed by virtual machines.
Program counter
An indicator of the line number executed by the current thread. Is the smallest area of memory in the JVM. When performing bytecode work, the program counter is used to select the next bytecode instruction to be executed.
2.Java memory allocation.
Register: We have no control. Static field: Static member defined by static. Constant pool: Symbolic references (fully qualified names of classes and interfaces, field names and descriptors, method and name and descriptors) to final constant values and some text modifications that are determined and stored in.class files at compile time. Non-ram storage: Permanent storage space such as hard disks. Heap memory: Objects and arrays created by new are managed by the Java virtual Machine automatic garbage collector and are accessed slowly. Stack memory: Variables of basic types and reference variables of objects (the access address of the heap memory space) are fast and can be shared, but the size and lifetime must be determined, which is inflexible. 3. What is the structure of the Java heap? What is Perm Gen space in the heap? The JVM’s heap is the run-time data area on which all instances and arrays of classes are allocated memory. It is created when the JVM starts. The heap memory occupied by objects is reclaimed by the automatic memory management system, the garbage collector. Heap memory is made up of living and dead objects. Live objects are accessible to the application and are not garbage collected. Dead objects are objects that are inaccessible to the application and have not yet been collected by the garbage collector. These objects occupy heap memory space until the garbage collector collects them.
3. Memory leaks in Java? Briefly describes the
import java.io.IOException;
public class GarbageTest {
/ * * *@param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try {
gcTest();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("has exited gcTest!");
System.in.read();
System.in.read();
System.out.println("out begin gc!");
for(int i=0; i<100;i++)
{
System.gc();
System.in.read();
System.in.read();
}
}
private static void gcTest(a) throws IOException {
System.in.read();
System.in.read();
Person p1 = new Person();
System.in.read();
System.in.read();
Person p2 = new Person();
p1.setMate(p2);
p2.setMate(p1);
System.out.println("before exit gctest!");
System.in.read();
System.in.read();
System.gc();
System.out.println("exit gctest!");
}
private static class Person
{
byte[] data = new byte[20000000];
Person mate = null;
public void setMate(Person other)
{ mate = other; }}}Copy the code
Memory leaks in Java: Long life cycle of object reference object holds a short life cycle is very possible memory leaks, although a short life cycle objects no longer needed, but because of long life cycle object holds its reference that can’t be recycled, which is memory leaks in Java takes place, in layman’s terms, is the programmer might create an object, Later have no longer use this object, the object has been cited, namely the object useless but not reclaimed by the garbage collector, this is the situation possible memory leaks in Java, for example, caching system, we loading an object in the cache (in a global map object, for example), and no longer use it, This object is always referenced by the cache but is no longer used.
To check for memory leaks in Java, make sure that the program executes all branches to the end of the program, and then see if an object is used. If not, the object is a memory leak.
If an external object instance of a class of the method returns an inner class instance of the object, the inner class object was cited for a long time, even if the external class instance objects no longer be used, but as a result of the inner class lasting external instance objects of a class, the external class object will not be garbage collected, this will cause memory leaks.
The following content from the Internet (one of the main characteristic is to empty stack elements, it is not completely removed from the array, but reduce the total number of storage, I can write better than this, when remove an element, by the way, let it disappear from the array to the element’s location value can be set to null) :
I really can’t think of a more classic example than that stack, so I have to cite someone else’s example. The following example is not what I thought of, but what I read in a book. Of course, if I didn’t read in a book, I might have thought of it myself after a while, but no one would believe me if I said I thought of it myself.
public class Stack {
private Object[] elements=new Object[10];
private int size = 0;
public void push(Object e){
ensureCapacity();
elements[size++] = e;
}
public Object pop(a){
if( size == 0) throw new EmptyStackException();
return elements[--size];
}
private void ensureCapacity(a){
if(elements.length == size){
Object[] oldElements = elements;
elements = new Object[2 * elements.length+1];
System.arraycopy(oldElements,0, elements, 0, size); }}}Copy the code
The above principle should be simple, if we add 10 elements to the stack, and then all pop up, even though the stack is empty, there is nothing we want, but this object is not recyclable, it meets the two conditions of memory leak: useless, not recyclable. But it doesn’t really matter if there’s such a thing, because if the stack is used less, we’re wasting a couple of K’s of memory, so it doesn’t matter if we’re using G’s, and it’s going to be recycled pretty quickly. Let’s look at two examples.
public class Bad{
public static Stack s=Stack();
static{
s.push(new Object());
s.pop(); // There is a memory leak on an object
s.push(new Object()); // The above object can be recycled, which is equivalent to self-healing}}Copy the code
Because it’s static, it’s there until the program exits, but we can also see that it has a self-healing function, which means that if you have a Stack of 100 objects at most, you’re going to have a Stack of 100 objects that can’t be recycled at most and this should make sense, the Stack holds 100 references inside, The worst that can happen is that they’re useless, because once we start something new, the old references disappear!
Another case of a memory leak: When an object is stored in a HashSet, it cannot modify the fields in the object that computed the hash. Otherwise, the modified hash value of the object will be different from the original hash value stored in the HashSet. In this case, Even if the Contains method retrieves an object from the HashSet collection using the current reference to the object as an argument, it returns a result that the object is not found, which makes it impossible to remove the current object from the HashSet collection separately, causing a memory leak.
Second, garbage recycling related
1. What is GC? Why GC?
GC stands for garbage collection. Memory processing is the place where programmers are prone to problems. Forgetting or wrong memory collection will lead to instability or even crash of the program or system. The Java language does not provide a display operation to free allocated memory.
2. Talk about Java garbage collection.
In Java, programmers do not need to display to free the memory of an object, but the virtual machine does it itself. In the JVM, there is a garbage collection thread, which is low priority and does not execute under normal circumstances, but only when the VIRTUAL machine is idle or the current heap is out of memory, which sweeps through objects that are not referenced by any of them and adds them to the collection for collection.
3. How to determine whether an object is alive or not?
There are two ways to determine whether an object is alive or not:
- Reference counting sets a reference counter for each object. It increments the counter every time the object is referenced, and decreases it by one when the reference fails. When an object’s reference counter is zero, the object is not referenced, which is a “dead object” and will be garbage collected. Reference counting method has A defect that it cannot solve the problem of circular reference. That is, when object A references object B, and object B references object A, then the reference counter of object A and object B is not zero, and garbage collection cannot be completed. Therefore, mainstream virtual machines do not use this algorithm.
- The idea of this algorithm is to start from an object called GC Roots and search down. If an object is not connected to GC Roots by any reference chain, it indicates that the object is unavailable.
There are several objects that can be used as GC Roots in Java:
1. Objects referenced in the virtual machine stack 2. Objects referenced in the static attribute of the method area class 3. Although these algorithms can determine whether an object can be reclaimed, an object ratio is not necessarily reclaimed when the above conditions are met. When an object is unreachable to GC Root, it is not immediately recycled, but rather in a dormant phase, and is marked twice before it can actually be recycled. If the object does not have a reference chain with GC Root in the reachability analysis, then it will be marked for the first time and filtered based on whether it is necessary to execute the Finalize () method. Objects that do not override the Finalize () method or have already been called by a virtual machine are considered unnecessary. If it is necessary for the object to execute finalize(), then the object will be put ina pair Queue called f-queue, and the virtual machine will trigger a Finalize() thread to execute, which is of low priority, and the virtual machine will not promise to wait until finalize() is finished. This is because if Finalize () is executed slowly or deadlocks occur, the F-queue will wait forever, causing the crash of the memory reclamation system. The GC marks the object in the f-queue a second time, at which point the object is removed from the collection to be collected.
4. Advantages and principles of garbage recycling. Talk about two recycling mechanisms.
A notable feature of the Java language is the introduction of garbage collection mechanism, which makes C++ programmers’ most troublesome memory management problem solved. It makes Java programmers no longer need to consider memory management when writing programs. Thanks to a garbage collection mechanism, objects in Java are no longer scoped; only references to objects are scoped. Garbage collection can effectively prevent memory leaks and efficiently use available memory. The garbage collector is usually run as a single low-level thread, cleaning and collecting objects in the heap that have died or have not been used for a long time in unexpected circumstances. The programmer cannot call the garbage collector in real time to collect an object or all objects. Collection mechanisms include generational replication garbage collection, tagged garbage collection, and incremental garbage collection.
5. What is the basic principle of garbage collector? Can the garbage collector reclaim memory right away? Is there any way to proactively notify the virtual machine for garbage collection?
In the case of the GC, when a programmer creates an object, the GC starts monitoring the address, size, and usage of the object. In general, GC records and manages all objects in the heap in a directed graph. This is a way to determine which objects are “reachable” and which are “unreachable”. When the GC determines that some objects are “unreachable,” it is the GC’s responsibility to reclaim those memory Spaces. You can. Programmers can manually execute System.gc() to tell the GC to run, but the Java language specification does not guarantee that the GC will execute.
What is Distributed Garbage Collection (DGC)? How does it work?
DGC is called distributed garbage collection. RMI uses DGC for automatic garbage collection. Because RMI contains references to remote objects across virtual machines, garbage collection is difficult. DGC uses reference counting algorithms to provide automatic memory management for remote objects.
7. What is the difference between a serial collector and a throughput collector?
The throughput collector uses a parallel version of the new generation garbage collector for medium – and large-data applications. The serial collector, on the other hand, is sufficient for most small applications, which require roughly 100 megabytes of memory on modern processors.
8. When can objects be garbage collected in Java?
When an object becomes untouchable to the application that currently uses it, it can be reclaimed.
9. Briefly describe the Java memory allocation and reclamation policy and the Minor and Major GC.
- Objects are allocated in the Eden area of the heap first
- Big object goes straight to the old age
- When the Eden area does not have enough space to allocate, the virtual machine performs a Minor GC. Minor GC usually occurs in the Eden area of Cenozoic era. The objects in this area have a short lifetime, with a high frequency of GC occurrence and a relatively fast recovery rate. Full /Major GC occurs in the old generation. Normally, the old generation GC does not trigger a Minor GC, but can be configured to do a Minor GC before Full GC to speed up the old generation collection.
10. What are the garbage collectors in the JVM? What are their characteristics?
New generation garbage collector
Serial collector
The Serial collector can only use one thread for garbage collection, and all worker threads need to stop working while the garbage collection thread is complete before other threads can resume working.
Using algorithms: Copy algorithms
ParNew collector
Features: The ParNew garbage collector is a multithreaded version of the Serial collector. To take advantage of the CPU’s multi-core multithreading, the ParNew collector can run multiple collection threads for garbage collection. This can improve the efficiency of the garbage collection process.
Using algorithms: Copy algorithms
Parallel avenge
The Parallel Collector is a multi-threaded garbage collector, but the Collector is different from ParNew.
The Parallel Scavenge collector has different concerns than other collectors. Other collectors, such as ParNew and CMS, are primarily concerned with reducing the time of garbage collection. The Parallel Insane focuses on controlling the throughput of a system run. By throughput, we mean the amount of CPU time spent running the application as a percentage of total CPU time. Throughput = code run time/(code run time + garbage collection time). If the total CPU time of the virtual machine running is 100 minutes and the garbage collection time is 1 minute, then the throughput is 99%.
Using algorithms: Copy algorithms
Old age garbage collector
Serial Old collector
The Serial Old collector is an older version of the Serial collector. This collector is used primarily as a legacy garbage collector in client applications and can also be used as a garbage collector in server applications.
Use the algorithm: mark-tidy
Parallel Old collector
Features: The Parallel Old collector is an older version of the Parallel Scavenge collector. The collector was developed in the JDK1.6 release, so prior to the JDK1.6 collector, The New generation of the Parallel Scavenge can only be used in conjunction with the Serial Old, a single-threaded collector. The Parallel Old Garbage collector, like the Parallel Insane collector, is a throughput-focused garbage collector that, in conjunction with the Parallel Insane Collector, can implement a throughput-first garbage collection strategy for Java heap memory.
Use the algorithm: mark-tidy
CMS collector
Features: CMS collector is currently the better garbage collector in the old collector. CMS is a Concurrent Mark Sweep, which, as the name suggests, is a Concurrent collector that uses a “mark-sweep” algorithm.
The CMS garbage collector is a collector whose goal is to obtain the minimum pause times. As shown below:
As can be seen from the figure, the working process of the CMS collector can be divided into four stages:
- CMS Initial Mark phase
- CMS Concurrent Mark phase
- Re-marking (CMS Remark) phase
- (CMS Concurrent sweep) phase
Use algorithm: copy + mark clear
other
G1 garbage Collector
Features: Main steps: initial mark, concurrent mark, re-mark, copy clear.
Use algorithm: copy + mark collation
11. What are the methods of garbage collection in Java?
Mark-sweep: This is the most basic of the garbage collection algorithms, as the name suggests. The idea is to mark which objects are to be collected and then collect them collectively. This approach is simple, but there are two main problems:
- Low efficiency, marking and cleaning efficiency is very low;
- This can result in a large number of discrete memory fragments, causing a GC action to be triggered prematurely when a program allocates larger objects because there is not enough contiguous memory.
Replication algorithm: in order to solve the problem of efficiency, will be available memory replication algorithm according to the capacity is divided into two equal parts, and then using only one piece, every time when a piece of memory used up, it will also live objects are copied to the second piece of memory, then one-time clear out the first block of memory, and then on the second object is copied to the first block. But this way, the cost of memory is too high, and you basically waste half of your memory every time. Therefore, the algorithm was improved. Instead of 1:1, the memory region was divided into 8:1:1 three parts. The larger memory region was Eden region, and the remaining two smaller memory regions were Survior region. If Eden is full, objects will be copied to the second memory block, and then Eden will be cleared. If there are too many surviving objects so that there are not enough survivors, these objects will be copied to the old age through the allocation guarantee mechanism. (Java heap is divided into new generation and old generation)
Mark-collation: this algorithm is mainly to solve the problem of mark-clearing, resulting in a large amount of memory fragmentation; When the object survival rate is high, the efficiency of the replication algorithm is also solved. The difference is that it now moves the recyclable objects to one end when it clears objects, and then clears objects beyond the end boundary, so there is no memory fragmentation.
Generational collection: Most current virtual machine garbage collection uses this method, which divides the heap into new generation and old generation based on the life cycle of the object. In the new generation, due to the short life of objects, a large number of objects will die every time they are recycled, so the replication algorithm is adopted at this time. Subjects in the old age had a higher survival rate and no extra space to allocate guarantees.
Class loading
1. What are class loaders and what are they?
A block of code that implements retrieving the binary stream of a class by its permission name is called a classloader. There are four main types of loaders: 1. Bootstrap ClassLoader is used to load Java core class libraries, which cannot be directly referenced by Java programs. 2. Extensions Class Loader: It is used to load Java extension libraries. The Implementation of the Java Virtual machine provides an extension library directory. The class loader finds and loads Java classes in this directory. 3. System Class Loader: It loads Java classes according to the CLASSPATH of the Java application. In general, Java application classes are loaded by it. Through this. GetSystemClassLoader () to get it. 4. User-defined ClassLoader is implemented by inheriting java.lang.ClassLoader.
2. Class loader parent delegate model mechanism?
Basic definition: The workflow of the parent delegate model is: If a class loader received the request of the class loading, it won’t go to the load the first class, but give parent to complete the request, in turn up, as a result, all of the class loading request should be passed to the top finally start the class loader, only when the parent didn’t find the required class, the child loader will try to load the class.
Parent delegate mechanism:
-
When an AppClassLoader loads a class, it first does not attempt to load the class itself. Instead, it delegates the classloading request to the parent class loader, ExtClassLoader.
-
When ExtClassLoader loads a class, it doesn’t try to load the class itself in the first place. Instead, it delegates the class loading request to BootStrapClassLoader.
-
If the BootStrapClassLoader fails to load, the ExtClassLoader is used to try loading.
-
If the ExtClassLoader also fails to load, AppClassLoader is used to load it. If the AppClassLoader also fails to load, ClassNotFoundException is reported.
As shown below:
Parental delegation:
- Reloading of classes can be avoided by hierarchies with priority;
- To ensure the safe and stable running of Java programs, Java core API defined types will not be arbitrarily replaced.
3. What are Class files? What are the main information structures of a Class file?
A Class file is a set of binary streams in 8-bit byte base units. Each data item is arranged in strict order. The Class file format uses a pseudo-structure similar to the C language structure to store data. Such pseudo-structures have only two data types: unsigned numbers and tables. Unsigned number: is the basic data type. U1, U2, U4, u8 represent unsigned numbers of 1 byte, 2 byte, 4 byte, and 8 byte respectively, which can be used to describe narrative numbers, index references, quantity values, or string values encoded according to UTF-8. Table: A compound data type consisting of unsigned numbers or other tables as data items. All tables habitually end with _info.
Fourth, JVM tuning
1.JVM data run area, which causes OOM condition?
Except the data run area, other areas may cause OOM condition. Stack overflow: Java. Lang. OutOfMemoryError: Java heap space stack overflow: Java. Lang. StackOverflowError permanent generation overflow: Java. Lang. OutOfMemoryError: PermGen space
2. What are the JVM parameters commonly used online?
Data area Xms: initial heap size Xmx: maximum heap size Xss: Stack size of each Java thread XX:NewSize=n: Set the size of the young generation XX:NewRatio=n: Set the ratio of the young generation to the old generation. For example, is 3, indicating that the ratio of the young generation to the old generation is 1:3, and the young generation accounts for 1/4 of the sum of the young generation and the old generation. XX: SurvivorRatio=n: ratio of Eden zone to two Survivor zones in the young generation. Notice that there are two Survivor zones. For example, 3 means Eden: Survivor= 3:2, and a Survivor zone accounts for 1/5 of the whole young generation. XX: MaxPermSize=n: Set the persistent generation size. Collector set XX:+UseSerialGC: Set serial collector XX:+UseParallelGC: : Set parallel collector XX:+UseParalledlOldGC: Set parallel generation collector XX:+UseConcMarkSweepGC: XX:+PrintGC: prints brief GC information XX:+PrintGCDetails: prints GC details XX:+PrintGCTimeStamps: prints GC time stamps
3. Common tools provided by the JVM
JPS: Used to display the local Java process, you can view the local running several Java programs, and display their process number. Command format: JPS jinfo: Runtime environment parameters: Java System attributes, JVM command line parameters, Java Class Path, etc. Command format: jinfo process PID Jstat: a command line tool used to monitor VM running status information. Command format: jstat -gc 123 250 20 jstack: Displays the running status and status of all threads in the JVM. Command format: jstack process PID Jmap: Views the physical memory usage of the running JVM (for example, which objects are generated and the number of them). Command format: jmap [option] PID
The last
Java backend interview questions summarized a nearly 500 page PDF document, welcome to follow my official account: Win the world with Java architecture, reply [2020] to receive these organized materials! If you like the article, remember to pay attention to me. Thank you for your support!