Memory leak in SCIFIO when opening KLB stacks

scifio
imagej
klb

#1

When enabling SCIFIO to be able to open KLB stacks (from Keller’s Lab single plane illumination microscope [1]), turns out that opening a 40 MB stack results in a 400 MB memory leak that doesn’t go away.

The issue is observed from a script, in either jython or clojure, which opens KLB images with IJ.openImage and then carefully calls .flush() on the ImagePlus and doesn’t keep any handle onto the image data.

When opening a single KLB file form “File - Open” and then closing the window with the mouse, the leak doesn’t seem to be there.

To bypass this issue, here is one fix, in jython:

from org.janelia.simview.klb import KLB

klb = KLB.newInstance()

paths = ...
for path in paths:
  # Open as an ImgPlus, essentially an ImgLib2 Img<UnsignedShortType>
  imgplus = klb.readFull(path)
  # process the stack ...

In short, avoid using IJ.openImage, which uses the buggy SCIFIO framework when SCIFIO is enabled under “Edit - Options - ImageJ2”.

Hope this helps somebody.

[1] https://bitbucket.org/fernandoamat/keller-lab-block-filetype/src/805a09d6f84a8e0c55c3f68dbf8e4aae8522e975/javaWrapper/src/main/java/org/janelia/simview/klb/KLB.java?at=master&fileviewer=file-view-default