Java programmer interview BAT manufacturers, can not escape the JVM, JVM latest 20 interview questions

Many people say JVM is not really to learn? JVM interviewer will ask questions? Is it important?

Which is very important.

With the development of the Internet, high concurrent high availability, fast response becomes necessary software, and JVM has closely associated with these.

We enjoy the benefits of the Java virtual machine at the same time, you should also get to know and think "these technical features is how to achieve," to understand the bottom of the principles. Only familiar with the JVM, you can in the face of OutOfMemory and other abnormalities, will not go, do not look ignorant to force the Internet to find a solution, even if finally started to change a few parameters to solve the problem, it is still foggy.

In fact, "why bother to learn the Java Virtual Machine," this question, and "Why learn data structures and algorithms" is the truth: We must first of its profits. It can be said, Java Virtual Machine is every engineer Java Advanced raise the weapon, you want to rise, you want to delve technology, not always want to stay in a simple development, or are you doing Java performance analysis, tuning your work, , Java virtual machine is definitely a boost sword.

1. The memory model and partition, you need to detail what to put in each area.

JVM stack area and the heap area is divided into, and method area, the objects instantiated on the heap, which references on the stack, class information class constant pool (static variable and constant static) method and the like on the area

new:

Method regions: main storage class information, the constant pool (static and static variables constant), the compiled code (bytecode) data such as

Heap: initialized object member variables (the kind of non-static variables), all object instances and arrays to be allocated on the heap

Stack: the stack structure is composed of stack frames, calling a method with a press-frame local variable table stored thereon, the operand stack, and other information for export, it is stored in the local variable table 8 together with a large base-type application type, it is a pointer to the address

Native method stacks: the main method for the Native Service

Program Counter: Record the current line number of execution threads

2. heap inside the partition: Eden, survival (from + to), old time, their own characteristics.

Stack which is divided into the old generation and the new generation (java8 canceled permanent generation using Metaspace), the new generation area comprising Eden + Survivor, survivor which is divided into areas from and to, when the memory recovery, if the copy algorithm, copy from from to to, when one or more times after GC, surviving objects are moved to the tenured, when the JVM memory is not enough, will trigger Full GC, clean up the area JVM elderly

When the area is full of newborn trigger YGC, first surviving objects into one Survice area, then clean up the garbage. Because if only clean up the object to be deleted, this will lead to memory fragmentation, it is generally Eden will be completely clean, then come up memory. GC then the next time, it will use a next Survive, so recycling. If there is a particularly large objects, the new generation does not fit, it will guarantee the use of old age, directly into old's inside. Because the JVM think, survival time is generally more distant large object in general.

3. Object creation method, the object of memory allocation, access the location of the object.

new object

Two kinds 4.GC determination method:

Reference counting: means that if a place on the object referenced +1, -1 if it fails, but when the JVM is not 0 will be recovered in this manner, can not be determined because each circular reference (A reference B , B referenced a) of the case

Chain reference method: by means of a GC ROOT object (object region or the like of variable references -static static variable) to determine if there is a link to reach GC ROOT to explain, it can not reach the GC ROOT described can be recovered

What is 5.SafePoint

For example, when the GC have to wait until Java thread into the safepoint when VMThread to begin GC,

The end of the cycle (to prevent a cycle time has not entered safepoint, while other threads are waiting for it to enter the safepoint)

Method returns before

After the call to invoke a method

Throw abnormal position

6.GC three collection methods: Mark Clear, finishing mark, copy, principles and characteristics of the algorithm were used in any place, if let you optimize collection methods, what ideas?

First mark, then clear after marking is completed, the efficiency is not high, it will produce debris

Replication algorithm: divided into 8: Eden District 1 and District survivor, is mentioned above YGC

Tags to organize: After labeling is completed, all surviving objects move toward one end

7.GC collector What? Features CMS collector and G1 collector.

Parallel Collector: serial collector uses a separate thread to collect, GC pause time when service

