For Programmers: Free Programming Magazines  


Home > Archive > Matlab > August 2007 > GUI memory leak









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author GUI memory leak
ben

2007-08-30, 8:59 pm

Dear group,

I have written a small GUI that shows a movie frame-by-frame.
Apparently, there is a memory leak:

Starting Matlab: 300 mb of memory used
Starting my GUI and loading a 200 mb movie: 600 mb of memory used.
Everything ok till now, but then:
When I view one frame after the other, more and more memory is eaten
up, after 400 frames, 1500 mb are used (and my computer is getting
slow).
After closing the GUI, memory is back to normal.

I load the movie in OpeningFcn and make it global;
all callback functions access this global variable.
(Earlier, I saved movie in guidata, same problem.)

What is wrong here?
Any help appreciated.

I do not include the movie ;-),
but the m-file

Bye
Ben

function varargout = MovieUVLabelEditor_v1_1(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MovieUVLabelEditor_v1_1_OpeningFcn, ...
'gui_OutputFcn', @MovieUVLabelEditor_v1_1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

end % MovieUVLabelEditor_v1_1


% --- Executes just before MovieUVLabelEditor_v1_1 is made visible.
function MovieUVLabelEditor_v1_1_OpeningFcn(hObje
ct, eventdata,
handles, varargin)
% Choose default command line output for MovieUVLabelEditor_v1_1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

DataGUI = guidata(hObject);

% read in arguments
% FileName=varargin{1};
% Para = varargin{2};
% iSperm = varargin{3};
% TEST
Para='mo.mat';
FileName= '20070824Expnt01Movie';
iSperm=1;

% data directory
DataDir=['/home/luru/Work/SeaUrchinSperm/2007/',FileName(1:end-5)];

cd(DataDir)


% save them
DataGUI.FileName = FileName;
DataGUI.iSperm = iSperm;
% load parameter file
load(Para);
% load enhanced movie
FileNameEnhanced = [FileName '_Enhanced.mat'];
disp('load enhanced movie');
global Movie;
load(FileNameEnhanced);
DataGUI.ParameterSet=ParameterSet;
DataGUI.nFrame = Movie(end).FrameNum;

% compute background intensity
DataGUI.BackgroundIntensity=zeros(DataGUI.nFrame,1);
for iFrame=1:DataGUI.nFrame
DataGUI. BackgroundIntensity(iFrame)=sum2(Movie(i
Frame).Frame);
end;
MaxBackgroundIntensity=max(DataGUI.BackgroundIntensity);
% guess first frame
ind=find(DataGUI.BackgroundIntensity>MaxBackgroundIntensity/2);
DataGUI.iFrame = max(1,ind(1)-2);

guidata(hObject, DataGUI);

Display(hObject);

% install keyboard short cuts
set(DataGUI.figure1, 'KeyPressFcn', {@KeyShortCut,hObject});

end % MovieUVLabelEditor_v1_1_OpeningFcn

% --- Outputs from this function are returned to the command line.
function varargout = MovieUVLabelEditor_v1_1_OutputFcn(hObjec
t,
eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
end

% --- Executes on button press in prev=previous
function prev_Callback(hObject, eventdata, handles)
% go to previous mask
DataGUI = guidata(hObject);
if DataGUI.iFrame > 1
DataGUI.iFrame = DataGUI.iFrame - 1;
guidata(hObject, DataGUI);
Display(hObject);
end
end

% --- Executes on button press in next = next
function next_Callback(hObject, eventdata, handles)
% go to next frame
DataGUI = guidata(hObject);
if DataGUI.iFrame < DataGUI.nFrame;
DataGUI.iFrame = DataGUI.iFrame + 1;
guidata(hObject, DataGUI);
Display(hObject);
end
end

% --- Executes on button press in UV.
function UV_Callback(hObject, eventdata, handles)

DataGUI = guidata(hObject);
global Movie;
Movie(DataGUI.iFrame).Label='UV';

% go to next frame
next_Callback(hObject);
end


% --- Executes on button press in semiUV.
function semiUV_Callback(hObject, eventdata, handles)

DataGUI = guidata(hObject);
global Movie;
Movie(DataGUI.iFrame).Label='semiUV';

% go to next frame
next_Callback(hObject);
end

% --- Executes on button press in save.
function save_Callback(hObject, eventdata, handles)

% save the movie
DataGUI = guidata(hObject);
global Movie;
Movie(1).Comment=get(DataGUI.edit1,'String');
FileNameEnhanced = [DataGUI.FileName '_Enhanced.mat'];
save(FileNameEnhanced,Movie);

end

function Display(hObject)

DataGUI = guidata(hObject);
global Movie;

% frame
h1=DataGUI.axes1;
axis(h1);
set(gcf,'CurrentAxes',h1);
hold(h1,'on');
% ImageShow(Movie(DataGUI.iFrame).Frame,DataGUI.ParameterSet);
imshow(double(Movie(DataGUI.iFrame).Frame)/500);
title(sprintf('%d /
%d',DataGUI.iFrame,DataGUI.nFrame),'Interpreter','none');
hold(h1,'off');

% intensity profile
h2=DataGUI.axes2;
cla(h2);
axis(h2);
set(gcf,'CurrentAxes',h2);
hold(h2,'on');
plot(DataGUI.BackgroundIntensity,'b');
xlim([0 DataGUI.nFrame]);
h2YLim=get(h2,'ylim');
plot([DataGUI.iFrame DataGUI.iFrame],h2YLim,'g');
plot(DataGUI.iFrame,DataGUI.BackgroundIntensity(DataGUI.iFrame),'r.');
title(h2,'frame intensity');
hold(h2,'off');

end % Display

function KeyShortCut(scr,evnt,hObject)

if strcmp(evnt.Key,'rightarrow') == true
next_Callback(hObject);
end

if strcmp(evnt.Key,'leftarrow') == true
prev_Callback(hObject);
end

if strcmp(evnt.Key,'s') == true
save_Callback(hObject);
end

if strcmp(evnt.Key,'u') == true
UV_Callback(hObject);
end

if strcmp(evnt.Key,'m') == true
semiUV_Callback(hObject);
end

end

function edit1_Callback(hObject, eventdata, handles)
end

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor')
)
set(hObject,'BackgroundColor','white');
end;
end

ben

2007-08-30, 8:59 pm

I think I found the solution myself:

I must cla the old plot, before I start a new one.

On Aug 30, 7:02 pm, ben <benjamin.friedr...@gmail.com> wrote:
> Dear group,
>
> I have written a small GUI that shows a movie frame-by-frame.
> Apparently, there is a memory leak:
>
> Starting Matlab: 300 mb of memory used
> Starting my GUI and loading a 200 mb movie: 600 mb of memory used.
> Everything ok till now, but then:
> When I view one frame after the other, more and more memory is eaten
> up, after 400 frames, 1500 mb are used (and my computer is getting
> slow).
> After closing the GUI, memory is back to normal.
>
> I load the movie in OpeningFcn and make it global;
> all callback functions access this global variable.
> (Earlier, I saved movie in guidata, same problem.)
>
> What is wrong here?
> Any help appreciated.
>
> I do not include the movie ;-),
> but the m-file
>
> Bye
> Ben
>
> function varargout = MovieUVLabelEditor_v1_1(varargin)
>
> % Begin initialization code - DO NOT EDIT
> gui_Singleton = 1;
> gui_State = struct('gui_Name', mfilename, ...
> 'gui_Singleton', gui_Singleton, ...
> 'gui_OpeningFcn', @MovieUVLabelEditor_v1_1_OpeningFcn, ...
> 'gui_OutputFcn', @MovieUVLabelEditor_v1_1_OutputFcn, ...
> 'gui_LayoutFcn', [] , ...
> 'gui_Callback', []);
> if nargin && ischar(varargin{1})
> gui_State.gui_Callback = str2func(varargin{1});
> end
>
> if nargout
> [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
> else
> gui_mainfcn(gui_State, varargin{:});
> end
> % End initialization code - DO NOT EDIT
>
> end % MovieUVLabelEditor_v1_1
>
> % --- Executes just before MovieUVLabelEditor_v1_1 is made visible.
> function MovieUVLabelEditor_v1_1_OpeningFcn(hObje
ct, eventdata,
> handles, varargin)
> % Choose default command line output for MovieUVLabelEditor_v1_1
> handles.output = hObject;
> % Update handles structure
> guidata(hObject, handles);
>
> DataGUI = guidata(hObject);
>
> % read in arguments
> % FileName=varargin{1};
> % Para = varargin{2};
> % iSperm = varargin{3};
> % TEST
> Para='mo.mat';
> FileName= '20070824Expnt01Movie';
> iSperm=1;
>
> % data directory
> DataDir=['/home/luru/Work/SeaUrchinSperm/2007/',FileName(1:end-5)];
>
> cd(DataDir)
>
> % save them
> DataGUI.FileName = FileName;
> DataGUI.iSperm = iSperm;
> % load parameter file
> load(Para);
> % load enhanced movie
> FileNameEnhanced = [FileName '_Enhanced.mat'];
> disp('load enhanced movie');
> global Movie;
> load(FileNameEnhanced);
> DataGUI.ParameterSet=ParameterSet;
> DataGUI.nFrame = Movie(end).FrameNum;
>
> % compute background intensity
> DataGUI.BackgroundIntensity=zeros(DataGUI.nFrame,1);
> for iFrame=1:DataGUI.nFrame
> DataGUI. BackgroundIntensity(iFrame)=sum2(Movie(i
Frame).Frame);
> end;
> MaxBackgroundIntensity=max(DataGUI.BackgroundIntensity);
> % guess first frame
> ind=find(DataGUI.BackgroundIntensity>MaxBackgroundIntensity/2);
> DataGUI.iFrame = max(1,ind(1)-2);
>
> guidata(hObject, DataGUI);
>
> Display(hObject);
>
> % install keyboard short cuts
> set(DataGUI.figure1, 'KeyPressFcn', {@KeyShortCut,hObject});
>
> end % MovieUVLabelEditor_v1_1_OpeningFcn
>
> % --- Outputs from this function are returned to the command line.
> function varargout = MovieUVLabelEditor_v1_1_OutputFcn(hObjec
t,
> eventdata, handles)
> % Get default command line output from handles structure
> varargout{1} = handles.output;
> end
>
> % --- Executes on button press in prev=previous
> function prev_Callback(hObject, eventdata, handles)
> % go to previous mask
> DataGUI = guidata(hObject);
> if DataGUI.iFrame > 1
> DataGUI.iFrame = DataGUI.iFrame - 1;
> guidata(hObject, DataGUI);
> Display(hObject);
> end
> end
>
> % --- Executes on button press in next = next
> function next_Callback(hObject, eventdata, handles)
> % go to next frame
> DataGUI = guidata(hObject);
> if DataGUI.iFrame < DataGUI.nFrame;
> DataGUI.iFrame = DataGUI.iFrame + 1;
> guidata(hObject, DataGUI);
> Display(hObject);
> end
> end
>
> % --- Executes on button press in UV.
> function UV_Callback(hObject, eventdata, handles)
>
> DataGUI = guidata(hObject);
> global Movie;
> Movie(DataGUI.iFrame).Label='UV';
>
> % go to next frame
> next_Callback(hObject);
> end
>
> % --- Executes on button press in semiUV.
> function semiUV_Callback(hObject, eventdata, handles)
>
> DataGUI = guidata(hObject);
> global Movie;
> Movie(DataGUI.iFrame).Label='semiUV';
>
> % go to next frame
> next_Callback(hObject);
> end
>
> % --- Executes on button press in save.
> function save_Callback(hObject, eventdata, handles)
>
> % save the movie
> DataGUI = guidata(hObject);
> global Movie;
> Movie(1).Comment=get(DataGUI.edit1,'String');
> FileNameEnhanced = [DataGUI.FileName '_Enhanced.mat'];
> save(FileNameEnhanced,Movie);
>
> end
>
> function Display(hObject)
>
> DataGUI = guidata(hObject);
> global Movie;
>
> % frame
> h1=DataGUI.axes1;
> axis(h1);
> set(gcf,'CurrentAxes',h1);
> hold(h1,'on');
> % ImageShow(Movie(DataGUI.iFrame).Frame,DataGUI.ParameterSet);
> imshow(double(Movie(DataGUI.iFrame).Frame)/500);
> title(sprintf('%d /
> %d',DataGUI.iFrame,DataGUI.nFrame),'Interpreter','none');
> hold(h1,'off');
>
> % intensity profile
> h2=DataGUI.axes2;
> cla(h2);
> axis(h2);
> set(gcf,'CurrentAxes',h2);
> hold(h2,'on');
> plot(DataGUI.BackgroundIntensity,'b');
> xlim([0 DataGUI.nFrame]);
> h2YLim=get(h2,'ylim');
> plot([DataGUI.iFrame DataGUI.iFrame],h2YLim,'g');
> plot(DataGUI.iFrame,DataGUI.BackgroundIntensity(DataGUI.iFrame),'r.');
> title(h2,'frame intensity');
> hold(h2,'off');
>
> end % Display
>
> function KeyShortCut(scr,evnt,hObject)
>
> if strcmp(evnt.Key,'rightarrow') == true
> next_Callback(hObject);
> end
>
> if strcmp(evnt.Key,'leftarrow') == true
> prev_Callback(hObject);
> end
>
> if strcmp(evnt.Key,'s') == true
> save_Callback(hObject);
> end
>
> if strcmp(evnt.Key,'u') == true
> UV_Callback(hObject);
> end
>
> if strcmp(evnt.Key,'m') == true
> semiUV_Callback(hObject);
> end
>
> end
>
> function edit1_Callback(hObject, eventdata, handles)
> end
>
> % --- Executes during object creation, after setting all properties.
> function edit1_CreateFcn(hObject, eventdata, handles)
>
> if ispc && isequal(get(hObject,'BackgroundColor'),
> get(0,'defaultUicontrolBackgroundColor')
)
> set(hObject,'BackgroundColor','white');
> end;
> end



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com