openseadragon
openseadragon copied to clipboard
Resources not being deallocated causing renderer crash
Hello, I'm using the GeoTIFFTileSource plugin to display several .tiff images in an electron app, building the tiles using Sharp.js and passing them to the viewer:
Main Process:
const buildtiledTif = async (filePath) => {
try {
return await sharp(filePath, {limitInputPixels:false}).tiff({tile:true, pyramid:true}).toBuffer();
} catch (err) {
await database.log('ERROR', err.stack);
}
}
Renderer Process:
const updateScanImage = async (filePath) => {
/* close the viewer */
viewer.close();
/* Build tiled tiff */
const typedArray = await window.electron.sharp("build-tiled-tif", filePath);
const arrayBuffer = typedArray.buffer.slice(typedArray.byteOffset, typedArray.byteLength + typedArray.byteOffset);
/* generate tiffTileSource and open viewer */
const tiffTileSources = await OpenSeadragon.GeoTIFFTileSource.getAllTileSources(arrayBuffer);
viewer.open(tiffTileSources);
}
After some issues with loading the images from a local arrayBuffer, I managed to pass the tiled image to OSD and open it with decent performances (~2s to open a ~200MB file). The issue I'm now facing is that the resources (tiles) loaded are not deallocated even after closing/destroying the viewer and keeps on adding into memory at every pan/zoom/image replacement, eventually leading to renderer going out of memory:
clientWin.webContents.on("render-process-gone", (event, details) => {
console.error("gone", details) // This prints: { reason: 'oom', exitCode: -536870904 }
})
I originally opened the issue on the GeoTIFFTileSource repo (pearcetm/GeoTIFFTileSource#9), however I think it may not be strictly related to the library itself, so I'm opening one here as well.
I believe OSD is generally pretty good about releasing its resources, so my guess is this has something to do with it running in Electron, or the use of Sharp, or something. I think the next step is to try to narrow it down to isolate where the issue is coming from.
@Ottozz
Unless I'm totally missing something, I don't see anything in the OpenSeadragon code that touches tile sources when Viewer.close() is called. For what it's worth, tile sources aren't touched in Viewer.destroy() either.
Are the resources that aren't getting freed held in the tile sources?
If the resources that aren't getting freed are being held by the OpenSeadragon Viewer instance, then I would recommend trying viewer.destroy() instead of viewer.close() and creating a new OpenSeadragon.Viewer instead of calling viewer.open(). See if that changes anything? 🤷♂️
This looks like an issue within geotiff.js
which is part of the tile source. See https://github.com/pearcetm/GeoTIFFTileSource/issues/9#issuecomment-2104281193. It's been/being addressed by fixing the tile source library.