rmfield does not release memory
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Antworten (2)
Oleg Komarov
am 10 Mär. 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 Kommentare
Oleg Komarov
am 11 Mär. 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;
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!