1. 程式人生 > >將不使用的物件置為NULL可以加快回收嗎

將不使用的物件置為NULL可以加快回收嗎

一直聽說將不用的物件置為NULL,可以加迅被GC回收,但又聽見另一種觀點說置NULL不會被GC.倒底會不會加速呢,本人不太懂GC的機制,但也可以知道置NULL有沒有用,寫個程式碼測試一下就OK了.貼程式碼:
public class LoopObject
{
public static void main(String[] args)
{
LoopObject lo;
for (int i = 0; i < 100000; i++)
{
lo = new LoopObject();
}

}
}


javac LoopObject.java
為了公平,執行了五次執行命令,每執行一次,複製記錄一下:
java -verbosegc LoopObject
結果如下:
[GC 896K->161K(5056K), 0.0013781 secs]
[GC 896K->161K(5056K), 0.0013644 secs]
[GC 896K->161K(5056K), 0.0013768 secs]
[GC 896K->161K(5056K), 0.0013536 secs]
[GC 896K->161K(5056K), 0.0013715 secs]


被置為空後的程式碼:
public class LoopObject
{
public static void main(String[] args)
{
LoopObject lo;
for (int i = 0; i < 100000; i++)
{
lo = new LoopObject();
lo = null;
}
}
}


javac LoopObject.java
為了公平,執行了五次執行命令,每執行一次,複製記錄一下:
java -verbosegc LoopObject
結果如下:
[GC 896K->161K(5056K), 0.0013662 secs]
[GC 896K->161K(5056K), 0.0013609 secs]
[GC 896K->161K(5056K), 0.0013539 secs]
[GC 896K->161K(5056K), 0.0013664 secs]
[GC 896K->161K(5056K), 0.0014042 secs]

結果對比,用前段程式碼的GC回收時間減去後一段程式碼的回收時間:
0.0000119、0.0000035、0.0000229、-0.0000128、-0.0000327
從結果上看,感覺差不多。。。。。。可能是例子太簡單的,那麼再寫個複雜的程式碼

import java.util.Hashtable;

public class LoopObject
{
public static void main(String[] args)
{
Hashtable<Object, Object> ht;
for (int i = 0; i < 100000; i++)
{
ht = new Hashtable<Object, Object>();
}
}
}

javac LoopObject.java
執行了三次執行命令,每執行一次,複製記錄一下:
java -verbosegc LoopObject
結果如下:
[GC 896K->161K(5056K), 0.0013631 secs]
[GC 1057K->161K(5056K), 0.0004871 secs]
[GC 1057K->161K(5056K), 0.0002996 secs]
[GC 1057K->161K(5056K), 0.0001650 secs]
[GC 1057K->161K(5056K), 0.0001509 secs]
[GC 1057K->161K(5056K), 0.0001557 secs]
[GC 1057K->161K(5056K), 0.0001597 secs]
[GC 1057K->161K(5056K), 0.0001625 secs]
[GC 1057K->161K(5056K), 0.0002215 secs]
[color=green][GC 1057K->161K(5056K), 0.0001568 secs][/color]

[GC 896K->161K(5056K), 0.0013520 secs]
[GC 1057K->161K(5056K), 0.0005030 secs]
[GC 1057K->161K(5056K), 0.0003084 secs]
[GC 1057K->161K(5056K), 0.0001522 secs]
[GC 1057K->161K(5056K), 0.0001523 secs]
[GC 1057K->161K(5056K), 0.0001546 secs]
[GC 1057K->161K(5056K), 0.0001626 secs]
[GC 1057K->161K(5056K), 0.0001473 secs]
[GC 1057K->161K(5056K), 0.0002252 secs]
[color=blue][GC 1057K->161K(5056K), 0.0001502 secs][/color]

[GC 896K->161K(5056K), 0.0013446 secs]
[GC 1057K->161K(5056K), 0.0004839 secs]
[GC 1057K->161K(5056K), 0.0003243 secs]
[GC 1057K->161K(5056K), 0.0001527 secs]
[GC 1057K->161K(5056K), 0.0001585 secs]
[GC 1057K->161K(5056K), 0.0001537 secs]
[GC 1057K->161K(5056K), 0.0001669 secs]
[GC 1057K->161K(5056K), 0.0002167 secs]
[GC 1057K->161K(5056K), 0.0001568 secs]
[color=red][GC 1057K->161K(5056K), 0.0001760 secs][/color]


import java.util.Hashtable;

public class LoopObject
{
public static void main(String[] args)
{
Hashtable<Object, Object> ht;
for (int i = 0; i < 100000; i++)
{
ht = new Hashtable<Object, Object>();
ht = null;
}
}
}

重新編譯後並執行
javac LoopObject.java
執行了三次執行命令,每執行一次,複製記錄一下:
java -verbosegc LoopObject

[GC 896K->161K(5056K), 0.0013605 secs]
[GC 1057K->161K(5056K), 0.0004848 secs]
[GC 1057K->161K(5056K), 0.0003503 secs]
[GC 1057K->161K(5056K), 0.0001571 secs]
[GC 1057K->161K(5056K), 0.0001541 secs]
[GC 1057K->161K(5056K), 0.0001685 secs]
[GC 1057K->161K(5056K), 0.0001570 secs]
[GC 1057K->161K(5056K), 0.0001546 secs]
[GC 1057K->161K(5056K), 0.0001561 secs]
[color=green][GC 1057K->161K(5056K), 0.0001341 secs][/color]

[GC 896K->161K(5056K), 0.0014766 secs]
[GC 1057K->161K(5056K), 0.0004738 secs]
[GC 1057K->161K(5056K), 0.0003499 secs]
[GC 1057K->161K(5056K), 0.0001562 secs]
[GC 1057K->161K(5056K), 0.0001534 secs]
[GC 1057K->161K(5056K), 0.0001450 secs]
[GC 1057K->161K(5056K), 0.0001547 secs]
[GC 1057K->161K(5056K), 0.0001607 secs]
[GC 1057K->161K(5056K), 0.0001585 secs]
[color=blue][GC 1057K->161K(5056K), 0.0001300 secs][/color]

[GC 896K->161K(5056K), 0.0013647 secs]
[GC 1057K->161K(5056K), 0.0004726 secs]
[GC 1057K->161K(5056K), 0.0003491 secs]
[GC 1057K->161K(5056K), 0.0001531 secs]
[GC 1057K->161K(5056K), 0.0001437 secs]
[GC 1057K->161K(5056K), 0.0001537 secs]
[GC 1057K->161K(5056K), 0.0001530 secs]
[GC 1057K->161K(5056K), 0.0001282 secs]
[GC 1057K->161K(5056K), 0.0001714 secs]
[color=red][GC 1057K->161K(5056K), 0.0001593 secs][/color]

從這次的結果中不難看出,多加了一行置空的程式碼,還真有點效果,只是不知道這些效果對系統性能有沒有什麼提高呢