Issue with image accumulation in memory though successive images are loaded in the same variable ( already alocated memory ?)

1 visualización (últimos 30 días)
Hi everyone !
I have binary files of staked images. One image is ( 2048, 2048, 'uint16'), so that the full file is ( 2048, 2048, imageNumber, 'uint16').
I made a GUI to naviguate one file wihtout opening it full, but one by one image using fseek and fread, and then select one of this image.
However, each time a new image is loaded the used memory increases, although the image is loaded to the same variable and so I thought to the same allocated memory.
This memory is not released when the GUI is closed, or even the workspace cleared. It is only released when matlab is terminated.
Here is my source code:
function selectedReference = PEEMstartOnTheFlyAnalysis_test(fileBaseName)
% Open data file.
IDfile = fopen(fileBaseName, 'r');
% Open meta file to get the total number of layer.
IDfile_meta = fopen(fileBaseName+'_meta', 'r');
layers = fread(IDfile_meta, Inf, 'uint64');
layerNumber = size(layers, 1);
fprintf("%u layers in file.\n", layerNumber);
% Write header for log file.
system( 'top -n 1 | awk ''/RES/ {print( $7 "\t" $11 "\t" $12 "\t" $13)}'' > mem.log' );
writeMemLogEntry( 'Before opening first image.' );
% Create figure and display first figure for reference selection.
refID = 1;
figShownIm = figure;
shownIm = imagesc( loadOneBinPEEMimgAt( IDfile, refID) );
colormap gray; axis image; axis off;
writeMemLogEntry( 'First image loaded.' );
% Handles for GUI checkout function.
setappdata(figShownIm,'layerNumber',layerNumber);
setappdata(figShownIm,'imID',refID);
setappdata(figShownIm,'IDfile',IDfile);
setappdata(figShownIm,'shownIm',shownIm);
% GUI
shownImID = uicontrol('Style', 'text',...
'String', sprintf('Current image: %u / %u', refID, layerNumber),...
'Units', 'normalized',...
'Position', [0.33 0.05 .15 .03]);
setappdata(figShownIm,'shownImID',shownImID);
prevIm = uicontrol('String', '<-',...
'Callback', @loadNewImg,...
'Units','normalized',...
'Position',[0.17 0 .15 .04]);
nextIm = uicontrol('String', '->',...
'Callback', @loadNewImg,...
'Units','normalized',...
'Position',[0.34 0 .15 .04]);
selectRef = uicontrol('String', 'Select as reference',...
'Callback', 'uiresume()',...
'Units','normalized',...
'Position',[0.51 0 .15 .04]);
% Wait until the user select an image as reference.
uiwait();
fclose(IDfile);
fclose(IDfile_meta);
% Return selected reference image.
selectedReference = shownIm.CData;
close(figShownIm);
writeMemLogEntry( 'Image figure closed.' );
end
function loadNewImg(hObject, eventData, handles)
layerNumber = getappdata(hObject.Parent,'layerNumber');
imID = getappdata(hObject.Parent,'imID');
IDfile = getappdata(hObject.Parent,'IDfile');
shownIm = getappdata(hObject.Parent,'shownIm');
shownImID = getappdata(hObject.Parent,'shownImID');
switch(hObject.String)
case '<-'
diffID = -1;
case '->'
diffID = 1;
end
% Keep imID inside the existing layers.
if( (imID+diffID)<=0 || (imID+diffID)>layerNumber )
diffID=0;
end
% Update display
shownImID.String = sprintf('Current image: %u / %u', imID+diffID, layerNumber);
setappdata(hObject.Parent,'imID',imID+diffID);
% Load new image
shownIm.CData = loadOneBinPEEMimgAt( IDfile, imID+diffID);
% Write use of memory in log file.
writeMemLogEntry( shownImID.String );
end
function writeMemLogEntry( comment )
system( [ 'top -o RES -n 1 | awk ''/MATLAB/ {print( $7 "\t" $11 "\t" $12 "\t" $13 "\t' comment '")}'' >> mem.log' ] );
end
The source of the loading function:
function img = loadOneBinPEEMimgAt( IDfile, refID)
% loadOneBinPEEMimgAt Load one PEEM image from a binary file at a specific position.
% Load the 'refID'th image in a file previously opened with fopen at 'IDfile'.
% Inputs:
% IDfile : ID of the source file provided by fopen.
% refID : position of the image to be loaded within the file.
%
% Outputs:
% img : Loaded image.
% Here we assume image to be 2048*2048 uint16.
% fseek function places pointer on a (u)int8 basing,
% so one image is a 2048*2048*2 block.
fseek( IDfile, 2048*2048*2*(refID-1), 'bof'); % 'bof' stands for begining of file.
img = fread( IDfile, [2048 2048], 'uint16');
end
The code output the memory usage in log file (as it can be seen in the code), here is this log file:
RES %MEM TIME+ COMMAND
779628 2.4 0:11.32 MATLAB Before opening first image.
949800 2.9 0:15.31 MATLAB First image loaded.
1.0g 3.3 0:17.95 MATLAB Current image: 2 / 500
1.1g 3.5 0:18.67 MATLAB Current image: 3 / 500
1.1g 3.6 0:18.85 MATLAB Current image: 4 / 500
1.2g 3.8 0:20.79 MATLAB Current image: 5 / 500
1.2g 3.9 0:21.16 MATLAB Current image: 4 / 500
1.2g 3.9 0:21.39 MATLAB Current image: 3 / 500
1.3g 4.2 0:21.84 MATLAB Current image: 2 / 500
1.3g 4.2 0:22.18 MATLAB Image figure closed.
Do you have any suggestions to avoid this data accumulation or to release the memory?
Thanks a lot for your help!

Respuesta aceptada

Cedric Bareille
Cedric Bareille el 19 de Mzo. de 2021
This was an opengl rendering issue. Using software rendering allows to avoid accumulating data in the memory.
opengl('save','software')

Más respuestas (1)

BC
BC el 19 de Mzo. de 2021
Does resetting the axes after a figure is displayed help? I had this issue with imshow(), at least, after each figure was displayed the memory usage increased every time.
cla reset
  3 comentarios
BC
BC el 20 de Mzo. de 2021
Glad you got to the bottom of it, thanks for sharing your solution!

Iniciar sesión para comentar.

Categorías

Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by