GUI building and memory usage

조회 수: 6 (최근 30일)
David Rising
David Rising 2015년 5월 8일
답변: Malcolm Lidierth 2017년 1월 2일
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
Thomas Koelen
Thomas Koelen 2015년 5월 8일
편집: Thomas Koelen 2015년 5월 8일
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 2015년 5월 8일
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...

댓글을 달려면 로그인하십시오.

답변 (1개)

Malcolm Lidierth
Malcolm Lidierth 2017년 1월 2일
If you can, re-use figures rather than deleting them.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by