Serial collector: Secondary recovery using multiple threads to execute

CMS collector is based on the "mark - sweep" algorithm to achieve, after several mark will be cleared

G1 is based on the whole "mark - sorting" algorithm of the collector, from the local (between Region) view is based on the "copy" of the algorithm

8.Minor GC and Full GC, respectively, when did it happen?

When the new generation of memory is not enough when the MGC occurs also called YGC, JVM is not enough memory occurs FGC

9. several commonly used memory debugging tools: jmap, jstack, jconsole, jhat

jstack can see the current stack, jmap view memory, jhat information dump heap

mat (eclipse should also find out)

10. The process of loading several classes

Loading, validation, preparation, resolution, initialization. Then use and unloaded

To load generated by the fully qualified class name of the object into memory, and then verify this class file, including text

Checking member format, metadata validation bytecode verifier and the like. It is ready to allocate memory for this object. Resolution is the symbol

No direct reference is converted to a reference (pointer reference), is the initialization code starts execution constructor

11.JVM memory which divided into several zones, each zone is what role?

java virtual machine is mainly divided into a zone:

Method area:

1. sometimes become permanent generation garbage collection in this area are rare, but it does not mean that GC does not occur here

GC carried out mainly in the unloading area of ​​the method and type of constant pool

2. The method area is mainly used for information, constants, static variables and real-time compiler class storage has been loaded virtual machine

Code data and the like.

3. The area is shared by the threads.

The method has a zone runtime constant pool for storing literals and static compiler to generate a reference symbol. The constant pool

Dynamic, that is not necessarily constant determined at compile time, runtime generated will be constant this constant presence

Pool.

Virtual machine stack:

1. virtual machine stack is what we usually call stack memory, it is a java service method, each method at the time of execution

It creates a stack frame for storing information local variable table, the operand stack, and a method for dynamic link export.

2. Virtual machine thread stack is private, its life cycle and the same thread.

3. The local variable table stored in the basic data types, returnAddress type (pointer to a bytecode instruction

Site) and object references, this object reference there may be a pointer to the starting address of the object, there may be representative of

Object handle associated with an object or a position of. Local variables required memory space determined between the compiler

4. The main function of the operand stack is used to store operands and results of arithmetic operation, which is different from the local variable table by cable

Led to the visit, but push and pop way

5. Each stack frame contains a reference to the runtime constant pool of the stack frame associated method, it is to hold a reference to

Dynamic connection support method calls the process of dynamic linking is the symbolic constant pool references at runtime into direct

References.

Native method stacks

Native method stacks and stacks virtual machine is similar, except for the native method stacks Native method service.

stack

java heap is shared by all the threads of a memory is created when the virtual machine is started, almost all object instances in which

In creation, so the area is often garbage collection occurs.

Program Counter

Small memory space, by changing the bytecode count value may be selected when a need to perform the work of the bytecode interpreter

Instruction, branching, looping, branching, exception handling, and recovery functions are threads need to rely on the completion of this counter. The inner

Deposit area is the only java virtual machine specification does not specify any area OOM situation.

12 and determines whether the survival of a target? (GC determination method or object)

Determine whether an object is alive in two ways:

1. Reference counting

Reference counting is called to set a reference to each object counter, whenever a reference to the object where

, The counter will be incremented by one, when referring to fail, a counter is decremented. When an object's reference count is zero, said

Ming This object is not referenced, or "dead objects", will be garbage collection.

Reference counting method has a defect circular reference problem can not be solved, that is, when the object A reference object B, the object

B and referrer object is A, then the time A, B object reference counter is not zero, it can not cause complete garbage back

Income, so the mainstream virtual machine is not used in this algorithm.

2. reachability algorithm (reference chain method)

Idea of ​​the algorithm is: objects are called GC Roots from a start search down, if an object to GC

Roots when no reference chain is connected, then this object is not available.

GC Roots can be used as an object in java are the following:

VM stack referenced objects

Area class method static properties referenced objects

The method of constant pool area referenced objects

