memory leak in medianfiltermex
    2 views (last 30 days)
  
       Show older comments
    
For an image reconstruction project, I have to run medfilt2 a large number of times, let's assume millions of times. Internally medfilt2 uses either medianfiltermex (for single matrices) or ordfilt2 (for double matrices). Since medianfiltermex is significantly faster (see below example, 27s vs 159s), it carries my preference. However, medianfiltermex seems to have a memory leak, hogging more and more memory until Matlab crashes before finalizing the whole process. How can I solve this memory leak?
Some details: I work on R2017a on Linux Mint 18.2 64-bit with kernel 4.8.0-53-generic. This is a minimal example of the problem: the used memory increases as the number of runs increases when using medianfiltermex, but not when using ordfilt2.
img = rand(150,150);
display('Memory usage in MB: ');
display(' using medianfiltermex: ');
tic;
system('free -tm');
for i = 1:50000
    im = medfilt2(single(img),[3,3]);
    if (rem(i,10000)==0)
        display([' ' num2str(i) ' runs ']);
        system(['free -tm | grep ''Total:''']);
    end;
end;
toc;
display(' using ordfilt2: ');
tic;
system('free -tm');
for i = 1:50000
    im = medfilt2((img),[3,3]);
    if (rem(i,10000)==0)
        display([' ' num2str(i) ' runs ']);
        system(['free -tm | grep ''Total:''']);
    end;
end;
toc;
Output:
Memory usage in MB: 
 using medianfiltermex: 
              total        used        free      shared  buff/cache   available
Mem:          31985        3786       25611         159        2587       27455
Swap:         30517           0       30517
Total:        62503        3786       56129
 10000 runs 
Total:        62503        3870       56045
 20000 runs 
Total:        62503        3952       55962
 30000 runs 
Total:        62503        4034       55881
 40000 runs 
Total:        62503        4119       55795
 50000 runs 
Total:        62503        4203       55712
Elapsed time is 27.090739 seconds.
 using ordfilt2: 
              total        used        free      shared  buff/cache   available
Mem:          31985        4203       25194         159        2587       27038
Swap:         30517           0       30517
Total:        62503        4203       55712
 10000 runs 
Total:        62503        4201       55714
 20000 runs 
Total:        62503        4204       55710
 30000 runs 
Total:        62503        4202       55727
 40000 runs 
Total:        62503        4207       55721
 50000 runs 
Total:        62503        4206       55722
Elapsed time is 159.201341 seconds.
0 Comments
Answers (0)
See Also
Categories
				Find more on Image Processing Toolbox in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!