You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've experienced a couple of memory issues when using GMapsFX on an application that adds and removes markers and polygons to a map periodically. The number of instances of any class contained in this library keeps increasing (even though my application doesn't hold any references to them anymore) until there's no memory left. After checking with jvisualvm what the problem was, I found out that no instances of any class that extends from JavascriptObject (e.g., Marker, LatLong, etc) are getting garbage collected. That is because the WeakHashMappeerRegistry in JavascriptObject keeps a reference to each JavascriptObject forever. WeakHashMap will allow an entry to be garbage collected if there isn't a strong reference to its key. However, in this case, for each entry in the WeakHashMap, the value is containing a reference to the key (see the JavascriptObject constructors):
As per this question in StackOverflow, the entry won't get garbage collected if the value holds a reference to the key. This means that, even though my application doesn't hold a reference to the JavascriptObject anymore, this WeakHashMap does, and will always keep it.
One of the solutions is mentioned in that StackOverflow question, which would be having JavascriptObject hold a weak reference to jsObject:
I've experienced a couple of memory issues when using GMapsFX on an application that adds and removes markers and polygons to a map periodically. The number of instances of any class contained in this library keeps increasing (even though my application doesn't hold any references to them anymore) until there's no memory left. After checking with jvisualvm what the problem was, I found out that no instances of any class that extends from
JavascriptObject
(e.g.,Marker
,LatLong
, etc) are getting garbage collected. That is because theWeakHashMap
peerRegistry
inJavascriptObject
keeps a reference to eachJavascriptObject
forever.WeakHashMap
will allow an entry to be garbage collected if there isn't a strong reference to its key. However, in this case, for each entry in theWeakHashMap
, the value is containing a reference to the key (see the JavascriptObject constructors):As per this question in StackOverflow, the entry won't get garbage collected if the value holds a reference to the key. This means that, even though my application doesn't hold a reference to the JavascriptObject anymore, this WeakHashMap does, and will always keep it.
One of the solutions is mentioned in that StackOverflow question, which would be having
JavascriptObject
hold a weak reference tojsObject
:Another solution would be to switch the key and the value of the
WeakHashMap
:I do not know the reasons why that WeakHashMap is needed though, so I'm not sure if the latter solution would be feasible.
If anyone could confirm that this is correct that would be great. Thank you very much.
The text was updated successfully, but these errors were encountered: