Orphaned handle objects seem to not be garbage collected

  • Follow


I have a simulation that depends heavily on two handle classes. Many of these classes are constructed during each iteration but most of them get cleared later in the iteration. Thus I might create a vector of these objects of length 1000 but trim it to length 50.

After about 50 iterations, my memory usage is up to about 1 GB. If I then do a bunch of clearing (clear; clear all; clear classes; ) the memory is still not restored. 

Any ideas that don't involve the words "memory leak"?
0
Reply Matt 11/5/2010 5:54:05 PM

Did you try "close all"?
0
Reply Matt 11/5/2010 7:00:07 PM



"Matt " <matt.hilt@iHateSpam.numerica.us> wrote in message 
news:ib1gbs$sdh$1@fred.mathworks.com...
> I have a simulation that depends heavily on two handle classes. Many of 
> these classes are constructed during each iteration but most of them get 
> cleared later in the iteration. Thus I might create a vector of these 
> objects of length 1000 but trim it to length 50.
>
> After about 50 iterations, my memory usage is up to about 1 GB. If I then 
> do a bunch of clearing (clear; clear all; clear classes; ) the memory is 
> still not restored.
> Any ideas that don't involve the words "memory leak"?

Are you _certain_ that these handle objects are not in some scope (such as 
captured in the workspace of an anonymous function, in a Handle Graphics 
object property like UserData, in a property of another handle object, etc.) 
that would prevent them from being cleared?

Have you reviewed this document to determine if any of the issues it 
describes apply to the OS/version combination you're using?

http://www.mathworks.com/support/tech-notes/1100/1107.html

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 

0
Reply Steven_Lord 11/5/2010 8:17:54 PM

I'm exhibiting this problem as well in Matlab R2010b. I have objects that are instances of handle classes, but objects of one particular handle class do not seem to be cleaned up after invocation. For me it's not a memory issue, it's just that old state seems to persist. "Clear all" doesn't work nor does "close all". The only solution has  been to restart Matlab. I don't see how anything can be within scope if the function terminates. There are no global variables. 

Objects of my problematic class are components of another handle class. Both the holding object and the component that it owns have handles to each other...but it shouldn't matter because when the program terminates all references are invalid and should be garbage collected. I feel like this is a bug in the garbage collector with some corner case.
-mas


"Steven_Lord" <slord@mathworks.com> wrote in message <ib1opi$ein$1@fred.mathworks.com>...
> 
> 
> "Matt " <matt.hilt@iHateSpam.numerica.us> wrote in message 
> news:ib1gbs$sdh$1@fred.mathworks.com...
> > I have a simulation that depends heavily on two handle classes. Many of 
> > these classes are constructed during each iteration but most of them get 
> > cleared later in the iteration. Thus I might create a vector of these 
> > objects of length 1000 but trim it to length 50.
> >
> > After about 50 iterations, my memory usage is up to about 1 GB. If I then 
> > do a bunch of clearing (clear; clear all; clear classes; ) the memory is 
> > still not restored.
> > Any ideas that don't involve the words "memory leak"?
> 
> Are you _certain_ that these handle objects are not in some scope (such as 
> captured in the workspace of an anonymous function, in a Handle Graphics 
> object property like UserData, in a property of another handle object, etc.) 
> that would prevent them from being cleared?
> 
> Have you reviewed this document to determine if any of the issues it 
> describes apply to the OS/version combination you're using?
> 
> http://www.mathworks.com/support/tech-notes/1100/1107.html
> 
> -- 
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on 
> http://www.mathworks.com 
0
Reply Mirza 2/7/2011 6:31:04 PM

I have the same issue, I am reading a bunch of ascii files in matlab, and the memory keeps increasing...clear doesn't work...at a point matlab becomes really slow and I have to restart matlab again...would really appreciate if anyone could please suggest any tricks to avoid this issue...like inserting some kind of 'memory release' statements within the code...etc..

"Matt" wrote in message <ib1gbs$sdh$1@fred.mathworks.com>...
> I have a simulation that depends heavily on two handle classes. Many of these classes are constructed during each iteration but most of them get cleared later in the iteration. Thus I might create a vector of these objects of length 1000 but trim it to length 50.
> 
> After about 50 iterations, my memory usage is up to about 1 GB. If I then do a bunch of clearing (clear; clear all; clear classes; ) the memory is still not restored. 
> 
> Any ideas that don't involve the words "memory leak"?
0
Reply jay.oberoi (8) 4/9/2011 5:24:04 PM

