2012-01-19 73 views
4

有沒有可能看到哪些對象將被垃圾收集器刪除?我不需要該對象的內容,但該對象的類是必要的。java垃圾收集器 - 「獲取」已刪除的對象

我嘗試編寫一個實時應用程序,它創建並刪除了很多對象,過了一段時間後應用程序變慢了。目前我不確定這是我的代碼還是來自外部庫的問題。所以完美的是一個輸出,它標識所有已經被刪除的類以及它們的「數量」(有多少個這些對象已被刪除)。

我希望有人能幫助我。

最佳, 邁克爾

+0

我會使用內存分析器。這可以告訴你在哪裏分配對象以及爲什麼它們被保留(這可能是導致速度減慢的原因)我使用YourKit作爲它的易用性和更強大的功能。 VisiualVM是免費的。 –

回答

5

您可以嘗試使用VisualVM監視您的應用程序。有一個插件可以提供有關垃圾收集器活動的信息。

http://visualvm.java.net/plugins.html

+0

Visualvm解決了這個問題 - 謝謝。 – Michael

+0

BTW:Visualvm也包含在JDK中:jdk ..../bin/jvisualvm.exe(對於windows) – Michael

3

如何重寫finalize方法爲你的對象和記錄類有名字?但要小心,這可能會阻止垃圾收集對象。

你的類可能是這個樣子:

public class MyObject { 

    @Override 
    public void finalize() throws Throwable { 
     logger.debug("Object of class {} being garbage collected", this.getClass().getName()); 
    } 
} 

這裏是Object.finalize()方法簽名和文檔。

/** 
* Called by the garbage collector on an object when garbage collection 
* determines that there are no more references to the object. 
* A subclass overrides the <code>finalize</code> method to dispose of 
* system resources or to perform other cleanup. 
* <p> 
* The general contract of <tt>finalize</tt> is that it is invoked 
* if and when the Java<font size="-2"><sup>TM</sup></font> virtual 
* machine has determined that there is no longer any 
* means by which this object can be accessed by any thread that has 
* not yet died, except as a result of an action taken by the 
* finalization of some other object or class which is ready to be 
* finalized. The <tt>finalize</tt> method may take any action, including 
* making this object available again to other threads; the usual purpose 
* of <tt>finalize</tt>, however, is to perform cleanup actions before 
* the object is irrevocably discarded. For example, the finalize method 
* for an object that represents an input/output connection might perform 
* explicit I/O transactions to break the connection before the object is 
* permanently discarded. 
* <p> 
* The <tt>finalize</tt> method of class <tt>Object</tt> performs no 
* special action; it simply returns normally. Subclasses of 
* <tt>Object</tt> may override this definition. 
* <p> 
* The Java programming language does not guarantee which thread will 
* invoke the <tt>finalize</tt> method for any given object. It is 
* guaranteed, however, that the thread that invokes finalize will not 
* be holding any user-visible synchronization locks when finalize is 
* invoked. If an uncaught exception is thrown by the finalize method, 
* the exception is ignored and finalization of that object terminates. 
* <p> 
* After the <tt>finalize</tt> method has been invoked for an object, no 
* further action is taken until the Java virtual machine has again 
* determined that there is no longer any means by which this object can 
* be accessed by any thread that has not yet died, including possible 
* actions by other objects or classes which are ready to be finalized, 
* at which point the object may be discarded. 
* <p> 
* The <tt>finalize</tt> method is never invoked more than once by a Java 
* virtual machine for any given object. 
* <p> 
* Any exception thrown by the <code>finalize</code> method causes 
* the finalization of this object to be halted, but is otherwise 
* ignored. 
* 
* @throws Throwable the <code>Exception</code> raised by this method 
*/ 
protected void finalize() throws Throwable { } 
+0

這個想法看起來相當不錯。對於我的問題,它不會工作,因爲我認爲,問題來自外部庫,我不能覆蓋finalize方法。但是,謝謝你的回答! – Michael

+0

@Michael在這種情況下,您必須像其他人所建議的那樣使用一些分析器。 – adarshr

2

豈不是垃圾回收的對象更加有趣?

Anway,像VisualVM這樣的內存分析器就是你真正需要的。它可以準確顯示您的應用程序中存在每個類的對象數量,還可以通過比較GC之前和之後的堆轉儲來識別垃圾收集的類和對象縣。

1

我建議你使用任何java profileor,比如JProfiler,Yourkit等。 很容易看到垃圾收集對象的數量以及對象的類型。

1

垃圾收集線程運行在低優先級。所以這個線程沒有輪到做清理任務。

此外,不保證垃圾收集線程將始終運行。在你的應用程序中,垃圾收集的低優先級線程沒有機會在應用程序線程上執行。所以堆不會從未使用的對象中清除,因此應用程序由於堆大小有限而變慢。

您可以通過重寫public void finalize()方法來計算垃圾收集對象的數量。檢查javadoc瞭解更多詳情

我建議你使用一些java分析器來診斷內存問題。

+0

感謝您的回答。是否有可能以更高的優先級運行垃圾收集?問題是,我已經有一個大約5GB的堆空間,而且由於我創建了一個刪除了很多對象,垃圾收集對於那個應用程序來說非常重要。 – Michael

+0

您無法更改Garabage收集器的優先級。你所能做的就是儘可能在對象使用後立即取消引用。也不要創建不必要的對象,並在需要時進行懶惰創建(Singleton設計模式)。也可以使用Factory Design模式來控制和緩存對象創建 –