JNI native method stacks objects referenced

While these algorithms may determine whether an object can be recovered, but when the above conditions are satisfied, a different subject than

It will certainly be recycled. When an object is unreachable GC Root, and this object

It will not immediately be recovered, but a reprieve for the stage, to be a real recovery needs to undergo two marks

If the object is not referenced in the GC Root strand reachability analysis, then the time will be the first mark and for

First screening, the screening condition is whether it is necessary to perform the finalize () method. When the object is not covered finalize () method

Or it has been called a virtual machine, then that is not necessary.

If the object is necessary to perform a finalize () method, then the object will be called in a team of F-Queue

Column, the virtual trigger a Finalize () thread to execute, this thread is a low priority, and the virtual machine will not bear

Snow has been waiting for it to run to completion, it is as if finalize () or slow implementation of a deadlock occurs, it will result in F-

Queue queue has been waiting for, resulting in a collapse of the memory recovery system. GC object is performed in F-Queue

The second is marked, this time, the object will be removed "will recover" collection, wait for recovery.

13. Description of java garbage collection?

In java, the programmer is not required to release a displayed object memory, but executed by the virtual machine itself. in

JVM, there is a garbage collector thread, it is a low priority, under normal circumstances will not be implemented, only virtual

The proposed unit is idle or the current heap memory is insufficient to trigger the execution, sweep surfaces that are not used by any object reference, and

To be recovered are added to the collection, recovery.

The method of garbage collection 14.java What?

1. Mark - Clear:

This is the most basic garbage collection algorithm, according to the name you know, it's thought to be what is the mark

The recovered objects, and then uniform recycling. This method is simple, but there are two major problems: 1. efficiency

High mark and sweep efficiency is very low; 2 will produce a large number of discrete memory fragmentation, leading to later in the program

The allocation of a large object, because there is no sufficient contiguous memory prematurely trigger a GC operation.

2. Copy the algorithm:

In order to address efficiency issues, by the copy algorithm available memory capacity is divided into two equal parts, and then a time

Wherein the one used when one runs out of memory, it will also copy live objects to the second memory block, then

End of a clear one-time memory, and then copy the object on the second block to the first block. But this way,

The cost is too high memory, each basically have to waste a general memory.

Thus the algorithm is improved, the memory area is no longer 1: 1 to divide, but the memory is divided into

8: 1: 1 of three parts, a larger share of memory interleaving Eden region, the rest are two smaller memory region is called Survior region.

Eden preferentially used every time zone, if Eden area is full, the object will be copied to the second block memory area, then

After clearing the Eden area, at this time if the surviving objects too, when that is not enough Survivor, these will be on

