Out of Memory on simple macro

Hi there,
I am trying to apply a simple run(“RGB Color”) operation to all .tif files inside a chosen directory. The Macro runs and processes around 2000 images correctly, then quits with an “out of memory” error. The images themselves are only 2MB each in size, the machine has 16GB of Memory (running 64bit). I attached a screenshot showing the macro, memory use just before crash and the exception output. Please let me know what I am doing wrong.

If you turn off batchmode, are the windows closing? That’s the way to tell usually. This looks like it’s opening image after image after image and filling RAM.

Oh my. Yes, it is keeping all those 2k images open. I tell it to close() the active image, just like I do tell it so save (and saving is working fine). What’s going wrong?

You probably hit a race condition here: the image is still locked by the saving process when ImageJ tries to close() it. Adding something like wait(50); before the close(); might help.
Alternatively, you can:

  • close("*");
  • run("Close All");
  • use a full-fledged scripting language like Javascript.

Here’s a Javascript version of your macro:

// @File(label="Choose Source Directory", style="directory") dirIn
// @File(label="Choose Destination Directory", style="directory") dirOut

importClass(Packages.ij.IJ);
importClass(Packages.java.io.File);

file_list = dirIn.list();

for (i = 0; i < file_list.length; i++) {
    IJ.showProgress(i+1, file_list.length);
    if (file_list[i].endsWith("tif")) {
        imp = IJ.openImage(dirIn.getAbsolutePath() + File.separator + file_list[i]);
        IJ.run(imp, "RGB Color", "");
        IJ.saveAs(imp, "Tiff", dirOut.getAbsolutePath() + File.separator + file_list[i]);
    }
}
1 Like

It’s working now with close("*");

Thank you for the JavaScript code. The intended macro is a lot more extensive: From single image per channel (3-color channels, two of them empty, that is what the microscope gives me) of a z-stack on several x,y positions I want to combine channels, stack focus, merge colours then merge mosaic. Up to merged colours it is running now with only minor glitches.

Is there a sub-forum where I can get help to have this running as JavaScript? My last work in JavaScript was for Websites and 10 years ago :-/ Maybe I use KNIME to have it call the ImageJ Macros…

Hi @Theo

If you have any questions about KNIME Image Processing I am happy to help you!

If you could upload an example image and show me what you want to measure I can help you setting up a KNIME workflow.


This workflow merges multiple channels and multiple slices into one image stack. You can download this workflow here (26.4 KB).

1 Like

Thank you for the workflow. My trouble is getting the images into KNIME in the first place (see KNIME forum).