GUI memory management guidata

6 views (last 30 days)
Vincent
Vincent on 9 Dec 2016
Commented: Walter Roberson on 20 Dec 2016
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)?

Accepted Answer

Jan
Jan on 9 Dec 2016
Edited: Jan on 9 Dec 2016
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 Comments
Jan
Jan on 20 Dec 2016
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
Walter Roberson on 20 Dec 2016
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

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!