garbage collection

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

garbage collection

Michael Cammer-2
Dear all,

For years we have been having a problem with memory not being freed up
when images are closed.  Now we are running ImageJ 1.43n with Java
1.6.0_10 (32-bit) on a Windows Vista HP desktop.  (I also have the
problem on a WindowsXP professional Dell.)  Immediately after closing
an image, running the GarbageCollect_ routine a few times with pauses
between each run cleans out the memory.  Is there a way ImageJ could
do this automatically so that we don't have to do this manually every
time we close an image?

Thank you.

Sincerely,

Michael

--
Michael Cammer  *  http://coxcammer.com/
[hidden email]  or  [hidden email]
H 914-632-3044     Cell 914-309-3270
Reply | Threaded
Open this post in threaded view
|

Re: garbage collection

dscho
Hi,

On Fri, 22 Jan 2010, Michael Cammer wrote:

> For years we have been having a problem with memory not being freed up
> when images are closed.  Now we are running ImageJ 1.43n with Java
> 1.6.0_10 (32-bit) on a Windows Vista HP desktop.  (I also have the
> problem on a WindowsXP professional Dell.)  Immediately after closing an
> image, running the GarbageCollect_ routine a few times with pauses
> between each run cleans out the memory.  Is there a way ImageJ could do
> this automatically so that we don't have to do this manually every time
> we close an image?

It is in general a bad idea to run the garbage collector too often.  At
least on platforms where you do not need to, it puts a serious dent into
the performance.

But you can do something better: just copy the following text into a file
named "CollectGarbage_.java" in your plugins folder, use "Plugins>Compile
and Run..." to compile it (if you are using Fiji, this is not necessary,
it will turn up as "CollectGarbage" in your Plugins menu already).  Then
you can put a line 'call("CollectGarbage_.registerCloseListener");' into
your StartupMacros, and you are set.

-- snip CollectGarbage_.java --
import ij.ImageListener;
import ij.ImagePlus;

import ij.plugin.PlugIn;

public class CollectGarbage_ implements PlugIn {
        public void run(String arg) {
                if ("onclose".equals(arg))
                        registerCloseListener();
                else
                        run();
        }

        public static void registerCloseListener() {
                ImagePlus.addImageListener(new ImageListener() {
                        public void imageOpened(ImagePlus image) {}
                        public void imageUpdated(ImagePlus image) {}
                        public void imageClosed(ImagePlus image) {
                                run();
                        }
                });
        }

        public static void run() {
                System.gc();
                System.gc();
        }
}
-- snap --

Hth,
Dscho