JVM Frequently Asked Questions (a)

(A) java class loading process?

Java class loading process to go through seven.

  • load

  Load is the first class loading process, at this stage, needs to be done three things:

    • Securing such binary stream through a fully qualified name of the class
    • The static storage structure is converted into a binary stream data structure to the runtime method
    • The Class generating object in memory, such as the data access entry
  • verification

  The purpose of verification is to ensure that the information does not endanger Class file stream to a virtual machine, at this stage they need to accomplish four things.

    • File format validation: validation of a byte stream for compliance with Class files, such as major and minor version number is within a range of the virtual machine, if there is the constant in the constant pool type is not supported
    • Metadata authentication: described bytecode information semantic analysis, such as whether there is a parent class, whether inherited not inherited class, whether inherited not inherited class
    • Bytecode verification: verified by analyzing the data flow and control flow of the program to determine whether the correct semantics, the main body is directed to a method of verification, such as: the type of conversion process is correct, the jump instruction is correct
    • Verification of symbolic references: symbolic references to verify the correct implementation of this action is in, mainly to ensure that the resolution process of the action takes place behind the resolution process
  • ready

  This stage is to allocate memory and initialize static class variables to default values, which are allocated in the memory area method.

  Preparation phase is not allocated class instance variables in memory, along with instance variables will be assigned to objects in the heap when the object is instantiated.

  E.g:

  public static int number = 666; 
  initial value for the preparation stage number is 0, it will not become until the initialization phase 666
  • Resolve

  This process is above the verification process verification process symbolic references. The action is not necessarily resolved before the initialization operation is completed, there may be completed after

  • initialization

  The last step is to initialize a class loading, loading the above class, except that in the loading phase of the user application can define the class loader from the outside through participation,

  The remaining action completely dominated and controlled by the virtual machine. To the initialization phase, began in earnest the implementation of the class definition in Java code

  • use
  • Uninstall

(Two), JVM load the class file the principle mechanism of what is it?

  In Java will only load a class (class) file into the JVM to run.

  When running the specified program, JVM will compile the generated .class file is loaded in accordance with certain rules and requirements into memory, and the organization became a full Java application, the process is completed by the class loader, specifically, a classloader and its subclasses to achieve.

  Class loader is itself a class, its essence is the class files from the hard disk into memory.

 

  

  Class loading to load and display the implicit load.

  Implicitly refer to programs loaded when creating a new object using other means, it invoked implicitly class loader corresponding to the class loader into the JVM.

  Loading refers to display the required classes loaded into the JVM by calling class.forName () method.

  

  Any project is composed of many of the classes, when the program starts just need to use the classes loaded into the JVM, other classes will be loaded only used when needed.

  The advantage of this approach is that load faster, the memory overhead savings program is running.

 

  Each corresponds to a class or interface .class files, which can be seen as one unit can be dynamically loaded, so that when only a portion of the class is modified, only need to recompile the class changes, without the need recompile all files, thus speeding up the compilation speed.

 

  Load class is dynamic, and it will not be a one-time class will all fully loaded before running, but to ensure the base class running (for example, the base class) fully loaded into the JVM, as to when the other class, in need of It was loaded.

  What are the main steps of the class loader is it?

  • Loading: find the corresponding class file based search path, and then import
  • link:
    • Check the correctness of the class file to be loaded: Check  
    • Preparation: storage space allocated to the class Static variables  
    • Analysis: The symbolic reference is converted into direct consumption  
  • Initialization: for static variables and initializes code block

Java memory distribution

  • Register: can not control
  • Static fields: static static member variables
  • The constant pool: compile time is determined and stored in the .class files (Final) and constant values ​​of some modified text symbol references (fully qualified name of the class and interface, the name and descriptor fields, and the method name and descriptor)
  • Non-RAM storage: a hard disk permanent storage space
  • Heap memory: create new objects and arrays, there is Java virtual machine to manage the automatic garbage collection, slow access speed,
  • Stack memory: variables and objects of basic types of reference variables (access heap memory address space), fast, can be shared, but the size and survival must be determined, lack of flexibility

Java heap structure look like? What is permanent heap generations?

  JVM heap is the runtime data area, all class instances and arrays is allocated on the heap memory. It is loaded when the JVM starts. Object heap memory is occupied by an automatic memory management system that is the garbage collector.

 

  Heap memory is an object composed of live and dead.

  Live objects is the application can access, it will not be garbage.

  The object of death is the object of the application is not accessible yet have not been out of the garbage collected. Until the garbage collector will reclaim these objects fall, they will always occupy heap memory space.

GC is the What? Why GC?

  GC (Garbage Collection) garbage collector, memory handling is where programmers prone to problems, and forget or false memory recovery program will lead to instability or even system crashes,

  Java GC function provides automatic monitoring of the object exceeds the scope so as to achieve the purpose of automatic recovery of memory, Java language does not provide a method of operating the display release allocated memory.

Description of Java garbage collection mechanism.

  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, sweep surfaces that are not referenced by any object, and add them to the collection to recycling, recycling .

How to determine whether an object is referenced?

In two ways: 1, 2 reference count, reachability algorithm method

Reference counting: Reference counting is to give each object a reference counter provided, whenever there is a place to reference the object, the counter will be incremented by one, when referring to fail, a counter is decremented.

      When an object's reference count is zero, indicating that this object is not referenced, or "dead objects", it will be garbage collection.

      But the drawbacks of a reference counting method is this: That when the object A references object B, the object B and referrer object A, then the time A, B object reference counter is not zero, thus causing garbage collection can not be completed, this will cause problems a circular reference.

      So mainstream virtual machine is not used in this algorithm.

Reachability algorithm:

      :( idea of ​​the algorithm is God's perspective) is known as GC Roots from one object to start the search down, if an object is not connected to the GC Roots any reference to the chain, then this object is not available.

  GC Roots can be used as objects in Java are the following:

  • Virtual machines accounted for object references
  • 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, the object will not immediately be recovered , but a reprieve for the stage, to the recovery of the real need to go through two marks.

 

If the object is not referenced in reachability analysis with GC Root of the chain, then the time will be the first mark and conduct a screening , screening is whether the conditions 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 F-Queue called the queue,

Virtual chance to trigger a Finalize () thread to execute, this thread is a low priority, and the virtual machine will not promise has been waiting for it to run to completion, because if the finalize () or slow implementation of the deadlock,

Then it will cause 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.

 

 

Garbage collection principles and advantages.

Java language in a notable feature is the introduction of garbage collection mechanism that allows Java programmers in the preparation of the program no longer need to consider memory management.

 

Because there is a garbage collection mechanism, Java objects are no longer in the "scope" concept, and only the object reference have no "scope." Garbage collection can effectively prevent memory leaks, efficient use of memory can be used.

 

Garbage collector is usually as a separate low-level thread to run, heap memory have died a long time without the use of a clear and recovery under unpredictable circumstances, the programmer can not be called real-time garbage collector for a All objects or objects for garbage collection.

 

Recovery mechanisms have a generation copy garbage collection and marking garbage collection, incremental garbage collection. 

Guess you like

Origin www.cnblogs.com/LBJLAKERS/p/12165103.html