簡體   English   中英

JavaFX 8隱藏階段時內存泄漏

[英]JavaFX 8 Memory leak when Hiding Stage

我有一個JavaFX應用程序,當按下X按鈕時,該應用程序最小化到托盤。 我一直在通過VisualVM監視應用程序的內存趨勢。

奇怪的是,當應用程序打開或最小化到任務欄時,內存總是被GC返回到使用的初始內存。 然而,當它被最小化到托盤( stage.hide() systemTray.show()時,存儲器獲取GCed,但在向上的趨勢(泄漏)。

在VisualVM中,Old Gen空間不斷上升,一旦它在一段時間后達到最大值,應用程序將無法響應,並且CPU峰值達到80%。

我注意到如果我通過雙擊托盤圖標等在應用程序上stage.show() ,GC將清除所有內容恢復正常 但是,如果長時間離開,它將無法使GC成為老一代。

堆轉儲顯示javafx.scene.Scene#7javafx.scene.Node[]#2具有最多的保留空間。 如果未隱藏舞台,則兩者都不會出現。 在引用下,它顯示了this[] -> dirtyNodes()

this     - value: javafx.scene.Node[] #2
<- dirtyNodes     - class: javafx.scene.Scene, value: javafx.scene.Node[] #2
 <- value     - class: javafx.scene.Node$ReadOnlyObjectWrapperManualFire, value:  
 javafx.scene.Scene #7

造成這種情況的原因是什么?如何解決這個問題?

我從來沒有找到並回答這個問題。 相反,我會在隱藏上隱藏節點並在視圖上恢復它。 對於密集的動態節點/多個節點,我創建了一個哈希映射來將它們存儲在內存中。

這有點習慣於我在javafx8中處理所有圖形並重新分配隱藏和查看哈希映射。 在現代桌面上,額外的內存和CPU使用率可以忽略不計。 使用這種方法,我在win8 / 10上隱藏了0個cpu使用應用程序和低內存應用程序(~100m)。

Java具有類似弱參考的功能: https//docs.oracle.com/javase/7/docs/api/java/lang/ref/WeakReference.html

軟參考: https//docs.oracle.com/javase/7/docs/api/java/lang/ref/SoftReference.html

這些允許您專門針對VM - >>垃圾收集的內容。

還有,並發API http://winterbe.com/posts/2015/04/07/java8-concurrency-tutorial-thread-executor-examples/

那使用Executor Service和Thread Pooling。

對於java中的內存限制應用程序,軟件應該調用

System.gc() // garbage collector

在Intervals,無論其自動調用

您可以使用Runtime類為項目安排Load Balancers https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

public Process exec(String command)


                throws IOException
//--------------------------------------------------------
    Executes the specified string command in a separate process.

public void gc()

//----------------------------------------------------------
Runs the garbage collector. Calling this method suggests that the Java virtual machine expends effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects
This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null). 

對於內存密集型應用程序,線程一直是個問題,而在JavaFX中,場景的每個組件都是緊密綁定到場景的線程,但是在實現時,它似乎是松散綁定的。

如果需要長時間運行,則可以更好地處理Native端(JNI)中的一些處理器密集型任務。 此外,CLEAN架構將受益

https://www.google.co.in/webhp?sourceid=chrome-instant&rlz=1C1CHBF_enIN736IN736&ion=1&espv=2&ie=UTF-8#q=clean+code& *

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM