GUI memory management guidata
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi all,
I'm struggling with memory management in a GUI created with guide. My MATLAB version is R2014b.
I use guidata to store data in the handles structure to pass large structures (>1 GB) from one function to another inside my GUI.
Inside a Callback function (of a pushbutton), I would like to erase some of those large structures saved in the handles of guidata and create new ones. However, I cannot seem to delete those structures in the function itself. I added the following code in the function:
handles = guidata(hObject);
handles = rmfield(handles, 'my_variable');
guidata(hObject, handles);
But inside the function, the memory is not cleared. I assume it is because the function in which I call this code has hObject and handles as input parameters (it's a callback function...) The "pack" command also has no effect. However, if I debug on these lines and then during function execution quit debugging (so the function stops abruptly), the memory is cleared and my GUI is still open.
So how can I "reset" this guidata while executing a (Callback) function?
I noticed others were struggling with this too, but did not find a solution; e.g. here: https://nl.mathworks.com/matlabcentral/newsreader/view_thread/297824 (last 3 posts)
Thanks in advance! Vincent
Update From Sven's answer in this topic, I assume that after a Callbackfunction is called, the input parameters are actually a copy of the guihandles. So during the Callbackfunction's execution, the same copy is being stored in guihandles. That explains why the memory stays occupied. So how can I obviate that such copy is being made (or remove the variables in the copy as well)?
댓글 수: 0
채택된 답변
Jan
2016년 12월 9일
편집: Jan
2016년 12월 9일
How do you control of the memory is released? You can control this on the level of the operating system only.
Removing the field in the handles struct and overwriting the struct in the figure's application data by guidata removes the field efficiently. Afterwards the memory is not occupied anymore, but neither Matlab nor the OS have to give this memory back directly for allocating new arrays. Matlab can keep this memory until it finds time for a garbage collection and the OS will wait with clearing the memory by overwriting it with zeros until one of the processor cores finds the time for this job. Therefore I doubt that you can really determine when the release of the memory exactly happens.
Usually this is not a problem, because the OS cares for cleaning memory with a high precedence if it is getting low on it. Therefore the memory management is usually not a specific problem for users. What exactly is your problem you want to solve?
댓글 수: 7
Jan
2016년 12월 20일
I'm used to obtain the "handles" struct (a misleading name) dynamically using guidata, which calls setappdata / getappdata internally also. Therefore "handles struct" and "setappdata" might be exactly the same.
Walter Roberson
2016년 12월 20일
setappdata() / getappdata() allows you to give your own name for the item, and so allows you to store and retrieve different parts independently. The handles structure is one particular *appdata item, 'UsedByGUIData_m' in current versions
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!