JVM face common questions and answers

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

java virtual machine is mainly divided into a zone:

District method:
1. sometimes become permanent generation garbage collection in this area are rare, but it does not mean that GC does not occur, primarily GC in here is to uninstall method in the constant pool area and type of
2 the data information, the constants, variables and static-time compiler to compile the code and other methods zone mainly used to store the virtual machine has been loaded class.
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 is dynamic, that is not necessarily constant determined at compile time, generating the constant presence of the constant pool will run.

Virtual machine stack:
1. virtual machine stack is what we usually call stack memory, it is a java service method, each method creates a stack frame at the time of execution, used to store the local variable table, operand stack, dynamic Links and methods for export and other information.
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 (a point to the address of the bytecode instructions), and object reference, object reference that there may be a pointer to the starting address of the object, there may be representing an object a handle associated with the object or location. Local variable memory space required between the compiler determines
4. The main function of the operand stack for storing operands and results of arithmetic operation, which is different from the local variables accessed by an index table, but the stack push and embodiment
5. each stack frame contains a pointer to the runtime constant pool, the stack frame of reference associated method, it is to hold a reference to support dynamic method call in the connection procedure. the dynamic linking is symbolic references in the constant pool run into a direct reference.

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

Heap
java heap is shared by all the threads of a memory is created when the virtual machine is started, almost all object instances are created here, this area is often garbage collection occurs.

The program counter
small memory space, by changing the bytecode instruction count value may be selected when a need to perform the work of the bytecode interpreter, branching, looping, branching, exception handling, and recovery functions are threads need to rely on the completion of this counter . The memory 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)

Determining whether a live objects in two ways:
1. Reference counting
when a so-called reference counting is to give each object a reference counter provided, whenever there is a place to reference the object, it will increment a counter, reference failure, counter on the minus one. When an object reference counter is zero, indicating that this object is not referenced, or "dead objects", will be garbage collected.
Reference counting has a flaw that can not resolve the circular reference problem, that is to say when the object A references object B, the object B and the object referenced by a, then the time a, B object reference counter is not zero, thus causing garbage collection can not be completed, so the mainstream of the virtual machine is not used in this algorithm.

