Search code examples
javamacrosimagej

ImageJ macro not release memory resources


I am using ImageJ v1.49e (that comes with it java 1.6.0_24 (64bit))

I wrote a macro that read images from an input directory, does some processing and depending on a condition moves the image to an output directory. Runs in batch mode because I expect to process like 100,000 images.

So I run the macro from ImageJ and I monitor the memory usage via JConsole and I just see the memory usage going up and up and never comes back down. ImageJ has 6GB allocated but soon enough it reaches that limit. Even if I invoke the GC from JConsole or from the macro it does nothing.

I check to make sure I am running in batch mode and closing any window I am opening, still no go. Can't find anything on the net either as to why this is happening.

Am I not releasing my resources correctly? Is there something I'm missing?

Below the is the Macro Code

inputDir = getDirectory("Choose the Input Directory");
outputDir = getDirectory("Choose the Output Directory");

inputDir = replace(inputDir,"\\\\", "\\\\\\\\");
outputDir = replace(outputDir,"\\\\", "\\\\\\\\");

if(inputDir != "" || outputDir != "") {
    setBatchMode(true);
    analyzeImagesBatch(inputDir, outputDir);
    exit("Done");
}
else {
    exit("Must select an input and output directory");
}

function analyzeImagesBatch(inputDir, outputDir) {
    inputList = getFileList(inputDir);

    for (i=0; i < inputList.length; i++) {
        showProgress(i+1, inputList.length);
        fileName = inputList[i];
        ok = imageAnalysis(inputDir, outputDir, fileName, 50, 30, 20);
        if(ok != 1) {
            imageAnalysis(inputDir, outputDir, fileName, 5, 10, 10);
        }
    }
}

function imageAnalysis(inputDir, outputDir, fileName, backgroundValue, size, countThresh) {
    ok = 0;
    open(inputDir+fileName);
    imageId = getImageID();
    run("8-bit");
    run("Subtract Background...", "rolling="+backgroundValue);  
    setAutoThreshold("Default");
    setOption("BlackBackground", false);
    run("Convert to Mask");
    run("Analyze Particles...", "size="+size+"-Infinity circularity=0.40-1.00 exclude clear");

    count = nResults();
    if(count >= countThresh) {
        ok = File.rename(inputDir+fileName, outputDir+fileName);
    }

    selectImage(imageId);
    close();
    return ok;
}

Solution

  • I re-wrote the macro as a Java Plugin and ran it on the same set of 100,000 images. Memory usage stayed below 70mb.

    Definately is a memory leak when using IJ macros.