Leaflet / Leaflet.markercluster

Marker Clustering plugin for Leaflet
MIT License
3.98k stars 1k forks source link

DistanceGrid doesn't free up memory #977

Open MarkFalconbridge opened 4 years ago

MarkFalconbridge commented 4 years ago

How to reproduce

What behaviour I'm expecting and which behaviour I'm seeing

I'm not expecting a memory leak but I'm seeing one

When markers are added or removed the grid inside DistanceGrid is updated.

When objects are added to the grid, entries are created in those dictionaries and the object is inserted into the appropriate place in the final array. When objects are removed from the grid, the object is removed from the row, and the row is removed, but the top-level part of the grid isn't removed.

So if we start with an empty grid and add an object O at

{x: 1, y: 2} , the grid becomes grid: { '2': { '1': [O] } }

When that object is removed, the grid becomes: grid: { '2': {} }

It doesn't remove the row object when empty, however. It would be worth considering modifying the code to add:

if (Object.keys(row).length === 0) { delete grid[y]; }

just after the

delete row[x];

line.

If we insert objects at 1 million different y positions, that grid object will accumulate 1 million empty objects and never clean them up.

mikila85 commented 4 years ago

@MarkFalconbridge , did you find a solution for this? having the same problem and it seems no one answers here.

MarkFalconbridge commented 4 years ago

@mikila85 We ended up patching the code ourselves as described above.