[英]Is onDestroy() also indicate to the Garbage collector that the Activity object should be collected?
[英]When JavaFX tab should be collected by garbage collector?
我有TabPane
與Tab
,它出現。 我正在創建新的Tab
,將其添加到我的TabPane
。 然后我關閉選項卡並TabPane
編程方式從TabPane
刪除它。
Tab
未設置為null
。 這是正確的行為嗎? 如何清理/破壞這個Tab
對象?
垃圾收集器不會破壞您仍然持有引用的對象。
假設你有一個局部變量或一個字段myTab
,那么只需分配
myTab = null;
之后讓垃圾收集者完成他的工作。 如果您對此對象實例有其他引用,請對它們執行相同的操作。
你的標簽可能仍然有一個持有引用的EventHandler。
tab.setOnClosed(null);
例:
// I make setOnClose handler in my constructor
// My set on close handler will close my project
tab.setOnClosed(new EventHandler<Event>() {
@Override
public void handle(Event t) {
closeProject();
}
});
// Somehow my setOnClose handler still exists after closing the tab
public void closeProject() {
// Setting the setOnClose handler to null fixes the garbage collection issue for me
tab.setOnClosed(null);
tab.setUserData(null);
tab = null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.