Like copied to the old era by allocating guarantee mechanism. (Java heap is divided into the old and the new generation's)

3. Mark - Finishing

The algorithm to solve the mark - sweep, a large amount of memory fragmentation problems; when the higher survival rate of target

When, but also solve the problem of efficient replication algorithm. Its difference is in the clear when the object will now be back

Receiving an end of the object to move, and then clear out the objects other than the end border, so that a memory fragmentation is not generated.

4. generational collection

Now they use a virtual machine garbage collection in this way, it is based on the life cycle of the object, into the heap newborn

And on behalf of years old. In the new generation, because of the short lifetime of the object, there will be a large number of objects recovered each time to die, that

Why then on the use of replication algorithm. The old era of objects higher survival rates, there is no extra space is allocated Tam

Paul, it is possible to use the mark - or finishing mark - sweep.

15.java memory model

java memory model (JMM) is a control mechanism in communication between threads .JMM defines an abstract relationship between the thread and the main memory.

Shared memory variables between threads in main memory (main memory), each thread has a private local

Memory (local memory), local memory store a copy of the thread to read / write shared variables. Local memory is

JMM an abstract concept, and it is not real. It covers the cache, write buffer, registers and other hardware

Parts and compiler optimizations. Abstract diagram of Java memory model are as follows:

From the map view, between threads A and B are as threads to be communicated, it must go through the following two steps:

1. First, the updated A thread local memory A shared variables to be flushed to main memory.

2. Then, the thread B reads to main memory before the thread A has updated the shared variable.

16.java class loading process?

java class loading process to go through about 7:

load

When loading the first class loading process, at this stage, to be completed about three things:

1. Such binary stream acquired by the fully qualified name of a class.

2. Structure of the static storage binary stream data structure is converted to the runtime method.

3. Generate Class object class in memory, such as data access to the inlet.

verification

The purpose of verification is to ensure that the information byte stream Class file does not endanger back to the virtual machine. At this stage the main completion

Bell verify the following four:

1. File format validation: validation of a byte stream for compliance with Class files, such as major and minor version number in the current virtual

Machine range, if there is a constant in the constant pool type is not supported.

2. Metadata authentication: described bytecode information semantic analysis, such as whether the class has a parent class, whether or not integrated

Inherited class and so on.

3. bytecode verification: the entire verification process is the most complex stage, analyzed by verifying the data flow and control flow,

Determine whether the correct program semantics, mainly for the verification method body. The: method of the type conversion is correct, jump

Instruction is correct and so on.

4. Verify reference symbols: this action takes place behind the resolution process, primarily to ensure that the analysis operation can be executed correctly

Row.

ready

Preparation phase of memory is allocated and initialized to default values ​​as a static class variables, which will go to the memory area in the process

Line distribution. Preparation phase not allocated class instance variables in memory, when an object instance variables will be instantiated as objects

Distributed with the Java heap.

public static int value = 123; // preparation phase value initial value is 0. In the initialization phase will change

123.

Resolve

In this phase, the completion of the operation of a direct conversion symbol reference cited. Action does not necessarily resolve the completion of the initialization operation

Before, there might after initialization.

initialization

The last step in the initialization class is loaded, the front loading type, except that in the loading phase by a user application

Composer custom class loader to participate, and completely dominated the rest of the action is controlled by a virtual machine. To the initialization phase, really

Java program code starts execution defined in the class.

17. Description of java class loading mechanism?

The virtual machine data that describes the classes loaded from Class file into memory, and data validation, parsing, and initialize, most

Finally formed types may be used directly java virtual machine.

18. The parent class loader delegation model mechanism?

When a class has received class loading request, not myself go to load the class, but will delegate it to the parent, the parent class

To load, if the parent class can not be loaded at this time, back to the subclass, the subclass of the class to complete loading.

19. What is the class loader class loader What?

Realized by code block based naming authority acquired binary byte stream class called a class loader.

There are about four types of loader:

1. Start the class loader (Bootstrap ClassLoader) for loading java core library, the program can not be directly java

References.

2. The extension class loader (extensions class loader): It is used to load a Java extension library. Java Virtual Machine

Implementation will provide an extension library catalog. This class loader to find and load Java classes in this directory.

3. The system class loader (system class loader): It is based on the classpath Java application (the CLASSPATH)

To load Java classes. In general, class Java applications are made to complete it loaded. able to pass

ClassLoader.getSystemClassLoader () to get it.

4. The user-defined class loader, class java.lang.ClassLoader achieved by way of inheritance.

20. DESCRIPTION java memory allocation and recovery rates and policies Minor GC and Major GC

1. Object preferentially partitioned Eden heap area.

2. Large objects directly into the old era.

3. Long-term survival of the object will go directly to the old era.

When there is not enough space allocated Eden area, the virtual execution through a Minor GC.Minor Gc

Often occurs in the Eden area of ​​the new generation, object lifetime is short in this area, we tend to have higher frequency of occurrence Gc,

Faster recovery; Full Gc / Major GC occurred in the old years, under normal circumstances, triggering years old GC

When not trigger Minor GC, however, can be carried out once before the Full GC Minor by configuration

GC This can speed up the recovery rate of old age.

Guess you like

Origin www.cnblogs.com/Java-JJ/p/12628529.html