Keon Wang
Keon Wang

Reputation: 365

javaFX Memory release , javaFX bug?

I found when switch pages frequently in the javaFX sample Ensemble.jar, memory will get higher an higher and can't release. This also happened in my project. Is that a bug of javaFX? Now our testers are always complaining about this problem.

Are there some good ways to solve this problem? What can we do in "memory release" in javaFX?

To solve this problem,what we've done:

  1. Set the global variables to NULL when we destroyed the javaFX pages.
  2. Decrease the use of "repeated big images" in .css file.
  3. Invoke GC in Platform.runLater(). (This seems a little silly)

But the effect is not so clear, Who can help us?

Upvotes: 8

Views: 1585

Answers (1)

Cyriled
Cyriled

Reputation: 31

This is not a bug in JavaFX.

I guess your memory leaks come from the use of listeners on Properties.

JavaFX uses Properties as an implementation of the Observer Pattern. When you add a ChangeListener to a property, you actually add a reference to your listener in the property object. If you don't call the RemoveListener method to remove this reference, your listener won't be garbage collected as long as the property object is not garbage collected itself.

I have no idea of what your code looks like but I can make some assumptions:

  • Each time you switch pages you instantiate a new controller
  • Each controller instantiate a listener object and add it to a property object.
  • When switching pages the previous controller is garbage collected while the property object is not. In the property object, there is a reference to the Listener object and thus the listener object remains in the memory. The more you switch pages, the more you instantiate listeners that won't be garbage collected, the bigger your memory leak is.

If you add Listeners to Properties, try to call the removeListener method and see if it solves the problem.

Regards,

Cyril

Upvotes: 3

Related Questions