How to free up memory when analyzing images (using ImageJ) within a loop
2 views (last 30 days)
Show older comments
I use Matlab for Image analysis. I dont know a lot about programing.
I have a working code that imports fiji (ImageJ) java class files and a specific plugin for time-lapse movie analysis. Part of it as follows -
clear;
javaaddpath ('.../Matlab/mij.jar');
addpath(genpath('.../Fiji.app/scripts'));
addpath ('.../Matlab Scripts/bfmatlab');
addpath ('.../Matlab Scripts/jheapcl/jheapcl');
Miji(false);
%----------------------------------
% Import Fiji and TrackMate classes
%----------------------------------
import ij.*
import loci.plugins.BF.*
import java.util.HashMap
import fiji.plugin.trackmate.*
import fiji.plugin.trackmate.detection.*
import fiji.plugin.trackmate.features.*
import fiji.plugin.trackmate.features.track.*
import fiji.plugin.trackmate.features.track.edges.*
import fiji.plugin.trackmate.features.spot.*
import fiji.plugin.trackmate.tracking.*
import fiji.plugin.trackmate.tracking.sparselap.*
import fiji.plugin.trackmate.util.TMUtils.*
import fiji.plugin.trackmate.SelectionModel.*
import fiji.plugin.trackmate.visualization.hyperstack.*
import fiji.plugin.trackmate.visualization.*
...
...
for j=1:num_file
...
reader=bfGetReader(filelist{j});
...
%----------------------------------
% Read and display image file
%----------------------------------
imps = openImagePlus(filelist{j});
imp = imps(1);
imp.show();
...
...
reader.close();
MIJ.closeAllWindows();
jheapcl %java heap space memory release
clearvars -except filelist num_file ;
end
MIJ.exit();
Before running the script I estimate the free memory
>> ij.IJ.freeMemory()
ans =
149MB of 9189MB (1%)
After running 10 movie files (about 500mb each) I estimate the free memory again
>> ij.IJ.freeMemory()
ans =
6063MB of 9189MB (65%)
clearly, in my script, memory is getting incrementally occupied after each movie is analyzed. If I queue up too many movie files to analyze in loops, eventually, the system is ‘out of memory’ and hang. I have tried running the garbage collector at the command prompt “java.lang.Runtime.getRuntime.gc;”, without much of a change.
I always have to restart Matlab to analyze next set of 10 movie files.
Any help on this is much appreciated.
Thanks,
Dabanjan
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!