JVM and the principle of garbage collection mechanism

JVM

Java Virtual Machine The Java virtual machine (JVM) is an essential mechanism for running Java programs. The JVM implements the most important feature of the Java language: platform independence. Principle: The compiled Java program instructions are not directly executed on the CPU of the hardware system, but are executed by the JVM. The JVM shields the information related to the specific platform, so that the Java language compiler only needs to generate the target bytecode (.class) running on the JVM, and it can run on various platforms without modification. When the Java virtual machine executes the bytecode, it interprets the bytecode into machine instructions for execution on a specific platform. So achieve java platform independence. It is a reliable guarantee that Java programs can be seamlessly ported between multiple platforms, and it is also a security check engine for Java programs (and also performs security checks).

The JVM is the interface between the compiled Java program (.class file) and the hardware system (after compilation: javac is the Java language compiler included in the JDK. This tool can compile source files with a suffix of .java into a suffix of .java Bytecode named .class that can run on the Java Virtual Machine.)


JVM architecture:

Image taken from  http://javapapers.com/java/java-garbage-collection-introduction/

JVM = class loader classloader + execution engine + runtime data area runtime data area
classloader loads the class file on the hard disk into the runtime data area in the JVM, but it is not responsible for whether the class file can be executed, and this is The execution engine is responsible.


classloader

Function: Load .class file
classloader There are two ways (timing) to load class:
1. Implicit: During the running process, when an object is generated in the new method, the classLoader is implicitly called to the JVM
2. Explicit: through class.forname () dynamic loading

Parent Delegation Model:

