System 類的 gc() 方法運行垃圾Collector。通過這種方法,JVM 盡最大努力重新使用丟棄對象的內存,以便快速重新使用。
用法
public static void gc()
例子1
import java.awt.Window;
public class SystemGCExample1 {
public static void main(String[] args) {
for (int i = 0; i< 100; i++) {
new Window(null);
}
System.out.println("before garbage collection = "+Window.getWindows().length);
System.gc();
System.out.println("after garbage collection = "+Window.getWindows().length);
}
}
輸出:
before garbage collection = 100 after garbage collection = 0
例子2
public class SystemGCExample2 {
public static void main(String[] args) {
String i=null; //value of i is null
System.gc();
System.out.println(i); //value of i after gc
}
}
輸出:
null
相關用法
- Java System getProperty()用法及代碼示例
- Java System getenv()用法及代碼示例
- Java System getSecurityManager()用法及代碼示例
- Java System getProperties()用法及代碼示例
- Java System getLogger()用法及代碼示例
- Java System clearProperty()用法及代碼示例
- Java System loadLibrary()用法及代碼示例
- Java System console()用法及代碼示例
- Java System arraycopy()用法及代碼示例
- Java System exit()用法及代碼示例
- Java System setIn()用法及代碼示例
- Java System nanoTime()用法及代碼示例
- Java System setErr()用法及代碼示例
- Java System identityHashCode()用法及代碼示例
- Java System inheritedChannel()用法及代碼示例
- Java System load()用法及代碼示例
- Java System setOut()用法及代碼示例
- Java System lineSeparator()用法及代碼示例
- Java System setSecurityManager()用法及代碼示例
- Java System setProperties()用法及代碼示例
注:本文由純淨天空篩選整理自 Java System gc() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。