Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Isaac

Date: 27 Oct, 2009 17:34:20

Message: 1 of 12

Hi all :)
in examining my code for some memory problems, i verified that the simple calling of a function (before it doing any operations!!!!!) deletes about 10 megabytes of ram and virtual memory... how is it possibile?
i'll write what i did so make the concept clearer:

main code:

feature memstats
y=functionA(x)


functionA code:

feature memstats
... all operations ....
end


the first feature memstats tells me:

Physical Memory (RAM):
        In Use: 1997 MB (7cdf8000)
        Free: 1067 MB (42bed000)
        Total: 3065 MB (bf9e5000)
    Page File (Swap space):
        In Use: 3100 MB (c1c00000)
        Free: 3238 MB (ca6c0000)
        Total: 6338 MB (18c2c0000)
    Virtual Memory (Address Space):
        In Use: 2059 MB (80bb8000)
        Free: 1012 MB (3f428000)
        Total: 3071 MB (bffe0000)
    Largest Contiguous Free Blocks:
         1. [at 8cd80000] 817 MB (331c0000)
         2. [at 73655000] 13 MB (00dfb000)
         3. [at 70af0000] 10 MB (00af0000)
         4. [at 79d32000] 10 MB (00a4e000)
         5. [at 68b00000] 10 MB (00a00000)
         6. [at 71da8000] 9 MB (00978000)
         7. [at 7f7f0000] 7 MB (007f0000)
         8. [at 72df2000] 7 MB (007de000)
         9. [at 71633000] 7 MB (0073d000)
        10. [at 7b9a0000] 6 MB (006a0000)
                                            ======= ==========
                                             902 MB (3861c000)



the second feature memstats tells me:

Physical Memory (RAM):
        In Use: 1998 MB (7ce22000)
        Free: 1067 MB (42bc3000)
        Total: 3065 MB (bf9e5000)
    Page File (Swap space):
        In Use: 3099 MB (c1b8d000)
        Free: 3239 MB (ca733000)
        Total: 6338 MB (18c2c0000)
    Virtual Memory (Address Space):
        In Use: 2075 MB (81b88000)
        Free: 996 MB (3e458000)
        Total: 3071 MB (bffe0000)
    Largest Contiguous Free Blocks:
         1. [at 8dd50000] 801 MB (321f0000)
         2. [at 73655000] 13 MB (00dfb000)
         3. [at 70af0000] 10 MB (00af0000)
         4. [at 79d32000] 10 MB (00a4e000)
         5. [at 68b00000] 10 MB (00a00000)
         6. [at 71da8000] 9 MB (00978000)
         7. [at 7f7f0000] 7 MB (007f0000)
         8. [at 72df2000] 7 MB (007de000)
         9. [at 71633000] 7 MB (0073d000)
        10. [at 7b9a0000] 6 MB (006a0000)
                                            ======= ==========
                                             886 MB (3764c000)




and more surprising...
this fact does not happen always.... it sometimes happen and sometimes not...!!!!

how can it be?

thanks everybody can help me solving this puzzle :)

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Bruno Luong

Date: 27 Oct, 2009 17:52:04

Message: 2 of 12

"Isaac " <asimov4000@hotmail.com> wrote in message <hc7aus$m7v$1@fred.mathworks.com>...
> Hi all :)
> in examining my code for some memory problems, i verified that the simple calling of a function (before it doing any operations!!!!!) deletes about 10 megabytes of ram and virtual memory... how is it possibile?

Matlab parses and loads functions into memory the first call.

Bruno

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: dpb

Date: 27 Oct, 2009 18:00:46

Message: 3 of 12

Isaac wrote:
...
> in examining my code for some memory problems, i verified that the
> simple calling of a function (before it doing any operations!!!!!)
> deletes about 10 megabytes of ram and virtual memory... how is it
> possibile?
> i'll write what i did so make the concept clearer:
...

> and more surprising...
> this fact does not happen always.... it sometimes happen and sometimes not...!!!!
>
> how can it be?

Virtual memory changes will have to do w/ what _else_ was loaded that
was swapped out, etc., in order to load the new....I'd not think it
really reproducible at all unless start from a fresh reboot every time
and follow same sequence of operations.

As Bruno says, ML caches functions on first call; your physical RAM
shows 1MB used in the example which wouldn't seem out of line at all.

Once loaded, a repeat load of the same function(s) will likely show
little if any change at all owing to having just cached the function(s)...

Nothing seems surprising to me at least so far...

--

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Isaac

Date: 28 Oct, 2009 10:10:03

Message: 4 of 12

dpb <none@non.net> wrote in message <hc7cq6$hvq$1@news.eternal-september.org>...
> Isaac wrote:
> ...
> > in examining my code for some memory problems, i verified that the
> > simple calling of a function (before it doing any operations!!!!!)
> > deletes about 10 megabytes of ram and virtual memory... how is it
> > possibile?
> > i'll write what i did so make the concept clearer:
> ...
>
> > and more surprising...
> > this fact does not happen always.... it sometimes happen and sometimes not...!!!!
> >
> > how can it be?
>
> Virtual memory changes will have to do w/ what _else_ was loaded that
> was swapped out, etc., in order to load the new....I'd not think it
> really reproducible at all unless start from a fresh reboot every time
> and follow same sequence of operations.
>
> As Bruno says, ML caches functions on first call; your physical RAM
> shows 1MB used in the example which wouldn't seem out of line at all.
>
> Once loaded, a repeat load of the same function(s) will likely show
> little if any change at all owing to having just cached the function(s)...
>
> Nothing seems surprising to me at least so far...
>
> --

i did not understand well what you said...

i noticed that during this particular cycle memory disappear:

for j=numerodate:-1:2
    if weekday(datenum(Matriceserie(1,j)))>weekday(datenum(Matriceserie(1,j-1)))
        Matriceserie(:,j-1)=[];
    end
end

where Matriceserie is a 2 row matrix: first row with dates and second row with numbers

can be function "weekday" the guilt?

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Isaac

Date: 28 Oct, 2009 12:19:02

Message: 5 of 12

"Isaac " <asimov4000@hotmail.com> wrote in message <hc959r$rf7$1@fred.mathworks.com>...
> dpb <none@non.net> wrote in message <hc7cq6$hvq$1@news.eternal-september.org>...
> > Isaac wrote:
> > ...
> > > in examining my code for some memory problems, i verified that the
> > > simple calling of a function (before it doing any operations!!!!!)
> > > deletes about 10 megabytes of ram and virtual memory... how is it
> > > possibile?
> > > i'll write what i did so make the concept clearer:
> > ...
> >
> > > and more surprising...
> > > this fact does not happen always.... it sometimes happen and sometimes not...!!!!
> > >
> > > how can it be?
> >
> > Virtual memory changes will have to do w/ what _else_ was loaded that
> > was swapped out, etc., in order to load the new....I'd not think it
> > really reproducible at all unless start from a fresh reboot every time
> > and follow same sequence of operations.
> >
> > As Bruno says, ML caches functions on first call; your physical RAM
> > shows 1MB used in the example which wouldn't seem out of line at all.
> >
> > Once loaded, a repeat load of the same function(s) will likely show
> > little if any change at all owing to having just cached the function(s)...
> >
> > Nothing seems surprising to me at least so far...
> >
> > --
>
> i did not understand well what you said...
>
> i noticed that during this particular cycle memory disappear:
>
> for j=numerodate:-1:2
> if weekday(datenum(Matriceserie(1,j)))>weekday(datenum(Matriceserie(1,j-1)))
> Matriceserie(:,j-1)=[];
> end
> end
>
> where Matriceserie is a 2 row matrix: first row with dates and second row with numbers
>
> can be function "weekday" the guilt?

i've further analyzed the code and the result is that is this single instruction which deletes 16 MB of virtual memory :

 w1=weekday(d1);


can anyone tell me pls how can i avoid this in anyway?

thanks

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Bruno Luong

Date: 28 Oct, 2009 12:51:03

Message: 6 of 12

>
>
> can anyone tell me pls how can i avoid this in anyway?
>

You can't.

Bruno

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Steven Lord

Date: 28 Oct, 2009 14:00:49

Message: 7 of 12


"Isaac " <asimov4000@hotmail.com> wrote in message
news:hc9crm$2eu$1@fred.mathworks.com...
> "Isaac " <asimov4000@hotmail.com> wrote in message
> <hc959r$rf7$1@fred.mathworks.com>...

*snip*

>> i noticed that during this particular cycle memory disappear:
>>
>> for j=numerodate:-1:2
>> if
>> weekday(datenum(Matriceserie(1,j)))>weekday(datenum(Matriceserie(1,j-1)))
>> Matriceserie(:,j-1)=[];

Note that this line of code needs to make a copy of Matriceserie (without
its j-1st column.)

>> end
>> end
>>
>> where Matriceserie is a 2 row matrix: first row with dates and second row
>> with numbers
>>
>> can be function "weekday" the guilt?
>
> i've further analyzed the code and the result is that is this single
> instruction which deletes 16 MB of virtual memory :
>
> w1=weekday(d1);

You're misusing terminology. MATLAB is no way, shape, or form "deletes"
memory. It _USES_ memory, but that's different.

Anyway, the WEEKDAY function uses some memory internally to perform the
calculations necessary to obtain the result, as well as allocating some
memory for that result which it will return to the function that called it.
As you call functions and create or clear variables, the amount of memory
available (as well as the largest contiguous block of memory available to
MATLAB) may vary.

> can anyone tell me pls how can i avoid this in anyway?

You can't. Well, okay, you _could_, but only by avoiding creating variables
and calling functions, and that would be kind of a boring MATLAB session.

For more information about how MATLAB handles memory, read documents 1106
and 1107 in the MATLAB Programming section on this page:

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

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Isaac

Date: 28 Oct, 2009 14:36:03

Message: 8 of 12

"Steven Lord" <slord@mathworks.com> wrote in message <hc9ip0$mnh$1@fred.mathworks.com>...
>
> "Isaac " <asimov4000@hotmail.com> wrote in message
> news:hc9crm$2eu$1@fred.mathworks.com...
> > "Isaac " <asimov4000@hotmail.com> wrote in message
> > <hc959r$rf7$1@fred.mathworks.com>...
>
> *snip*
>
> >> i noticed that during this particular cycle memory disappear:
> >>
> >> for j=numerodate:-1:2
> >> if
> >> weekday(datenum(Matriceserie(1,j)))>weekday(datenum(Matriceserie(1,j-1)))
> >> Matriceserie(:,j-1)=[];
>
> Note that this line of code needs to make a copy of Matriceserie (without
> its j-1st column.)
>
> >> end
> >> end
> >>
> >> where Matriceserie is a 2 row matrix: first row with dates and second row
> >> with numbers
> >>
> >> can be function "weekday" the guilt?
> >
> > i've further analyzed the code and the result is that is this single
> > instruction which deletes 16 MB of virtual memory :
> >
> > w1=weekday(d1);
>
> You're misusing terminology. MATLAB is no way, shape, or form "deletes"
> memory. It _USES_ memory, but that's different.
>
> Anyway, the WEEKDAY function uses some memory internally to perform the
> calculations necessary to obtain the result, as well as allocating some
> memory for that result which it will return to the function that called it.
> As you call functions and create or clear variables, the amount of memory
> available (as well as the largest contiguous block of memory available to
> MATLAB) may vary.
>
> > can anyone tell me pls how can i avoid this in anyway?
>
> You can't. Well, okay, you _could_, but only by avoiding creating variables
> and calling functions, and that would be kind of a boring MATLAB session.
>
> For more information about how MATLAB handles memory, read documents 1106
> and 1107 in the MATLAB Programming section on this page:
>
> http://www.mathworks.com/support/tech-notes/list_all.html
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

thanks You for all answering :)
i will try to build "weekday" function on my own and i'll what happens :)
thanks

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Philip Borghesani

Date: 28 Oct, 2009 15:28:17

Message: 9 of 12


