Skip to content

addGeotiff(): free a raster when its layer is removed - #111

Open
eliotmcintire wants to merge 1 commit into
r-spatial:masterfrom
eliotmcintire:geotiff-handler-leak
Open

eliotmcintire wants to merge 1 commit into
r-spatial:masterfrom
eliotmcintire:geotiff-handler-leak

Conversation

@eliotmcintire

Copy link
Copy Markdown

Thanks for leafem; I am a heavy user of it, through the map viewer in our SpaDES.shiny package. I only work in R, so I used Claude (an AI assistant) to find the cause and write the fix. This is a person and an AI working on it together.

When a Shiny app replaces a raster drawn with addGeotiff(), the browser never frees the old one. An app that steps through maps with a time slider uses more memory at every step, and after a few hours the tab can no longer draw maps.

Affects: apps that replace or remove addGeotiff() layers now keep memory flat; clicking and hovering work as before.

Technical details

Each addGeotiff() call adds a click and a mousemove handler to the map (georaster-binding.js, end of addGeotiff). Each handler holds the whole georaster, and nothing removes them, so a raster stays in memory after its layer is gone. This keeps the two handlers and removes them with map.off() when the layer fires remove. Replacing a layer under the same layerId, removeImage() and clearImages() all remove the layer, so each now frees it.

Tested in headless Chromium with a minimal Shiny app that replaces a 1000×1000 layer through leafletProxy() every 2 s, forcing garbage collection before each reading. Before: heap 66 → 423 MB in 80 s, mousemove handlers 5 → 46. After: 40 MB throughout, 2 handlers. No errors were thrown in either run. The package has no JS tests, so none were added; I can share the test app.

addCOG() has the same pattern. I left it alone because #110 changes that function; I can apply the same fix there or in a follow-up, whichever you prefer.

Separately, and not fixed here: when layers are replaced faster than they draw (every 0.5 s with 1000×1000 rasters), georaster-layer-for-leaflet throws "Cannot read properties of null (reading 'reproj')". The browser console keeps each error, and each error keeps its layer, so memory grows that way too.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Lx3DXcxmq3mSxenWLiFbBD

Every addGeotiff() call added a click and a mousemove handler to the map,
each holding the whole georaster, and never removed them. A layer replaced
under the same layerId therefore stayed in memory, so an app stepping
through rasters ran out of memory after a few hours. The handlers are now
removed when their layer is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lx3DXcxmq3mSxenWLiFbBD

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant