Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX 8 Memory leak when Hiding Stage

I have a JavaFX application that minimizes to tray when the X button is pressed. I have been monitoring the application via VisualVM for memory trends.

The weird part is, when the application is open, or minimized to taskbar, the memory is always GCed back to initial memory used. However, when it is minimized to tray (stage.hide(), systemTray.show()), the memory gets GCed, but in an upward trend (leak).

In VisualVM, the Old Gen space keeps going up, and once it hits the max after some time, the application will be unresponsive, and CPU spikes to 80%.

I notice that if I stage.show() on the app by double clicking the tray icon etc, GC will clear everything back to normal. However, if left for prolonged periods, it will simply fail to GC the old gen.

A heap dump shows javafx.scene.Scene#7 and javafx.scene.Node[]#2 as having the most retained space. Both will not appear if the stage is not hidden. Under references, it shows 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 

What is causing this and how can I solve this?

like image 294
staticvoid Avatar asked Jun 24 '14 15:06

staticvoid


People also ask

What causes Java memory leaks?

In general, a Java memory leak happens when an application unintentionally (due to logical errors in code) holds on to object references that are no longer required. These unintentional object references prevent the built-in Java garbage collection mechanism from freeing up the memory consumed by these objects.

What is the main cause of memory leaks?

A memory leak starts when a program requests a chunk of memory from the operating system for itself and its data. As a program operates, it sometimes needs more memory and makes an additional request.


1 Answers

I never did find and answer to this. Instead, I would null the node on hide and restore it back on view. For intensive dynamic nodes/multiple nodes I created a hash map to store them in memory.

This has sort of become a habit for me in javafx8 to dispose all graphics and reassign on hide & view from hash map. The extra memory and cpu usage is negligible on modern desktops. Using this method, I have had 0 cpu usage apps and low memory apps (~100m) running when hidden on win8/10.

like image 137
staticvoid Avatar answered Oct 05 '22 23:10

staticvoid