"Isaac " <asimov4000@hotmail.com> wrote in message news:hc7aus$m7v$1@fred.mathworks.com...
> Hi all :)
> in examining my code for some memory problems, i verified that the simple calling of a function (before it doing any
> operations!!!!!) deletes about 10 megabytes of ram and virtual memory... how is it possibile?
> i'll write what i did so make the concept clearer:
>
> main code:
>
> feature memstats
> y=functionA(x)
>
>
> functionA code:
>
> feature memstats
> ... all operations ....
> end
>
>
> the first feature memstats tells me:
>
> Physical Memory (RAM):
> In Use: 1997 MB (7cdf8000)
> Free: 1067 MB (42bed000)
> Total: 3065 MB (bf9e5000)
> Page File (Swap space):
> In Use: 3100 MB (c1c00000)
> Free: 3238 MB (ca6c0000)
> Total: 6338 MB (18c2c0000)
> Virtual Memory (Address Space):
> In Use: 2059 MB (80bb8000)
> Free: 1012 MB (3f428000)
> Total: 3071 MB (bffe0000)
> Largest Contiguous Free Blocks:
> 1. [at 8cd80000] 817 MB (331c0000)
> 2. [at 73655000] 13 MB (00dfb000)
> 3. [at 70af0000] 10 MB (00af0000)
> 4. [at 79d32000] 10 MB (00a4e000)
> 5. [at 68b00000] 10 MB (00a00000)
> 6. [at 71da8000] 9 MB (00978000)
> 7. [at 7f7f0000] 7 MB (007f0000)
> 8. [at 72df2000] 7 MB (007de000)
> 9. [at 71633000] 7 MB (0073d000)
> 10. [at 7b9a0000] 6 MB (006a0000)
> ======= ==========
> 902 MB (3861c000)
>
>
Isaac
I think you may be looking at the wong problem. Somthing about your configuration is unusual, MATLAB should be starting with much
more virtual memory free. Your machine appears to be configued to allow 3Gb per process instead of the usual 2GB in Windows but for
some reason that memory does not seem to be avalible to MATLAB. Under normal conditions when MATLAB starts up it uses between 500MB
and 1000MB virtual memory depending on if Simulink has been started in your machine. Feature dumpmem may show some hints where the
memory is going. What version of Windows are you running and do you know how it is configured to use memory?

Phil

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Isaac

Date: 28 Oct, 2009 15:45:19

Message: 10 of 12

"Philip Borghesani" <philip_borghesani@mathworks.spam> wrote in message <hc9nuh$q3k$1@fred.mathworks.com>...
>
> "Isaac " <asimov4000@hotmail.com> wrote in message news:hc7aus$m7v$1@fred.mathworks.com...
> > Hi all :)
> > in examining my code for some memory problems, i verified that the simple calling of a function (before it doing any
> > operations!!!!!) deletes about 10 megabytes of ram and virtual memory... how is it possibile?
> > i'll write what i did so make the concept clearer:
> >
> > main code:
> >
> > feature memstats
> > y=functionA(x)
> >
> >
> > functionA code:
> >
> > feature memstats
> > ... all operations ....
> > end
> >
> >
> > the first feature memstats tells me:
> >
> > Physical Memory (RAM):
> > In Use: 1997 MB (7cdf8000)
> > Free: 1067 MB (42bed000)
> > Total: 3065 MB (bf9e5000)
> > Page File (Swap space):
> > In Use: 3100 MB (c1c00000)
> > Free: 3238 MB (ca6c0000)
> > Total: 6338 MB (18c2c0000)
> > Virtual Memory (Address Space):
> > In Use: 2059 MB (80bb8000)
> > Free: 1012 MB (3f428000)
> > Total: 3071 MB (bffe0000)
> > Largest Contiguous Free Blocks:
> > 1. [at 8cd80000] 817 MB (331c0000)
> > 2. [at 73655000] 13 MB (00dfb000)
> > 3. [at 70af0000] 10 MB (00af0000)
> > 4. [at 79d32000] 10 MB (00a4e000)
> > 5. [at 68b00000] 10 MB (00a00000)
> > 6. [at 71da8000] 9 MB (00978000)
> > 7. [at 7f7f0000] 7 MB (007f0000)
> > 8. [at 72df2000] 7 MB (007de000)
> > 9. [at 71633000] 7 MB (0073d000)
> > 10. [at 7b9a0000] 6 MB (006a0000)
> > ======= ==========
> > 902 MB (3861c000)
> >
> >
> Isaac
> I think you may be looking at the wong problem. Somthing about your configuration is unusual, MATLAB should be starting with much
> more virtual memory free. Your machine appears to be configued to allow 3Gb per process instead of the usual 2GB in Windows but for
> some reason that memory does not seem to be avalible to MATLAB. Under normal conditions when MATLAB starts up it uses between 500MB
> and 1000MB virtual memory depending on if Simulink has been started in your machine. Feature dumpmem may show some hints where the
> memory is going. What version of Windows are you running and do you know how it is configured to use memory?
>
> Phil
>

