3

I use Fiji/ImageJ on Windows and encountered the following problem: when I close a file, the memory allocated for that file is not released. ImageJ keeps that memory allocated, and reuses it when other files are opened.

So this is not strictly a memory leak, but still highly annoying, especially when working with large files. Is there a manual or (preferably) automatic way to trigger garbage collection after images are closed?

Edit:

Example: I use a large file and windows task manager to check memory allocation.

  1. clean start: 9278K of 12123MB in ImageJ, 3.56GB/16GB used in task manager.
  2. open large raw file: 5928MB of 12123MB, 9.41/16GB in task manager
  3. close large file: 9253K of 12123MB, 9.43/16GB GB in task manager. Fiji-win64.exe still uses 6419188K memory in task manager.
  4. Close Fiji: 3.28 GB / 16GB in task manager.

My problem is that I normally always keep Fiji open with a few files and windows. Then I open some large files and eventually close them, but ImageJ still keeps my memory reserved. Then I start executing a memory hungry job, and I quickly run out of memory and the whole system hungs up when windows starts swapping things.

hthms
  • 793
  • 8
  • 22

3 Answers3

5

Whether java returns unused memory to OS depends on the used garbage collector. Some implementations do return it and in some, the heap only keeps growing. Which garbage collector is used by default differs in various JRE versions and is also dependent on the system.

You can force what garbage collector is used by command line arguments when java is started. To change the used gc for ImageJ to one of gcs that return unused memory, edit ImageJ.cfg file in the ImageJ install directory and on the third line add -XX:+UseParNewGC.

For fiji, run the fiji launcher with -XX:+UseParNewGC added to command line arguments, i.e. ImageJ-win64.exe -XX:+UseParNewGC --

On my system with 64-bit java 8, this leads to memory being released back to OS. This is a graph of memory usage of ImageJ when importing a big stack, duplicating it a few times, closing it and running garbage collector a few times:

ImageJ memory usage

See this usefull article on how different gcs behave with respect to returning memory.

Josef Borkovec
  • 1,049
  • 8
  • 13
  • I've tried both the cfg and the command line argument solution, but it does not seem to change anything. I'll keep on experimenting. – hthms Apr 09 '14 at 06:40
  • The Fiji wiki (http://fiji.sc/Java_Options#Passing_Java_options_to_Fiji) mentions two parameters related to garbage collection: -XX:+UseParallelGC and -XX:+UseConcMarkSweepGC. Unfortunately, the former gives me a 'Could not create the Java virtual machine' error, while the latter does not seem to have any effect. – hthms Apr 09 '14 at 07:09
2

In many cases, Java never releases memory back to the system, so Windows Task Manager will always report an ever-growing amount of RAM used by the Java process, until the JVM shuts down. That said, some articles suggest that you can cause Java to give back free memory to the OS under certain conditions; see:

Community
  • 1
  • 1
ctrueden
  • 6,394
  • 3
  • 32
  • 64
  • 1
    Ok, so the solution is to always close ImageJ after working with large files? Wonderful. – hthms Apr 08 '14 at 06:56
  • To be clear, Java does reuse memory when you close and reopen images. The behavior described above is not a memory _leak_ per se. In my lab we leave ImageJ running for days or weeks at a time doing batch processing and it works just fine. – ctrueden Apr 19 '14 at 03:49
1

Within ImageJ/Fiji, you can change how much memory is allocated to Java via Edit > Options > Memory & Threads.... You can also manually trigger the garbage collector by clicking on the status bar.

The issue has also been discussed several times on the ImageJ mailing list, e.g. here.

Jan Eglinger
  • 3,876
  • 1
  • 22
  • 49
  • Clicking on the status bar or running Plugins/Utilities/Collect garbage does not work for me. For example, I open a 6GB large file with ImageJ, and then close it: memory allocation still remains 6GB, no matter how many times I click on the status bar. – hthms Apr 07 '14 at 13:58
  • How do you check the memory allocation? If the status bar keeps reporting a large number after clicking on it, there might be a memory leak in one of the plugins you are using, e.g. an `ImagePlus` opened but its `ImagePlus#flush()` method never being called. – Jan Eglinger Apr 07 '14 at 14:25
  • I use a large file + windows task manager. I'm gonna edit the original question and add an example. – hthms Apr 07 '14 at 14:35
  • Clicking the status bar does not help because the pre-allocated memory will not be released to OS but it just becomes available for ImageJ. So. allocating near minimum memory for the task I am doing from `Memory & Threads` dialog was a better solution for me. – otterb Jan 21 '16 at 17:03