2. reachability algorithm (reference chain method)
idea of the algorithm is: an object is called from a GC Roots begin searching down, if an object is not connected to any reference GC Roots chain, 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 ratio of the object is not necessarily recovered. When an object is unreachable GC Root, and the object 
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 in the reachability analysis with GC Root rEFERENCE chain, then the time will be first labeled and screened once, whether the filter condition is necessary to perform a finalize () method. When the object does not cover the finalize () method has been called or virtual machine, then that is not necessary.
If the object is necessary to perform a finalize () method, then the object will be placed in a called F-Queue to queue, the virtual trigger a Finalize () thread to execute, this thread is a low priority, and virtual machine will not promise 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 have been waiting for, resulting in a collapse of the memory recovery system. GC F-Queue object is in a second labeled, then, the object will be removed "will recover" set wait recovered.

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 the JVM, there is a garbage collector thread, it is a low priority, under normal circumstances will not be implemented only when the virtual machine is idle or the current lack of heap memory, will trigger the execution, those who have not been any sweeping surface referenced object, and adds them to the collection to be recovered, recovered.

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 what is thought to be recycled object tag, then uniform recycling. This method is simple, but there will be two main issues: 1. efficiency is not high, marking and removal efficiency is very low; 2 will produce a large number of discrete memory fragmentation, resulting in the allocation procedure after large objects Since there is insufficient contiguous memory prematurely trigger a GC operation.
  2. Replication algorithm:
    In order to solve the efficiency problem, the copy algorithm according to available memory capacity is divided into two equal parts, and then uses only one of them, when a copy runs out of memory, it will also live objects to a second memory block , and then a second clear finished disposable 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 the memory area cross-Eden, two small remainder memory area called Survior area. Eden precedence every time zone, if the Eden area is full, the object will be copied to the second memory block area, and then clear the Eden area, this time surviving if too many objects, so that when Survivor is not enough, these objects will pass guarantee mechanism for distributing copies to the old era. (java heap is divided into the old and the new generation's)
  3. Mark - finishing
    the algorithm is to solve the mark - sweep, a large amount of memory fragmentation problems; when the object is high survival rates, but also solve the problem of efficient replication algorithm. Its difference is now clear recoverable object moves to the object when the one end, and then removed the object other than the end boundary, so that a memory fragmentation is not generated.
  4. Generational collection 
    is now mostly a virtual machine garbage collection in this way, it is based on the life cycle of the object, the heap into the new generation and the old era. 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, then the time on the use of replication algorithm. The old era of objects higher survival rates, there is no guarantee extra space is allocated, 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. JMM local memory is an abstract concept, and are not real. It covers the cache, write buffer, registers and other hardware and compiler optimizations. Abstract schematic Java memory model is 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, thread A to the updated local memory shared variables A flushed to main memory to go.
2. Then, the thread B reads to main memory before the thread A has updated the shared variable.
Well written: http://www.infoq.com/cn/articles/java-memory-model-1

16.java class loading process?

java class loading process to go through about 7:
Load
first class loading process when loading, at this stage, to be completed about three things:
1. Get the class of binary stream 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
verification purpose is to ensure that the information in the file byte stream Class of harm to the virtual machine does not return to complete the following four main bell verify at this stage:
1. File format validation: validation of a byte stream for compliance with Class files, the major and minor version number is within the range of the current virtual machine, whether there is the 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 with the inherited class and so on.
3. bytecode verification: the entire verification process is the most complex stage, by analyzing the validation data flow and control flow of the program to determine whether the correct semantics, the main body for the authentication method. The: method of the correct type conversion, like the jump instruction is correct.
4. Verify reference symbols: this action takes place behind the resolution process, primarily to ensure that the analysis operation can be performed correctly.

Preparation
preparation phase is allocated memory and initialized to default values for the static variable class, which will be allocated in the memory area method. Preparation stage memory not allocated class instance variables, instance variables will be assigned when an object is instantiated along with the objects in the Java heap.

    public static int value=123;//在准备阶段value初始值为0 。在初始化阶段才会变为123 。

Parsing
the main phase reference symbol to complete the conversion action directly referenced. Action does not necessarily resolve before the initialization operation is completed, there are possible after initialization.

Initialization
final step of initializing the class loader, the front loading type, by addition to defining class loader from participation, the remaining operation is completely dominated by the virtual machine in the loading phase and the user application control. To the initialization phase, really started classes defined in Java code.

17. Description of java class loading mechanism?

The virtual machine data that describes the classes loaded from Class file into memory, and verify the data, and parsing initialization, can be finally formed types java virtual machine directly.

18. The parent class loader delegation model mechanism?

When a class is received load request class, the class would not themselves go to the load, but it will be delegated to the parent class, a parent class to load, if the parent class can not be loaded at this time, back to the subclass, the subclass loaded to complete the class.

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 class loader (Bootstrap ClassLoader) to load the java core libraries, and can not be directly referenced java program.
2. The extension class loader (extensions class loader): It is used to load a Java extension library. Implementation of the Java Virtual Machine will provide an extensive library directory. This class loader to find and load Java classes in this directory.
3. The system class loader (system class loader): It is the Java class according to the class to load Java application path (CLASSPATH). In general, class Java applications are made to complete it loaded. You can get it by ClassLoader.getSystemClassLoader ().
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. Priority in the allocation of objects in the heap Eden area.
    2. Large objects directly into the old era.
    3. Long-term survival of the object will go directly to the old era.
      When the Eden area is not enough space to be allocated, the virtual machine to perform a Minor GC.Minor Gc usually occurs in the Eden area of the new generation, object lifetime is short in this area, often occur Gc higher frequency, faster recovery; Full Gc / Major GC occurred in the old years, under normal circumstances, when the trigger will not trigger GC years old Minor GC, but through configuration, you can conduct a Minor GC before the Full GC this can speed up the recovery rate of old age.

Guess you like

Origin www.cnblogs.com/newAndHui/p/11875764.html
Recommended