JavaFX 8隐藏阶段时内存泄漏

问题描述 投票:36回答:2

我有一个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

造成这种情况的原因是什么?如何解决这个问题?

java memory-leaks garbage-collection javafx javafx-8
2个回答
1
投票

我从来没有找到并回答这个问题。相反,我会在隐藏上隐藏节点并在视图上恢复它。对于密集的动态节点/多个节点,我创建了一个哈希映射来将它们存储在内存中。

这有点习惯于我在javafx8中处理所有图形并重新分配隐藏和查看哈希映射。在现代桌面上,额外的内存和CPU使用率可以忽略不计。使用这种方法,我在win8 / 10上隐藏了0个cpu使用应用程序和低内存应用程序(~100m)。


-1
投票

Java具有Like Weak Reference: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 - >>垃圾收集的内容。

另外,还有Concurrency 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类为项目https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html计划Load Balancers

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& *

© www.soinside.com 2019 - 2024. All rights reserved.