Open nsilvestri opened 2 years ago
I may be confused but I believe your test is wrong.
You do remove incremental layers but in the end you still add to the final layer a polygon made out of 10 million polyline objects.
Just a simple JS object is about 250 bytes(https://stackoverflow.com/questions/58631581/javascript-compare-object-size-in-memory), that is about 2,4GB worth of memory for 10 mil objects. Perhaps a polyline is 5 to 10 times larger than a simple object plus the color property plus the final polygon and you get your 15GB!?
latlngs
and each coordinate pair are initialized in the loop, so their values should be reset with every cycle. The only layer in the group and on the map after the final iteration is a single triangle. You can verify this by reducing the number of iterations in the loop to a smaller amount: the final map will display only a single layer.
You are right, I got confused.
For me the ballooning-up happens on Firefox and on Chrome it doesn't:
While going throw the debugger I can see that the actual clearing happens:
I get the feeling that the issue is with Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1633626 https://bugzilla.mozilla.org/show_bug.cgi?id=1343234 https://bugzilla.mozilla.org/show_bug.cgi?id=1251596
I believe I am also having this issue, I have a piece of code that essentially does
layerGroup.addTo(map);
layerGroup.removeFrom(map);
however it appears that somehow the map is remembering the layerGroup, and as I add and remove various layerGroups the heap becomes quite large. I'm trying to find where the data is being kept using Chrome's memory profile tools, no luck. Has anyone discovered a workaround?
Checklist
Steps to reproduce
layerGroup.clearLayers()
Example code:
Expected behavior
After I
clearLayers()
, I am no longer holding any references to the layer, nor is it being displayed on the map. The garbage collector should clean up the reference and any Leaflet objects that were created from it.Current behavior
The memory consumption grows until the tab crashes, even though I'm only displaying one layer before clearing it. The provided example balloons to >15GB on my machine before crashing.
Minimal example reproducing the issue
https://plnkr.co/plunk/SSoPyUYk5kQjsFv3
Environment