Clear Filters
Clear Filters

GUI building and memory usage

8 views (last 30 days)
David Rising
David Rising on 8 May 2015
Answered: Malcolm Lidierth on 2 Jan 2017
This is maybe a two part question, one specific to my program and one general to gui programming. Specifically, I have a close function for my main gui figure which saves some values stored inside the global structure to a mat file. To actually close the figure and clear the global variable, I added:
delete(1000); %the figure number
clearvars -global s
to the end of my close_func. This doesn't seem to completely free up all of the memory however as I can see the amount of memory that matlab uses increases when I open and close the gui multiple times. Unless there is something going on in Matlab independent of my program which is causing this.
In addition, if I open the program enough times, I eventually get a bunch of java exceptions and the program no longer works correctly. I then have to close Matlab and restart the program. I am assuming that these issues are related. I have discovered in the matlab help an entry about not using the get and set commands with Java objects as this will cause a memory leak. Is this also the case for normal uicontrol/uipanel etc gui objects? Am I correct when I say that they are also Java based?
Thanks and sorry for the possibly ignorant questions but software programming is not my strong suit. Although it is a lot of fun!
  2 Comments
Thomas Koelen
Thomas Koelen on 8 May 2015
Edited: Thomas Koelen on 8 May 2015
This is exactly why you stay away from globals...
Use the save and the load function instead!
Matt Fig summarizes it pretty well in one of his posts:
Globals not good: Experience! In my work I help others with MATLAB. Beginners dive right in to globals because they think using them is easy. They are also easy to do wrong. Too often in my work I have to trace through all a user's M-files that use globals, and the base workspace, to find where the user accidentally made a change that is bugging up a entire complicated project. Each function has its own workspace for a reason(s) - and easy bug tracking is one. I always end up teaching people to program by careful data management rather than relying on globals. Usually they have less problems after that with "Where the @#$#*@ is the bug!"
David Rising
David Rising on 8 May 2015
I had also read this when I first started to develop the program a while back but couldn't get the callbacks to function correctly for some reason. At the moment the basic program works like so:
global s
s.fig=figure(1000)
then I have a series of what I call "build functions" which do not have the global s declaration but rather read s as an input, modify the figure by adding controls plots etc, then put the modified s back out again. However the call backs all have the global s definition at the beginning because I couldn't get them to work with:
'callback',{@function_name,s}
and use varargin in the input for the callback. That is s=varargin{3} (I think it was three, can't remember at the moment).
Maybe worth noting that the intial program start is a function and not a script. Not sure what I am doing wrong here though...

Sign in to comment.

Answers (1)

Malcolm Lidierth
Malcolm Lidierth on 2 Jan 2017
If you can, re-use figures rather than deleting them.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!