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堆分爲新生代和老年代,這樣就可以根據各個年代的特點採用最適當的收集算法。在新生代中,每次垃圾收集時都發現有大批對象死去,只有少量存活,那就選用複製算法,只需要付出少量存活對象的複製成本就可以完成收集。而老年代中因爲對象存活率高、沒有額外空間對它進行分配擔保,就必須使用“標記—清理”或者“標記—整理”算法來進行回收。

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