I am using pyimagej to stitch 3x3 images via the Grid/Collection stitching plugin (and hopefully at some point via lower level APIs), see here for some of the details and the extract of the code that does work in pyimagej: Using imageJ functions like type conversion and setting pixel size via pyimagej
I need to run hundreds of those stitching jobs sequentially. I initialize like this at the moment:
ij = imagej.init('C:\\Program Files\\Fiji')
When I initialize at the beginning of the script and then run my jobs in a loop, I run into an OutOfMemoryError after 45 images have been processed (reproducibly at the same image), apparently during saving image 46:
It has nothing to do with the specific images in question. If I start at image 46 where it would normally run into the OutOfMemoryError, it passes it fine.
I have observed that the memory usage is continuously increasing. Therefore, I am using
ij.window().clear() after saving each image. If I understand it correctly, this closes ImageJ windows (as created by the Grid/Collection stitching plugin). But I also convert those to an ImageJ2 dataset
stitched_img_dataset = ij.py.to_dataset(stitched_img). Is there a way to close these as well? Even when closing ImageJ1 images, memory usage still goes from using 8 GB of RAM in the beginning to 32 GB of RAM, but then stays around 32 GB for a while (for 15 images being processed or so), until it fails when trying image #46 (without actually appearing to use more RAM in the task manager).
Some of that may be solved when using lower level APIs, but I think there are some things that just accumulate over each round of the run (not the actual full images, otherwise my memory usage would have exploded much faster, after 4-5 images or so). Therefore, I wondered if there is a way to “re-initialize” or to shut down the ImageJ gateway. All my sequential runs are completely independent, so restarting ImageJ each time wouldn’t matter from that perspective. But when I try to initialize every round by having
ij = imagej.init('C:\\Program Files\\Fiji') in the loop, I get the following error message:
Is there a supported way to close a session, something like
imagej.close()? Or to restart a session fresh? Or just clear all its memory?