Hi Phil thanks for your advice but Matlab does start with much more virtual memory free. In those cases there was much less because i had been using it long without starting it again!

Anyway i am using a function made by me instead of "weekday"

function [giornosettimanale]=giornosettimana(datenumgiorno)

datenumriferimento=datenum('01/01/1990');
giornosettimanaleriferimento=2;

differenzadatenum=datenumgiorno-datenumriferimento;

giornosettimanale=giornosettimanaleriferimento+ differenzadatenum-floor(differenzadatenum/7)*7;


and those problems have gone away!!!! (badly it's much slower)
so the problem was indeed in the function weekday!

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Steven Lord

Date: 28 Oct, 2009 17:19:48

Message: 11 of 12


"Isaac " <asimov4000@hotmail.com> wrote in message
news:hc9ouf$svt$1@fred.mathworks.com...

*snip*

> Hi Phil thanks for your advice but Matlab does start with much more
> virtual memory free. In those cases there was much less because i had been
> using it long without starting it again!
>
> Anyway i am using a function made by me instead of "weekday"
>
> function [giornosettimanale]=giornosettimana(datenumgiorno)
>
> datenumriferimento=datenum('01/01/1990');
> giornosettimanaleriferimento=2;
>
> differenzadatenum=datenumgiorno-datenumriferimento;
>
> giornosettimanale=giornosettimanaleriferimento+
> differenzadatenum-floor(differenzadatenum/7)*7;
>
>
> and those problems have gone away!!!! (badly it's much slower)
> so the problem was indeed in the function weekday!

I wouldn't say that "the problem" was in the function weekday -- I would say
that WEEKDAY is more general and handles more cases than the code you wrote.
For example, as documented in the reference page for WEEKDAY:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/weekday.html

It handles different locales. It also does some error checking on its
inputs. But if the code you've written is sufficient for your needs, by all
means use it.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: RANDOM LOOSING MEMORY WITH ONLY CALLING A FUNCTION?

From: Isaac

Date: 28 Oct, 2009 17:25:35

Message: 12 of 12

"Steven Lord" <slord@mathworks.com> wrote in message <hc9ue3$jjv$1@fred.mathworks.com>...
>
> "Isaac " <asimov4000@hotmail.com> wrote in message
> news:hc9ouf$svt$1@fred.mathworks.com...
>
> *snip*
>
> > Hi Phil thanks for your advice but Matlab does start with much more
> > virtual memory free. In those cases there was much less because i had been
> > using it long without starting it again!
> >
> > Anyway i am using a function made by me instead of "weekday"
> >
> > function [giornosettimanale]=giornosettimana(datenumgiorno)
> >
> > datenumriferimento=datenum('01/01/1990');
> > giornosettimanaleriferimento=2;
> >
> > differenzadatenum=datenumgiorno-datenumriferimento;
> >
> > giornosettimanale=giornosettimanaleriferimento+
> > differenzadatenum-floor(differenzadatenum/7)*7;
> >
> >
> > and those problems have gone away!!!! (badly it's much slower)
> > so the problem was indeed in the function weekday!
>
> I wouldn't say that "the problem" was in the function weekday -- I would say
> that WEEKDAY is more general and handles more cases than the code you wrote.
> For example, as documented in the reference page for WEEKDAY:
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/weekday.html
>
> It handles different locales. It also does some error checking on its
> inputs. But if the code you've written is sufficient for your needs, by all
> means use it.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

sure. i didn't want to appearinf offensive regarding the function weekday.
Anyway it's still a puzzle how happened that in some cases it used 16 MB of ram...

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us