Epsilon(A No-Op Garbage Collector)垃圾回收器控制內存分配,可是不執行任何垃圾回收工做。一旦java的堆被耗盡,jvm就直接關閉。設計的目的是提供一個徹底消極的GC實現,分配有限的內存分配,最大限度下降消費內存佔用量和內存吞吐時的延遲時間。一個好的實現是隔離代碼變化,不影響其餘GC,最小限度的改變其餘的JVM代碼。java
代碼:程序員
public class TestEpsilon {
public static void main(String[] args) {
System.out.println("程序開始");
boolean flag = true;
List<Garbage> list = new ArrayList<>();
long count = 0;
while (flag) {
list.add(new Garbage(list.size() + 1));
if (list.size() == 1000000 && count == 0) {
list.clear();
count++;
}
}
System.out.println("程序結束");
}
}
class Garbage {
private int number;
public Garbage(int number) {
this.number = number;
}
/** * GC在清除對象時,會調用finalize()方法 */
@Override
public void finalize() {
System.out.println(this + " : " + number + " is dying");
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
複製代碼
啓動參數:bash
-Xms100m -Xmx100m
複製代碼
運行程序後,結果以下:微信
程序開始
...
com.gf.demo8.Garbage@15ddf76b : 305097 is dying
com.gf.demo8.Garbage@35e52705 : 305224 is dying
com.gf.demo8.Garbage@32c14bc1 : 305362 is dying
com.gf.demo8.Garbage@7521660a : 305705 is dying
com.gf.demo8.Garbage@f3da16a : 305948 is dying
com.gf.demo8.Garbage@13fc7287 : 306089 is dying
at java.base/java.lang.ref.Finalizer.register(Finalizer.java:66)
at java.base/java.lang.Object.<init>(Object.java:50)
at com.gf.demo8.Garbage.<init>(TestEpsilon.java:28)
at com.gf.demo8.TestEpsilon.main(TestEpsilon.java:14)
...
複製代碼
會發現G1一直回收對象,直到內存不夠用。jvm
啓動參數:ide
UnlockExperimentalVMOptions:解鎖隱藏的虛擬機參數。測試
-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xms100m -Xmx100m
複製代碼
運行程序後,結果以下:this
程序開始
Terminating due to java.lang.OutOfMemoryError: Java heap space
複製代碼
會發現很快就內存溢出了,由於Epsilon不會去回收對象。spa
歡迎掃碼或微信搜索公衆號《程序員果果》關注我,關注有驚喜~ 線程