Login  Register

Re: Out of memory (sooner or later...)

Posted by Wayne Rasband on Mar 21, 2007; 7:07pm
URL: http://imagej.273.s1.nabble.com/Out-of-memory-sooner-or-later-tp3699999p3700000.html

I ran this macro with

     print(i, nImages);

at the end of the for loop and got

    0 1
    1 2
    2 3
    3 4

in the Log window so it appears that one window was not getting closed.
Adding

     // close all image windows
     while (nImages>0) {
         selectImage(nImages);
         close();
     }

to the end of the loop seems to fix the problem.

-wayne

On Mar 21, 2007, at 9:36 AM, Marco Bravi wrote:

> Dear All,
>
> I am using ImageJ to monitor the hydrolysis of lignocellulosic
> biomass. As
> such, I take timed snapshots of the process, store it and
> subsequently analyze them. As the individual treatment takes time, I am
> preparing a macro (below) that recursively applies the same processing
> sequence
> to each image found inside one directory and saves the results of each
> analysis in one subdirectory.
>
> Unfortunately, sooner or later (depending on the amount of memory I
> dedicate to ImageJ upon startup) the heap memory is exhausted and the
> analysis process stops.
>
> I went over and over my (simple) code looking for missing "Close"
> commands that would progressively exhaust memory, but did not find any.
> However, this is my first experience as a macro coder, neither do I
> know
> much of ImageJ internals.
>
> I would appreciate any hint that could help me chase the bug away.
>
> Best regards to all those working at such a nice, open piece of
> software!
>
> Marco Bravi
>
> ==== macro ==============
> image_dir = getDirectory("Scegli la cartella di base:");
> result_dir = image_dir + "risultati/"
> start = getTime();
> setBatchMode(true); // runs up to 6 times faster
> list = getFileList(image_dir);
> for (i = 0; i < list.length; i++) {
>   if (endsWith(list[i], ".jpg")) {
>     open(image_dir + list[i]);
>     nome_immagine = getTitle();
>     fine_nome = lastIndexOf(nome_immagine, ".jpg");
>     nome_immagine_base = substring(nome_immagine, 0, fine_nome);
>     rename("mia_immagine");
>     run("8-bit");
>     run("Enhance Contrast", "saturated=0.5");
>     run("Pseudo Flat Field", "mean=150 keep 32-bit");
>     imageCalculator("Divide create 32-bit", "mia_immagine","Flat"); //
> Abbiamo ottenuto "Result of mia_immagine"
>     selectWindow("Flat");
>     //close();
>     run("Close");
>     //wait(2000);
>     selectWindow("mia_immagine");
>     close();
>     selectWindow("Result of mia_immagine");
>     run("Threshold");
>     run("Fill Holes");
>     // Analizziamo immagine binaria: "Result of mia_immagine"
>     run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00
> show=Nothing exclude exclude clear include summarize record");
>     // Salviamo il file dei risultati
>     nome_out = result_dir + nome_immagine_base + ".txt";
>     save_cmd = nome_out;
>     selectWindow("Summary of Result of mia_immagine");
>     saveAs("Text", save_cmd);
>     // Chiudiamo la finestra dei risultati
>     selectWindow("Summary of Result of mia_immagine");
>     run("Close");
>     // Salviamo l'immagine binaria "Result of mia_immagine"
>     // con il nome originario
>     selectWindow("Result of mia_immagine");
>     nome_out = result_dir + nome_immagine;
>     saveAs("Jpeg", nome_out);
>     wait(2000);
>     close();
>     wait(2000);
>   }
> }
> //print((getTime()-start)/1000);
> //setBatchMode(false);
>