demo1
public class Main {
public static void main(String[] args) {
{
byte[] bytes=new byte[20*1024*1024];
}
System.gc();
}
}
在 verbose:gc -XX:PrintGCDetails 之後
控制檯顯示:
[GC (System.gc()) [PSYoungGen: 25398K->952K(56320K)] 25398K->21432K(184832K), 0.0200508 secs] [Times: user=0.01 sys=0.00, real=0.02 secs]
新生代佔用內存減小了,但是整個堆內存使用量並沒有減小太多遠沒有20M,那麼這20M去了哪兒?,這說明內存佔用轉移到了老年代,從之後的FullGC可以驗證,老年代20480->21384k,沒有被回收掉,一直在老年代中
[Full GC (System.gc()) [PSYoungGen: 952K->0K(56320K)] [ParOldGen: 20480K->21384K(128512K)] 21432K->21384K(184832K), [Metaspace: 3497K->3497K(1056768K)], 0.0242428 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
Heap
PSYoungGen total 56320K, used 973K [0x0000000781900000, 0x0000000785780000, 0x00000007c0000000)
eden space 48640K, 2% used [0x0000000781900000,0x00000007819f34c0,0x0000000784880000)
from space 7680K, 0% used [0x0000000784880000,0x0000000784880000,0x0000000785000000)
to space 7680K, 0% used [0x0000000785000000,0x0000000785000000,0x0000000785780000)
ParOldGen total 128512K, used 21384K [0x0000000704a00000, 0x000000070c780000, 0x0000000781900000)
object space 128512K, 16% used [0x0000000704a00000,0x0000000705ee22d8,0x000000070c780000)
Metaspace used 3506K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 386K, capacity 388K, committed 512K, reserved 1048576K
demo2
public class Main {
public static void main(String[] args) {
{
byte[] bytes=new byte[20*1024*1024];
}
int a=10;
System.gc();
}
}
GC打印:
[GC (System.gc()) [PSYoungGen: 25398K->968K(56320K)] 25398K->976K(184832K), 0.0194207 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
這個明顯是新生代內存佔用減少的量跟整個堆內存佔用減少的量是相等的,說明直接被回收了
[Full GC (System.gc()) [PSYoungGen: 968K->0K(56320K)] [ParOldGen: 8K->909K(128512K)] 976K->909K(184832K), [Metaspace: 3455K->3455K(1056768K)], 0.0084968 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 56320K, used 973K [0x0000000781900000, 0x0000000785780000, 0x00000007c0000000)
eden space 48640K, 2% used [0x0000000781900000,0x00000007819f3510,0x0000000784880000)
from space 7680K, 0% used [0x0000000784880000,0x0000000784880000,0x0000000785000000)
to space 7680K, 0% used [0x0000000785000000,0x0000000785000000,0x0000000785780000)
ParOldGen total 128512K, used 909K [0x0000000704a00000, 0x000000070c780000, 0x0000000781900000)
object space 128512K, 0% used [0x0000000704a00000,0x0000000704ae34e8,0x000000070c780000)
Metaspace used 3477K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 381K, capacity 388K, committed 512K, reserved 1048576K
demo3
public class Main {
public static void main(String[] args) {
{
byte[] bytes=new byte[20*1024*1024];
bytes=null;
}
System.gc();
}
}
GC:
[GC (System.gc()) [PSYoungGen: 25398K->1048K(56320K)] 25398K->1056K(184832K), 0.0025668 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
同上
[Full GC (System.gc()) [PSYoungGen: 1048K->0K(56320K)] [ParOldGen: 8K->904K(128512K)] 1056K->904K(184832K), [Metaspace: 3498K->3498K(1056768K)], 0.0356986 secs] [Times: user=0.05 sys=0.00, real=0.04 secs]
Heap
PSYoungGen total 56320K, used 973K [0x0000000781900000, 0x0000000785780000, 0x00000007c0000000)
eden space 48640K, 2% used [0x0000000781900000,0x00000007819f34c0,0x0000000784880000)
from space 7680K, 0% used [0x0000000784880000,0x0000000784880000,0x0000000785000000)
to space 7680K, 0% used [0x0000000785000000,0x0000000785000000,0x0000000785780000)
ParOldGen total 128512K, used 904K [0x0000000704a00000, 0x000000070c780000, 0x0000000781900000)
object space 128512K, 0% used [0x0000000704a00000,0x0000000704ae2258,0x000000070c780000)
Metaspace used 3511K, capacity 4498K, committed 4864K, reserved 1056768K
class space used 387K, capacity 390K, committed 512K, reserved 1048576K
總結:
用過的變量如果比較大,最好是置爲null,否則,如上的大數組就會一直佔用着內存,因爲引用一直是存在的.