21 days to learn the Java (Java SE Part IV): Java virtual machine, garbage collection

Java Virtual Machine (JVM)

For object-oriented Java-memory analysis, we must first know what the Java Virtual Machine (java virtual machine, JVM) that? JVM is capable of running java virtual machine bytecode. As a programming language virtual machine, in fact, is not only dedicated to the Java language, as long as the compiler-generated files that match the requirements of the JVM to load the compiled file format, in any language can run compiled by the JVM. And by using the Java virtual machine, you can solve the problem of cross-platform.

The basic structure of the JVM

JVM consists of three major subsystems

  • Class loader subsystem
  • Runtime data areas (memory structure)
  • Execution Engine

Class loading mechanism

Class life cycle

Class flowchart lifecycle

1. Load: The .class file is read from disk into memory

2. Connect

2.1 verify: verification of the correctness of the byte code file ( Sum )

2.2 Preparation: static variables to the class of allocating memory, and given a default value

2.3 Analysis: all other classes (statically linked) class loader loads a class referenced

3. Initialization: given static variables like the correct initial value of the preparation phase for the static variable is assigned to a virtual machine default initial value given here is the programmer for the real value of the variable initial allocation, performing static code block

4. Use: Run Program

5. Uninstall: End Program

Type class loader

  • Start class loader (Bootstrap ClassLoader)
    responsible for loading the JRE core class libraries, such as rt.jar under JRE goal, charsets.jar etc.
  • Extension class loader (Extension ClassLoader)
    responsible for loading the JRE extension directory ext jar in class package
  • System class loader (Application ClassLoader)
    is responsible for loading classes in the package path ClassPath
  • Custom loader (User ClassLoader)
    is responsible for loading the user-defined class package path

Type class loader

Class loading mechanism

  • Overall responsibility delegation mechanism
    when a ClassLoader when loading a class, unless another show ClassLoader, that class is dependent and referenced class is also loaded by the ClassLoader

  • Parents delegate mechanism
    refers to delegate the parent class loader to find the target class, in the case of downloading can not find their own path to find and load the target class

  • Advantages parents delegate mode

    1. sandbox security mechanism: for example, to write their own String.class classes will not be loaded, which prevents tampering with the core library is
    2. Free repeated loading class: when the parent ClassLoader already loaded class time, you do not need child ClassLoader loaded once again

Run-time data area (memory analysis)

1. Method region (Method Area) (jdk1.8 formerly called permanent Generation / permanent generation, hereinafter called element space)

(1) JVM is only one way area, shared by all threads

(2) Method stack area is actual only for storing class information associated constants

(3) Method used to store the contents of the program is always constant or only, all the fields and methods of the class bytecode, and some special methods such as constructor, interface code is also defined herein. Briefly, all defined process information are stored in the region, the static variable + constant + class information (Constructor / interface definition) + runtime constant pool (String category) are present in the zone method .

2. Stack (Stack)
memory model to perform the method of Java threads, one thread corresponding to a stack, each method will create a stack frame (while execution for storing local variable table, the operand stack, dynamic link, the method exports information ) garbage collection problem does not exist, the storage characteristics of the stack follow the "last out, last in, first out" as long as the end of a thread stack is released, and the life cycle of the same thread. And the stack is automatically assigned by the system, high speed, the stack is a contiguous memory space.

3. heap (Heap)

(1) stack is used to store the created objects and arrays ( arrays are objects )

(2) JVM is only a heap, is shared by all threads

(3) the stack is not a contiguous memory space, flexible allocation, slow.

(4) while the stack is also the main garbage collector region management, garbage collection, see details below.

4. A native method stacks (Native Method Stack)

And stack effect is very similar, but the difference is the Java stack for the JVM implementation of Java service method, while the native method stacks for the JVM implementation of local (native) method service. Local registration method, a method to load a local library when performing Execution Engine.

The program counter (Program Counter Register)
is a pointer to a method of Method bytecode region (points used to store the address of the next instruction, the instruction code is also about to be performed), the next instruction is read by the execution engine It is a very small memory space, almost negligible

Garbage collection

In the introduction of the Java heap garbage collection mechanism, so that C ++ programmers the most headaches memory management problems immediately. So Java programmers can put more energy into the business logic rather than memory management work, greatly improving the efficiency of development.

Programmers can write their own System.gc(), but this is not a call to the garbage collector, just start the program recommended GC, whether to start or controlled by the garbage collection mechanism.

New Generation (Young Generation)

Like birth, growth and demise of the region, where a class is generated, the application, after collecting the garbage collector, the end of life.
Cenozoic is divided into two parts: Eden District (Eden space) and survivors zone (Survivor Space) , all classes are out in the new Eden area. Surviving area is divided into the From and To area. When the Eden area of the space is used up, the program and the need to create an object, JVM's garbage collector garbage collection (Minor GC) Eden District, the Eden area of the object is no longer applied to other objects were destroyed. Then the remaining region Eden From Survivor object to the region. From Survivor If the area is also full, then the garbage collection area, and then moved to the To Survivor region.

Years old (Old Generation)

After the new generation of object more than once (default 15 times) GC still alive moving to old age area. If the old year was full, Major GC (also can be called Full GC) will happen this time, for memory clean up older areas. If the area after elderly execute a Full GC found still can not save the object, throws OOM (OutOfMemoryError) exception.

Element space (Meta Space)

After JDK1.8, metaSpace replace permanent generations, which is an implementation of the JVM specification region method, except that the metadata area which is not in the virtual machine, but with local memory, the virtual machine on behalf of which the permanent, permanent substituting also belong to the logical structure of the stack, but not physically.
Why removed permanently generations? : Reference official explanation

Released six original articles · won praise 21 · views 2282

Guess you like

Origin blog.csdn.net/qq172387778/article/details/105315011