Removing overlays containing event listeners, memory leak or no?

835 views
Skip to first unread message

Leonid Rozenblyum

unread,
Jun 16, 2010, 2:46:08 AM6/16/10
to Google Maps JavaScript API v3
Google Map API doesn't clearly states what will be if:
a) create marker for the map;
b) attach event listener to the marker
c) setMap(null) - so delete marker from map;
d) remove reference to the marker from internal array.

Will it be memory leak here because event listener was connected to
the marker?
Should we preserve listener during add, and remove it as well to
ensure memory is really cleared or setMap(null) does this as well?

Ben Appleton

unread,
Jun 16, 2010, 3:00:56 AM6/16/10
to google-map...@googlegroups.com
On Wed, Jun 16, 2010 at 4:46 PM, Leonid Rozenblyum <lroze...@gmail.com> wrote:
Google Map API doesn't clearly states what will be if:
a) create marker for the map;
b) attach event listener to the marker
c) setMap(null) - so delete marker from map;
d) remove reference to the marker from internal array.

Will it be memory leak here because event listener was connected to
the marker?

There will be no memory leak.  If your event listener is connected to a marker, but that marker is otherwise free to be garbage collected, then the event listener will not prevent your marker from being garbage collected.

Of course, you must take the usual care to ensure that your marker is free to be garbage-collected.  It cannot be referenced from any other JavaScript Object, Array, or Function (closure).

Should we preserve listener during add, and remove it as well to
ensure memory is really cleared or setMap(null) does this as well?

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


Leonid Rozenblyum

unread,
Jun 16, 2010, 3:13:34 AM6/16/10
to Google Maps JavaScript API v3
Thanks for the very clear answer.
So if the event listener contains link to the marker in the closure,
we should keep the listener, and then remove it when needed.

Ben Appleton

unread,
Jun 16, 2010, 3:46:17 AM6/16/10
to google-map...@googlegroups.com

That should not be necessary.  If you have set the map property to null, the marker dom nodes will have been removed, so you are protected from causing leaks in Internet Explorer due to DOM-JS cycles.


--

You received this message because you are subscribed to the Google Groups "Google Maps JavaScript AP...

Nathan Raley

unread,
Jun 16, 2010, 9:44:06 AM6/16/10
to google-map...@googlegroups.com
You could always push your listeners into an array with some unique identifier that you can match to the marker you are removing and then remove the listener when you remove the marker.  I use this method just to ensure that the listeners get removed asap.

--
Reply all
Reply to author
Forward
0 new messages