On 4/9/2011 12:24 PM, jay jay wrote:
> I have the same issue, I am reading a bunch of ascii files in matlab,
> and the memory keeps increasing...clear doesn't work...at a point matlab
> becomes really slow and I have to restart matlab again...would really
> appreciate if anyone could please suggest any tricks to avoid this
> issue...like inserting some kind of 'memory release' statements within
> the code...etc..
....

Don't write code that loses track of handles for the actual subject...

You don't post enough information to tell; one might guess your case is 
you're adding to existing arrays or otherwise growing data structures as 
you proceed instead of either reusing or releasing memory instead...

--
0
Reply none1568 (6654) 4/9/2011 5:44:33 PM

Thanks dpb for your reply. Basically what I am doing is reading a bunch of files in ascii format and then converting them into .mat files. I am doing this in a 'for' loop several times and none of the variables are getting carried from one iteration to another. But as iterations proceed the memory usage is over 1GB and things start to get very very slow. Please see the code below:

for sID = 1:numOfSymbols
    symbols(sID) 
    for a_date = 1:num_dates
        file = strcat(origin_dir, num2str(dates(a_date)) , '\\', char(symbols(sID)), '.csv' );
        
        outFile = strcat(outDir, num2str(dates(a_date)) , '\\', char(symbols(sID)), '_list.mat');
                
        [C{1:4}] = textread(file,'%s%s%f%f','delimiter',',');
        tt                 = datenum(C{2});
        msecsMnite   = round(86400*1000*mod(tt,1));
        epoc2Mnite = int32((datenum(C{1}(1),'yyyymmdd')-datenum('19700101','yyyymmdd'))*86400);

        data = struct('e',epoc2Mnite,'n',length(C{1}),'m',msecsMnite,'s',C{4},'p',C{3});
        
        save(outFile,'data');
    end
end

dpb <none@non.net> wrote in message <inq5tu$4l0$2@speranza.aioe.org>...
> On 4/9/2011 12:24 PM, jay jay wrote:
> > I have the same issue, I am reading a bunch of ascii files in matlab,
> > and the memory keeps increasing...clear doesn't work...at a point matlab
> > becomes really slow and I have to restart matlab again...would really
> > appreciate if anyone could please suggest any tricks to avoid this
> > issue...like inserting some kind of 'memory release' statements within
> > the code...etc..
> ...
> 
> Don't write code that loses track of handles for the actual subject...
> 
> You don't post enough information to tell; one might guess your case is 
> you're adding to existing arrays or otherwise growing data structures as 
> you proceed instead of either reusing or releasing memory instead...
> 
> --
0
Reply jay.oberoi (8) 4/9/2011 6:05:20 PM

On 4/9/2011 1:05 PM, jay jay wrote:

