imagej / imagej2

Open scientific N-dimensional image processing :microscope: :sparkler:
https://imagej.net/
BSD 2-Clause "Simplified" License
1.19k stars 333 forks source link

Fixing "memory leaks" in ImageJ #168

Open stevenjwest opened 7 years ago

stevenjwest commented 7 years ago

Dear all,

This is more of a general comment/request than any specific issue. I have been running through some plugins I have made recently dealing with improper allocation of object references and ironing out "memory leaks" (I could probably be more careful during my coding!).

It occurred to me during this assessment that it would be great if there was the ability to run a method which would reset ImageJ back to its initial setup - i.e. to take all the GC Roots and all the initial references which are required when ImageJ starts, and discarding all the rest, to allow ImageJ to "reset" itself back to its initial state, and therefore remove any aberrant references via GC and allow ImageJ to remain running free from any memory leaks/get back all the available memory to the JVM.

Does anyone know if such a function already exists? Essentially it would amount to restarting ImageJ just without actually having to manually close and re-open. This would be convenient for developers to execute at the end of a plugin run to keep ImageJ free of memory leaks.... Assuming they want to refresh the system, and do not have any images or other important objects open.

Thanks,

Steve.

ctrueden commented 7 years ago

Quitting and restarting the program is of course the simplest, and currently available, way to reset ImageJ back to its initial state.

It might be possible to implement some technical solution that does not shut down the JVM, but what would be the purpose? I.e.: what is the undesirable side effect of actually restarting the software? Are you expecting e.g. images to remain open? If so, that is not really the "initial state" per se. So in that case then, how do you differentiate between objects which should remain allocated on the heap, and those which should be thrown away?

Do you know of any other Java-based projects which have a feature like this?

stevenjwest commented 7 years ago

Hi Curtis,

Thanks for your reply. Purpose would be to allow ImageJ to run for a long time, say on a server, without ever having an OOM.

Yes as say, ImageJ itself would have no way to know what objects should be retain, and I would expect any images or other objects not present in the initial state of ImageJ to be Garbage Collected. My plugins finish by saving information to disk and leaving no images open, so actually this would not be an issue for me.

I'm sorry I don't know of any other projects which implement this, but I think it would be a useful feature to allow ImageJ to stay running long term on a server for example. Perhaps its more important for people to ensure they minimise any memory leaks, but if you're using a third party plugin which has not cleaned up all object references this would be a problem for using ImageJ for any lengthy period of time.

Thanks,

Steve.

ctrueden commented 7 years ago

Thanks @stevenjwest for clarifying. In that case, I do not understand why you cannot simply shut down ImageJ and restart it automatically afterward, e.g. from a shell script which calls ImageJ in a loop? This will guaranteed flush all the memory and start from a clean slate for each iteration.