我有这段代码:
public static void main(String[] args) throws Exception {
Thread.sleep(5000);
System.out.println("Creating array...");
Integer[] integers = new Integer[Integer.MAX_VALUE/32];
Thread.sleep(5000);
System.out.println("Destroying array");
integers = null;
//System.gc ();
//System.runFinalization ();
Thread.sleep(60000);
}
当我运行这段代码后,大约5秒钟后,我将在活动监视器中看到分配给Java进程的内存为268 MB。
当我取消注释(System.gc和其后面的一行)后,在控制台中看到“Destroying array”后,分配的内存增加到278 MB。
我可以理解,内存没有被释放,因为System.gc()只是一个JVM的提示,但是为什么会增加10MB?在这个时刻已经加载了什么到内存中?
System.gc()
可以通过命令行标志禁用。但一般来说,在那些配置下(即hotspot的默认配置),System.gc()
不是一个noop,它会在本来不会发生GC的时候强制进行GC。一件事是实现允许做什么,另一件事是实现在正常情况下实际上如何行为。 - the8472