....[top posting repaired; please don't do that--hard makes thread follow]...

> dpb <none@non.net> wrote in message <inq5tu$4l0$2@speranza.aioe.org>...
>> On 4/9/2011 12:24 PM, jay jay wrote:
>> > I have the same issue, I am reading a bunch of ascii files in matlab,
>> > and the memory keeps increasing...clear doesn't work...at a point
....
>> You don't post enough information to tell; one might guess your case
>> is you're adding to existing arrays or otherwise growing data
>> structures as you proceed instead of either reusing or releasing
>> memory instead...
....

> Thanks dpb for your reply. Basically what I am doing is reading a
> bunch of files in ascii format and then converting them into .mat
> files. I am doing this in a 'for' loop several times and none of the
> variables are getting carried from one iteration to another. But as
> iterations proceed the memory usage is over 1GB and things start to
> get very very slow.
 > Please see the code below:
 >
 > for sID = 1:numOfSymbols
 >   symbols(sID)
     for a_date = 1:num_dates
 >     file = [origin_dir, num2str(dates(a_date)) ,  ...
                  '\\',char(symbols(sID)), '.csv'];
 >     outFile = [outDir, num2str(dates(a_date)) , '\\', ...
 >                  char(symbols(sID)), '_list.mat'];
 >     [C{1:4}] = textread(file,'%s%s%f%f','delimiter',',');
 >     tt = datenum(C{2});
 >     msecsMnite = round(86400*1000*mod(tt,1));
 >     epoc2Mnite = int32((datenum(C{1}(1),'yyyymmdd') - ...
                           datenum('19700101','yyyymmdd'))*86400);
 >
 >     data =  struct('e',epoc2Mnite,'n',length(C{1}), ...
                      'm',msecsMnite,'s',C{4},'p',C{3});
 >     save(outFile,'data');
 >   end
 > end

I have to admit I've not used struct() enough to have any feeling for 
what it does w/ memory so don't know enough to conclusively state but...

What happens to your memory usage if you just look at the result of the 
textread() call?

I'd probably consider a

clear C data

as reasonable practice.

--
0
Reply none1568 (6654) 4/9/2011 8:58:48 PM

Thanks, yea i too think the problem is coming out of textread. Could you please let me know the 'clear c data' command. I couldn't find this in documentation. Thanks!!


dpb <none@non.net> wrote in message <inqha5$13o$1@speranza.aioe.org>...
> On 4/9/2011 1:05 PM, jay jay wrote:
> 
> ...[top posting repaired; please don't do that--hard makes thread follow]...
> 
> > dpb <none@non.net> wrote in message <inq5tu$4l0$2@speranza.aioe.org>...
> >> On 4/9/2011 12:24 PM, jay jay wrote:
> >> > I have the same issue, I am reading a bunch of ascii files in matlab,
> >> > and the memory keeps increasing...clear doesn't work...at a point
> ...
> >> You don't post enough information to tell; one might guess your case
> >> is you're adding to existing arrays or otherwise growing data
> >> structures as you proceed instead of either reusing or releasing
> >> memory instead...
> ...
> 
> > Thanks dpb for your reply. Basically what I am doing is reading a
> > bunch of files in ascii format and then converting them into .mat
> > files. I am doing this in a 'for' loop several times and none of the
> > variables are getting carried from one iteration to another. But as
> > iterations proceed the memory usage is over 1GB and things start to
> > get very very slow.
>  > Please see the code below:
>  >
>  > for sID = 1:numOfSymbols
>  >   symbols(sID)
>      for a_date = 1:num_dates
>  >     file = [origin_dir, num2str(dates(a_date)) ,  ...
>                   '\\',char(symbols(sID)), '.csv'];
>  >     outFile = [outDir, num2str(dates(a_date)) , '\\', ...
>  >                  char(symbols(sID)), '_list.mat'];
>  >     [C{1:4}] = textread(file,'%s%s%f%f','delimiter',',');
>  >     tt = datenum(C{2});
>  >     msecsMnite = round(86400*1000*mod(tt,1));
>  >     epoc2Mnite = int32((datenum(C{1}(1),'yyyymmdd') - ...
>                            datenum('19700101','yyyymmdd'))*86400);
>  >
>  >     data =  struct('e',epoc2Mnite,'n',length(C{1}), ...
>                       'm',msecsMnite,'s',C{4},'p',C{3});
>  >     save(outFile,'data');
>  >   end
>  > end
> 
> I have to admit I've not used struct() enough to have any feeling for 
> what it does w/ memory so don't know enough to conclusively state but...
> 
> What happens to your memory usage if you just look at the result of the 
> textread() call?
> 
> I'd probably consider a
> 
> clear C data
> 
> as reasonable practice.
> 
> --
0
Reply jay.oberoi (8) 4/10/2011 12:26:04 AM

On 4/9/2011 7:26 PM, jay jay wrote:
> Thanks, yea i too think the problem is coming out of textread. Could you
> please let me know the 'clear c data' command. I couldn't find this in
> documentation. Thanks!!

_DON'T TOP_POST!!!!_

doc clear  % 'clear' followed by variables, handles, etc., ...

What evidence is there to support "I think"?  I don't think there's any 
reason at all that textread() would show such behavior unless your 
script/function is concatenating data sets.  I asked specifically to 
begin to isolate the problem and to isolate the cause of the symptom(s) 
as well as to more accurately describe them.

A "whos" judiciously placed and a debug session could go a long ways 
towards finding out where memory goes if (as I suspect) it isn't 
actually being leaked.

--
0
Reply none1568 (6654) 4/10/2011 12:51:16 AM

9 Replies
224 Views

(page loaded in 0.088 seconds)

Similiar Articles:









7/24/2012 12:32:25 AM


Reply: