seikichi/tiff.js

Memory leak

Kroppeb opened this issue · 2 comments

I have the following code:

// Create a new instance of the FileReader
const reader = new FileReader();

// Use the FileReader to read the blob as an ArrayBuffer
reader.readAsArrayBuffer(file);

const arrayBuffer: ArrayBuffer = await new Promise(resolve => {
    reader.onload = () => resolve(reader.result as ArrayBuffer)
})

const tiff = new Tiff({buffer: arrayBuffer});
const canvas: HTMLCanvasElement = tiff.toCanvas()

const convertedBlob: Blob = await new Promise(resolve => canvas.toBlob(result => resolve(result!), 'image/png'));

return URL.createObjectURL(convertedBlob);

The created URL's get revoked afterwards, yet there still seems to be a memory leak of some sorts. It also seem that once the memory leak hits a critical point, the library fails to read the data properly, and each subsequent image passed through causes an error. Yet those still keep increasing the memory footprint of the application.

I'm running this in FireFox btw, idk if that matters

It seems I should call tiff.close(), that seems to reduce the memory usage quite a bit. It seems there are still buffers being retained with the following gc path. Not quite sure yet where these are coming from.

  • Tiff -> Function
  • Module -> Object
  • FS -> Object
  • nameTable -> Array
  • -> Array
  • contents -> Uint8Array
  • ArrayBufferViewObject.buffer -> ArrayBuffer

has same issue