rmfield does not release memory

3 views (last 30 days)
Osamu
Osamu on 10 Mar 2011
Dear all,
I have a big field in the handles structure in my GUI program, and would like to replace it with a data in a .mat file.
First, I tried the following code:
1: function pushbutton1_Callback(hObject, eventdata, handles)
2: handles = rmfield(handles, 'fld1');
3: guidata(hObject, handles);
4: load('fld2.mat'); % load fld2 from the file
5: handles.fld1 = fld2;
However, after line 2 was executed, no memory was released, and the program crashed with the memory shortage error when line 4 was executed.
Next, I executed lines 1-2 above in another separate callback function:
1: function pushbutton2_Callback(hObject, eventdata, handles)
2: handles = rmfield(handles, 'fld1');
3: guidata(hObject, handles);
4:
5: function pushbutton1_Callback(hObject, eventdata, handles)
6: load('fld2.mat'); % load fld2 from the file
7: handles.fld1 = fld2;
pushbutton2 was pushed first, then pushbutton1 was pushed. In this case, the memory is acturall released after pushbutton2_Callback was finished, and no memory crash happened in pushbutton1_Callback, even after these processes were repeated many times.
I would like to do it in a single callback function like in the first code. Then, I tried the following code:
1: function pushbutton1_Callback(hObject, eventdata, handles)
2: handles = rmfield(handles, 'fld1');
3: guidata(hObject, handles);
4: refresh(gcf); refreshdata(gcf); drawnow expose update;
5: load('fld2.mat'); % load fld2 from the file
6: handles.fld1 = fld2;
However, no memory was released after line 4 was executed, and I got memory crash again at line 5.
How can I make sure the memory release of fields in the handles structure?

Answers (2)

Jan
Jan on 10 Mar 2011
You can try this C-Mex, which uses shared data copies: frmfield
  1 Comment
Osamu
Osamu on 11 Mar 2011
Thank you for your suggestion,
but even the cmex version didn't work.
After the execution of the frmfield function,
the deleted field disappears on the variable editor,
but the memory usage graph on the Task Manager doesn't change at all.
I'm afraid any change in a callback function doesn't take effect until the callback function is finished
and the control is given back to main event loop,
like in my second sample code.
That's why I wrote the line 4 in the third sample code.
However, even these three functions (refresh, refreshdata, drawnow) didn't work.

Sign in to comment.


Oleg Komarov
Oleg Komarov on 10 Mar 2011
You have to reassign the new structure:
s = struct('strings',{{'hello','yes'}},'lengths',[5 3]);
s = rmfield(s,'strings');
In your case:
handles = rmfield(handles, 'fld1');
EDIT
Save this code and open the task manager on the memory graph.
When the figure is created a rand(10000) matrix is created. Pushing the button the memory is cleared.
Can't make the other solution with subfunctions work...
function exampleGUI
S.data = rand(10000);
S.fh = figure('units','pixels',...
'position',[500 500 200 60],...
'menubar','none');
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Clear memory',...
'callback',@pb_call);
function pb_call(varargin)
S = rmfield(S, 'data');
end
end
Oleg
  5 Comments
Osamu
Osamu on 11 Mar 2011
My sample codes are in the m-file accompanied with the fig-file created by GUIDE.
In my second sample, the functions pushbutton1_Callback and pushbutton2_Callback are not nested to each other.
I tried to write a nested function in my program,
but it was rejected as a gramatical error,
and I was not able to have a nested function in my callback function.
Oleg Komarov
Oleg Komarov on 11 Mar 2011
You have to nest the callback in your main m-file as I shown above with my code.
1: function pushbutton1_Callback(varargin)
2: handles = rmfield(handles, 'fld1');
4: load('fld2.mat'); % load fld2 from the file
5: handles.fld1 = fld2;

Sign in to comment.

Categories

Find more on Structures 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!