rmfield does not release memory
이전 댓글 표시
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?
답변 (2개)
Oleg Komarov
2011년 3월 10일
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
Osamu
2011년 3월 10일
Oleg Komarov
2011년 3월 10일
Are you using callback functions as nested functions or subfunctions?
In the second case try to declare the subfunction as:
function handles = pb_call(hObject, eventdata, handles)
handles = rmfield(handles, 'fh');
end
Oleg Komarov
2011년 3월 10일
The solution doesn't work...I edit the post with a working subfunction solution.
Osamu
2011년 3월 11일
Oleg Komarov
2011년 3월 11일
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;
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!