Preface: Recently, I often received a private letter from my friend asking me if I could help to sort out a LIST of INTERVIEW questions related to JVM. He said that when he went to interview in a big factory, he asked a lot of questions about this, and every time he learned, he could not learn the key points. Here he is, a detailed JVM interview for you to sort out below!
When will stack memory overflow occur?
1. The stack is thread private, and the life cycle of the stack is the same as that of the thread. When each method is executed, a stack frame will be created, which contains local variable table, operand stack, dynamic link, method exit and other information. StackOverFlowError is raised when thread requests exceed the maximum stack depth allowed by the virtual machine. 3. Adjust the -xss parameter to adjust the JVM stack size
JVM memory model
The JVM divides the virtual machine into five areas: program counters, virtual machine stack, local method stack, Java heap, and method area.
Program counter: thread private, is a small memory space, as the current thread line number indicator, used to record the current VIRTUAL machine is executing thread instruction address;
Virtual machine stack: thread-private stack frame is created for each method execution to store local variables, operands, dynamic links, and method returns. StackOverFlowError is raised when thread requests exceed the maximum stack depth allowed by the VIRTUAL machine.
Local method stack: the thread is private, which stores the information of native method. When a thread created by JVM calls native method, the JVM will not create stack frame for the thread in the virtual machine stack, but simply dynamically link and directly call this method.
Heap: The Java heap is a piece of memory shared by all threads. Almost all object instances and arrays are allocated on the heap, so garbage collection often occurs in this area.
Method area: stores loaded class information, constants, static variables, and code data compiled by the immediate compiler. In JDK1.8, the method area is replaced by the metadata area. The original method area is divided into two parts. 1: loaded class information. 2: runtime constant pool. The loaded class information is stored in the metadata area, and the runtime constant pool is stored in the heap;
What does a full GC look like in a JVM? How does the target get promoted to the old age?
Java heap = New generation + old generation; Cenozoic = Eden + Suivivor (S0 + S1), the default allocation ratio is 8:1:1; When Eden’s space is full, a Minor GC is triggered to collect the garbage of the new generation. The surviving objects are allocated to the Survivor area and the large objects (objects that require a large amount of continuous memory) are allocated to the old age. If you survive a Minor GC, your age is +1. If you survive a Minor GC, your age is +1. If you survive a Minor GC, your age is +1. When the age is full and no more objects can be held, a full GC is triggered. The full GC stores the entire heap (both young and old); A Major GC is a GC that occurs in an older age, clearing an older area, often with at least one minor GC;
Garbage collection algorithm in Java?
There are four garbage collection algorithms in Java, which are mark removal, mark collation, copy algorithm and generational collection algorithm. Mark removal method: the first step: the use of reachability to traverse memory, the surviving objects and garbage objects marked; Step 2: Iterate again and recycle all marked objects; Features: efficiency is not good, marking and cleaning efficiency is not high; After marking and clearing, a large number of discontinuous spatial fragments will be generated, which may cause the program to allocate large objects and not find continuous fragments and have to trigger a GC.
Mark finishing method: the first step: the use of reachable to traverse the memory, the surviving objects and garbage objects to mark; Step 2: Move all surviving objects to a segment, reclaim all objects outside the end boundary; Features: Suitable for living objects, less garbage; No space debris is generated in the process of cleaning up;
Copy algorithm: according to the size of the memory is divided into two equal size, each time only use a piece, when a used up, the surviving objects will be moved to another piece, and then remove the used memory space; Features: No space debris; Very low memory usage;
Generational collection algorithms: according to the memory object different survival period, will be divided into a few pieces of memory, the Java virtual machine generally divide memory into new generation and the old generation, in the Cenozoic era, there are a large number of objects and a few dead objects alive, so use replication algorithm, only need to pay a small amount of live object replication costs can be completed collection; In the old age, because the survival rate of the object is very high, there is no extra space for him to allocate guarantee, so the use of mark cleaning or mark sorting algorithm for recycling;
How to determine whether an object is alive or not?
There are two algorithms to judge whether an object is alive or not: 1. 2. Accessibility analysis algorithm; Reference counting method: set a reference counter for each object, when there is a reference to the object, the reference counter is +1, when the reference is invalid, the reference counter is -1; When the reference counter is 0, the object is not referenced, that is, garbage, waiting to be collected. Disadvantages: It cannot solve the problem of circular reference. When A references B and B also references A, the reference of AB object is not 0 and garbage collection cannot be performed. Therefore, mainstream VMS do not use this method.
Reachability analysis searches down from an object called GC Roots. If an object is not connected to GC Roots by any reference chain, the object is not available. In Java, there are several possible GC Roots objects:
Virtual machine stack reference object method area class static attribute reference variable method area constant pool reference object local method stack JNI reference object but an object that meets the above conditions, will not be immediately recycled, also need to be marked twice; First mark: Determine whether the current object has a Finalize () method and the method has not been executed. If it does not exist, mark the object as garbage and wait for recycling. If so, mark the second time; The second tag will put the current object into the F-Queue and generate a Finalize thread to execute the method. The virtual machine does not guarantee that the method will be executed, because if the thread executes slowly or enters a deadlock, the recycle system will crash. If there is no direct or indirect reference to GC Roots after finalize method is executed, the object will be collected.
What kinds of garbage collectors are there and what are their advantages and disadvantages? What’s the difference between CMS and G1?
Garbage recyclers are classified into the following types: Serial, ParNew, Parallel Scavenge, Serial Old, Parallel Old, CMS, G1; Serial: single-threaded collector that must stop the world and use the copy algorithm when collecting garbage.
ParNew: A multithreaded version of the Serial collector that also needs to stop the world and copy the algorithm.
The Insane, the Parallel Scavenge collector, is designed to achieve a controlled throughput. The greatest difference between ParNew and the GC auto-regulation strategy; The vM collects performance monitoring information based on the system running status and dynamically sets these parameters to provide optimal pause time and maximum throughput.
Serial Old: An older version of the Serial collector, a single-threaded collector that uses a mark-collation algorithm.
Parallel Old: An older version of the Parallel Avenge collector, using the multithreaded, mark-collation algorithm.
CMS: is a collector to obtain the shortest recovery pause time as the goal, mark clearing algorithm, operation process: initial mark, concurrent mark, re-mark, concurrent clear, the end of the collection will produce a large number of space debris;
G1: Realization of the tag sorting algorithm, and the operation process mainly includes the following: initial tag, concurrent tag, final tag, and screening and recycling. No space debris can be generated and the pauses can be precisely controlled; G1 divides the whole heap into multiple regions (regions) of equal size. G1 tracks the size of garbage in each Region and maintains a priority list in the background. According to the allowed collection time, the Region with the largest collection value is prioritized to achieve the highest collection efficiency within a limited time.
What is class loading?
The virtual machine loads the data describing the class into memory, verifies, parses, and initializes the data, and finally becomes a class object that can be directly used by the VIRTUAL machine.
Class loading process?
It is mainly divided into the following processes: loading, verification, preparation, parsing, initialization; Loading: loading is divided into three steps: 1. Obtain the binary stream of the class through the full name of the class; 2. Convert the static storage structure of the binary stream into the runtime data structure of the method area; Generate a class object for the class in the heap.
Validation: Verify that the byte stream information in this class file is consistent with the requirements of the VIRTUAL machine and does not threaten the security of the JVM;
Preparation: Allocates memory for static variables of the class object and initializes their initial values;
Analysis: this stage is mainly completed symbol reference into direct reference;
Initialization: During the initialization phase, Java code defined in the class is executed. The initialization phase is the process of calling the class constructor;
What is a class loader? What are the common class loaders?
Class loaders are: the binary byte stream that gets a class by its full class name is called a class loader; Class loaders are classified into the following four types: Start class loaders: used to load Java core class libraries, which cannot be directly referenced by Java programs.
Extension classloader: An extension library used to load Java. Virtual machine implementations of Java provide an extension library directory in which the class loader finds and loads Java classes.
System classloader: It loads classes according to the Java classpath. In general, classes of Java applications are loaded through it.
Custom class loaders: implemented by the Java language and inherited from ClassLoader;
What is the parental delegation model?
When a classloader receives a classload request, it first does not try to load the request itself. Instead, it delegates the request to the parent classloader. Only when the parent classloader fails to find the class in its search scope, the child loader will try to load the class itself.
Why parent delegation model?
To prevent multiple identical bytecodes in memory; Because without parental delegation, the user can define a java.lang.String class by himself, and there is no guarantee that the class is unique;
How to break the parental delegation model?
Custom ClassLoader, inherit ClassLoader class, rewrite loadClass method and findClass method;
Strong reference, soft application, weak reference, virtual reference difference?
Strong references: Strong references are the most widely used references. If an object has a strong reference, the garbage collector never collects it. When there is not enough memory, the garbage collector would rather throw OutofMemoryErrors than collect objects with strong references. We can reclaim a strongly referenced object by explicitly setting it to NULL and making the GC think there is no reference to the object.
SoftReference: a soft application is a reference to some useful but unnecessary objects (SoftReference in Java). An object is returned only if its memory is insufficient. A soft reference can be used in conjunction with a reference queue. If the object referenced by the soft reference is collected by the garbage collector, the virtual machine adds the soft reference to the corresponding reference queue.
WeakReference: WeakReference is used to describe some unnecessary objects, which is represented by WeakReference in Java. In garbage collection, once an object is found to have only soft reference, the object will be reclaimed no matter whether the current memory space is sufficient. A weak reference can be used in conjunction with a reference queue. If the object referenced by a weak reference is garbage collected, the virtual machine adds the reference to the object to the reference queue associated with it.
Virtual reference: A virtual reference is an optional reference that cannot be used to represent the life cycle of an object and can be reclaimed at any time. A virtual reference is mainly used to track the activities of an object being garbage collected. The difference between a virtual reference and a soft reference and a weak reference is that a virtual reference must be used in conjunction with a reference queue. During garbage collection, if an object is found to have only virtual references, the reference of the object will be added to the reference queue associated with it. The program can know whether the referenced object needs to be garbage collected by discovering whether virtual references have been added to the reference queue.
BAT interview question + structure technical information, enhance the soft and hard strength, harvest a high salary good offer
Having said so much, how should study after all, have material or video?
Dachang Java interview questions database, BAT architecture technology database, follow the public number below you can get free
The content of the material covers the BAT architecture technology and the real interview questions, reducing the time you search for information everywhere, into the system of practical technical knowledge, so that you interview, progress is easier.
Java Interview. Java Core Knowledge