Detecting memory leaks in a Scene
John Hendrikx
hjohn at xs4all.nl
Sun Mar 28 12:03:55 UTC 2021
I've created a bit of helper code I've been using to detect Nodes that
have been removed from a Scene that are not getting garbage collected.
It takes a Scene as input, and will then monitor all Nodes that are
added and removed to compile a list of Nodes that are no longer part of
the Scene but are still being referenced.
The code is here:
https://gist.github.com/hjohn/f7b2d5b6d56ba5e5bb6b6c5621799ca5
One major issue I've already discovered with this code (apart from my
own mistakes) is that Scene$MouseHandler is holding on to references to
Nodes that it thinks might be part of a future drag/drop action. It does
this in the pdrEventTargets fields. I haven't been able to reproduce
this yet in a small sample program, but in my larger program I
can reproduce it at any time:
1) Hover over a component
2) Trigger some action with the keyboard which replaces or removes
the hovered component
3) References to the replaced nodes still exist in pdrEventTargets
(seen with VisualVM)
The references disappear as soon as you press a mouse button (moving the
mouse around isn't sufficient). Note that I'm not doing any drag'n'drop
type actions nor do I use any drag'n'drop things in my program (it is
fully keyboard controlled).
There is code in Scene$MouseHandler to apparently deal with this case
where a node gets removed, but it doesn't seem to help in a more complex
UI. I'll attempt to debug this further, but would also appreciate any
help if someone knows that code.
The removal code:
/**
* Generates mouse exited event for a node which is going to be removed
* and its children, where appropriate.
* @param removing Node which is going to be removed
*/
void generateMouseExited(Node removing) {
mouseHandler.handleNodeRemoval(removing);
}
In the "handleNodeRemoval" a lot of things are happening, but a lot of
it is skipped when certain conditions aren't met (like "pdrInProgress").
As there won't be a DnD in progress I think the references are not being
removed from the arraylists the MouseHandler is maintaining.
Anyway, I hope the leak detecting code will be useful, I've been toying
around with something like this for quite a while, and now that I've got
something that is actually really useful for detecting leaks as soon as
they happen I thought it be good to share.
--John
More information about the openjfx-dev
mailing list