Garbage Collection按代回收执行过程

一、 What is Automatic Garbage Collection?

Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects.

  • in use object, or a referenced object,means that some part of your program still maintains a pointer to that object.
  • An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed(回收).

In a programming language like C, allocating and deallocating memory is a manual process. In Java, process of deallocating memory is handled automatically by the garbage collector. The basic process can be described as follows.

Step 1: Marking
The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not.

在这里插入图片描述
Referenced objects are shown in blue. Unreferenced objects are shown in gold. All objects are scanned in the marking phase to make this determination. This can be a very time consuming process if all objects in a system must be scanned.

Step 2: Normal Deletion
Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.
在这里插入图片描述
The memory allocator holds references to blocks of free space where new object can be allocated.
Step 2a: Deletion with Compacting
To further improve performance, in addition to deleting unreferenced objects, you can also compact(紧凑) the remaining referenced objects. By moving referenced object together, this makes new memory allocation much easier and faster.
【compact:V to pack or join closely together; compress; condense】
在这里插入图片描述

二、Why Generational Garbage Collection?

通过广泛研究面向对象实现的应用,发现一些特点:

  • 大部分对象的生存时间都很短
  • 新生代很少引用生存时间长的对象

在这里插入图片描述

三、JVM Generations

在这里插入图片描述

  • The Young Generation is where all new objects are allocated and aged. When the young generation fills up(确切来讲,区域是eden,时间是不能为新对象分配内存时), this causes a minor garbage collection.(也就是说创建对象速度越快,minor GC越频繁) Minor collections can be optimized assuming a high object mortality rate.(依据高死亡率来优化minor GC) A young generation full of dead objects is collected very quickly. Some surviving objects are aged and eventually move to the old generation.(当到达在Yong Generation最大年龄对象会到Old Generation)

  • Stop the World Event - All minor garbage collections are “Stop the World” events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events.若大部分Eden对象是垃圾并且不会到Survivor/Old区域,那么停掉线程的时长是negligible(微不足道)。相反,会耗费大量时间。

  • The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a major garbage collection.

  • Major garbage collection are also Stop the World events. Often a major collection is much slower because it involves all live objects. So for Responsive applications, major garbage collections should be minimized. (对于响应式应用来讲,要尽量major GC操作)Also note, that the length of the Stop the World event for a major garbage collection is affected by the kind of garbage collector that is used for the old generation space.(停掉线程影响时长会和GC算法有关)

  • The Permanent generation contains metadata(方法区的元数据) required by the JVM to describe the classes and methods used in the application. Note that Perm Gen is not part of Java Heap memory.The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods be stored here.

Classes may get collected (unloaded) if the JVM finds they are no longer needed and space may be needed for other classes. The permanent generation is included in a full garbage collection.(Full GC清除整个堆,包含Young space and Old space)

四、The Generational Garbage Collection Process

1. First, any new objects are allocated to the eden space. Both survivor spaces start out empty.
在这里插入图片描述
2. When the eden space fills up, a minor garbage collection is triggered.
在这里插入图片描述
3. Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared.
(将被引用对象放到S0,并且年龄+1;未被引用对象也就是垃圾,被清除。从而使得eden区域是空白的)
在这里插入图片描述
4. At the next minor GC, the same thing happens for the eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1). In addition, objects from the last minor GC on the first survivor space (S0) have their age incremented and get moved to S1. Once all surviving objects have been moved to S1, both S0 and eden are cleared. Notice we now have differently aged object in the survivor space.
(简单来讲,eden、S0依然存活→S1,并且年龄+1。从而将eden、S0空闲出来)
在这里插入图片描述
5. At the next minor GC, the same process repeats. However this time the survivor spaces switch. Referenced objects are moved to S0. Surviving objects are aged. Eden and S1 are cleared.
(eden、S1依然存活→S0,并且年龄+1。从而将eden、S1空闲出来)

6. This slide demonstrates promotion. After a minor GC, when aged objects reach a certain age threshold (8 in this example) they are promoted from young generation to old generation.
在这里插入图片描述
7. As minor GCs continue to occure objects will continue to be promoted to the old generation space.
在这里插入图片描述
8. So that pretty much covers the entire process with the young generation. Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.
在这里插入图片描述

参看:https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
http://www.andrew-programming.com/2018/10/15/minor-gc-vs-major-gc-vs-full-gc/
https://plumbr.io/blog/garbage-collection/minor-gc-vs-major-gc-vs-full-gc

五、Example: To Learn Garbage Collector Mechanism in Java

class Student{
int a;
int b;

  public void setData(int c,int d){
    a=c;
    b=d;
  }
  public void showData(){
    System.out.println("Value of a = "+a);
    System.out.println("Value of b = "+b);
  }
  public static void main(String args[]){
    Student s1 = new Student();
    Student s2 = new Student();
    s1.setData(1,2);
    s2.setData(3,4);
    s1.showData();
    s2.showData();
    //Student s3;
    //s3=s2;
    //s3.showData();
    //s2=null;
    //s3.showData();
    //s3=null;
    //s3.showData();
  }
}

在这里插入图片描述

五、GC算法在实际中应用

GC算法

当前商业虚拟机的垃圾收集都采用“分代收集”(Generational Collection)算法,这种算法根据对象存活周期的不同将内存划分为几块。一般是把Java堆分为新生代和老年代,这样就可以根据各个年代的特点采用最适当的收集算法。在新生代中,每次垃圾收集时都发现有大批对象死去,只有少量存活,那就选用复制算法,只需要付出少量存活对象的复制成本就可以完成收集。而老年代中因为对象存活率高、没有额外空间对它进行分配担保,就必须使用“标记—清理”或者“标记—整理”算法来进行回收。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章