The class loading process adopts the parent delegation mechanism, which can better ensure the security of the Java platform.
This model requires that in addition to the Bootstrap class loader at the top level, the rest of the class loaders should have their own 父类加载器. The child class loader and the parent class loader are 不是以继承(Inheritance)的关系implemented, but 组合(Composition)关系the code of the parent loader is reused. Each class loader has its own namespace (composed of the classes loaded by the loader and all parent class loaders. In the same namespace, the full name of the class (including the package name of the class) will not be the same. two classes; in different namespaces, there may be two classes with the same full name of the class (including the package name of the class)

The working process of the parent delegation model is as follows:

1. The current ClassLoader first checks whether the class has been loaded from the classes it has loaded, and if it has been loaded, it directly returns the originally loaded class.

每个类加载器都有自己的加载缓存,当一个类被加载了以后就会放入缓存,
等下次加载的时候就可以直接返回了。

2. When the class to be loaded is not found in the cache of the current classLoader, the parent class loader is entrusted to load it. The parent class loader adopts the same strategy, first check its own cache, and then entrust the parent class of the parent class to load, all the time. To bootstrap ClassLoader.
3. When all parent class loaders are not loaded, the current class loader will load it and put it into its own cache, so that it can return directly next time there is a loading request.

The benefits of using this model to organize the relationship between class loaders: The
main purpose is to 安全性avoid dynamically replacing some core Java classes, such as String, with classes written by the user, and also avoid 重复加载, because the JVM distinguishes between different classes, not only Just according to the class name, the same class file is loaded by different ClassLoaders, which are two different classes. If they are converted to each other, java.lang.ClassCaseException will be thrown.

Class loader classloader has a hierarchical structure, that is, a parent-child relationship. Among them, Bootstrap is the father of all class loaders. As shown in the figure below:
Bootstrap class loader: Parent class
When running the Java virtual machine, this class loader is created, which is responsible for loading the core class libraries of the virtual machine, such as java.lang.* and so on. For example java.lang.Object is loaded by the root class loader. It should be noted that this class loader is not written in java language, but written in C/C++.
Extension class loader:
This loader loads some extension classes beyond the basic API.
AppClass Loader:
Loads application and programmer-defined classes.

In addition to the loader that comes with the above virtual machine, users can also customize their own class loader ( User-defined Class Loader). Java provides the abstract class java.lang.ClassLoader, all user-defined class loaders should inherit the ClassLoader class.

This is a good example of the autonomous ecosystem of the JVM division of labor.

http://www.importnew.com/6581.html


execution engine

Role: Execute bytecode, or execute native methods

runtime data area

The JVM runtime data area (JVM Runtime Area) actually refers to the division and allocation of the JVM memory space by the JVM during its operation. The JVM divides the data into 6 areas for storage at runtime.

All the programs written by the programmer are loaded into 运行时数据区域, and the different categories are stored in heap, java stack, native method stack, PC register, method area.

The functions and stored contents of each part are described below:

1. PC program counter: a small memory space, which can be regarded as 线程the line number indicator of the currently executed bytecode, NAMELY stores the JVM instructions that each thread will execute next, if the method is native, Then no information is stored in the PC register. Java's multi-threading mechanism is inseparable from the program counter, and each thread has its own PC in order to complete the switching of different thread contexts.

2. Java virtual machine stack: Like the PC, the Java virtual machine stack is also thread-private. Each JVM thread has its own Java virtual machine stack, which is created at the same time as the thread, and its life cycle is the same as that of the thread. The virtual machine stack describes that Java 方法执行的内存模型when each method is executed, it will create a 栈帧(Stack Frame)local variable table, operand stack, dynamic link, method exit and other information at the same time. 每一个方法被调用直至执行完成的过程就对应着一个栈帧在虚拟机栈中从入栈到出栈的过程.

3. Local method stack: Similar to the role of the virtual machine stack, the virtual machine stack serves for the virtual machine to execute and execute java methods, while the local method stack serves the local methods used by the virtual machine.

4. Java heap: A storage area shared by all threads, created when the virtual machine starts. It is the area where the JVM stores object instances and array values. It can be considered that the memory of all objects created by new in Java is here distribute.

The Java heap is created when the JVM starts. Various objects are stored in the heap. These objects are managed by the Automatic Storage Management System (also known as the "Garbage Collector"). . These objects need not and cannot be explicitly destroyed.

JVM divides Heap into two parts: New Generation and Old Generation

Note:

  • The heap is shared by all threads in the JVM, so allocating object memory on it needs to be locked, which is why the new overhead is relatively large.
  • In view of the above reasons, in order to improve the efficiency of object memory allocation, Sun Hotspot JVM allocates a separate space for the created thread, which is also called TLAB
  • TLAB only acts on the Eden Space of the new generation, so when writing Java programs, it is usually more efficient to allocate multiple small objects than large objects

5. Method area
The method area, like the heap area, is a memory area shared by each thread. It is used to store each 类的结构信息, such as the runtime constant pool, member variables and method data, the bytecode content of constructors and ordinary functions, and also Including some special methods used in class, instance, interface initialization. When the developer obtains information through the methods such as getName and isInstance in the Class object in the program, these data all come from the method area.

The method area is also globally shared and created when the virtual machine starts. It will also be GCed under certain conditions. This area corresponds to the Permanent Generation persistent generation. XX: PermSize specifies the size.

6. Runtime constant pool
Its space is allocated from the method area, and it stores the fixed constant information, method and domain reference information in the class.


GC

javapapers
Java garbage collection is an automatic process to manage the runtime memory used by programs. By doing it automatic JVM relieves the programmer of the overhead of assigning and freeing up memory resources in a program.
One advantage of java over C is, Memory space can be allocated and reclaimed automatically by its own JVM.

What is GC?
The garbage collection mechanism is implemented by the garbage collector Garbage Collection GC, which is a background daemon process. What makes it special is that it is a low-priority process, but its priority can be dynamically adjusted based on memory usage. Therefore, it will automatically run when the memory is low to a certain limit, so as to realize the recovery of memory. This is why the timing of garbage collection is indeterminate.

Why is it designed this way: Because GC is also a process, it also consumes resources such as CPU. If the GC is executed too frequently, it will have a greater impact on the execution of java programs (the java interpreter is not fast), so the designers of JVM choose With irregular gc.

GC is related to: heap in the runtime data area (where object instances will be stored) and the gapage collector method.
During program execution, all object instances are stored in the heap in the runtime data area. When an object is no longer referenced (used), it needs to be reclaimed. During the GC process, these objects that are no longer used are recovered from the heap, so that there will be space for recycling.
GC recycles objects that are no longer used in memory, and GC calls for recycling 方法-- 收集器garbage collector. Since GC consumes some resources and time, after analyzing the life cycle characteristics (eden or survivor) of objects, Java adopts the 分代method of The collection of objects is performed to shorten the pause caused by the GC on the application.

Before the memory can be reclaimed by the garbage collector, some cleanup is required.
Because garbage collection gc can only reclaim the memory (on the heap) applied for through the new keyword, but the memory on the heap is not completely allocated through the new application. There are also some native methods (usually C methods that are called). If this part of "special memory" is not released manually, it will lead to memory leak, and gc cannot reclaim this part of memory.
Therefore, it is necessary to use native methods such as free operations in finalize, and then use the gc method. The GC method shown issystem.gc()

Garbage Collection Technology

Method 1: Reference counting method. Simple but slow. The flaw is: it cannot handle the case of circular references.
Method 2: stop and copy. Low efficiency, large space required, advantages, no debris will be generated.
Method 3: mark and sweep algorithm (mark and sweep). Faster, takes up less space, and generates a lot of debris after the mark is cleared.

How is it done in the JAVA virtual machine?
Java's approach is clever, what we call an "adaptive" garbage collector, or an "adaptive, generational, stop-copy, mark-sweep" garbage collector. It will choose different processing methods according to different environments and needs.

heap composition

Since GC needs to consume some resources and time, after analyzing the life cycle characteristics of objects, Java adopts a 分代method to collect objects, that is, to collect objects according to the methods of the new generation and the old generation, so as to make the most of the objects. Possibly shorten the pause caused by GC on the application.
The heap is composed of three regions/generations: (It is understandable that over time, object instances continue to change the level in the heap, a bit like a grade)

  1. Young Generation

    1. Any new instance of Eden Space entering the runtime data area will be stored here
    2. The S0 Suvivor Space has existed for a long time, and the instances that have not been cleared after garbage collection were moved from Eden to S0
    3. S1 Survivor Space is the same, and the instance that exists for a longer time is moved from S0 to S1
  2. The same is true for Old Generation/tenured
    . For instances that exist for a longer time, the objects are reclaimed multiple times and have not been cleared, so they are moved from S1 to tenured.

  3. Perm stores the method area of ​​the runtime data area

Different generations of Java use different GC algorithms.

  1. Minor collection:
    The new generation Young Generation uses semi-space for copying collection (Copying collection) of the data in Eden and Survivor, and moves the objects that survive multiple garbage collections in the original Survivor to Tenured.
  2. Major collection will perform Minor collection, and Tenured generation will perform marked compression collection.


To note that:
this moving work is done by GC, which is also the source of the name of garbage collector, not garbage cleaner. GC is responsible for moving instances in the heap and reclaiming storage space.

How GC works

The JVM uses different garbage collection mechanisms for the new generation and the old generation respectively

What is garbage?

Those in Java 不可达的对象would become 垃圾. So what is unreachable? In fact, there is no way to get 引用to the object anymore. There are mainly the following situations that make the object become garbage:
1. 非线程的对象For all the active threads can not access the object, then the object will become garbage.
2. For the thread object, the above conditions are met, and the thread has not been started or has been stopped.

例如: 
(1)改变对象的引用,如置为null或者指向其他对象。 
   Object x=new Object();//object1 Object y=new Object();//object2 x=y;//object1 变为垃圾 x=y=null;//object2 变为垃圾 (2)超出作用域 if(i==0){ Object x=new Object();//object1 }//括号结束后object1将无法被引用,变为垃圾 (3)类嵌套导致未完全释放 class A{ A a; } A x= new A();//分配一个空间 x.a= new A();//又分配了一个空间 x=null;//将会产生两个垃圾 (4)线程中的垃圾 class A implements Runnable{ void run(){ //.... } } //main A x=new A();//object1 x.start(); x=null;//等线程执行完后object1才被认定为垃圾 这样看,确实在代码执行过程中会产生很多垃圾,不过不用担心,java可以有效地处理他们。 

The JVM divides object references into four types. Different object reference types will cause the GC to use different methods for recycling:
(1) Strong references: By default, objects use strong references
(GC will not recycle)
(2) Soft reference: Soft reference is an application provided in Java that is more suitable for caching scenarios
(it will be GC only when the memory is not enough)
(3) Weak reference: it will be recycled by GC during GC
(4) Virtual reference: it will be recycled by GC during GC

Please indicate the source of the content http://segmentfault.com/blog/exploring/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325343606&siteId=291194637