# Regarding calculation of free memory

Started by September 28, 2005
```Gurus,
I was just wondering what could be the best possible way to calculate
free memory while our code is running in SDRAM.I have used vxworks and
it uses a approach to calculate the free memory as follows:
Fill up the entire stack with 0xeeeee and then when you start using the
stack,if at all a particular address in memory is used,the value of
0xeeeee in that location should be overwritten with real data.So if we
are able to track the first occurance of the pattern 0xeeeee we should
be able to subtract it from the entire size of memory and make a fair
assesment of the available free memory.

How ever I beleive such a algorithm has drawbacks.First drawback which
I can think is
1)What is the guarentee that the real data is not ur pattern chosen(In
vxworks case,its 0xeeeee,if 0xeeeee itself is real data,we cant find
out real size!)?

2)What should you look for to identify unique pattern?Or other way how
would you arrive at the pattern word to be used?

3)Are there any other good techniques for calculating free memory?

4)We are making software for a consumer electronics appliance.It uses a
premitive propreitory RTOS which does not give enough tools to find out
free memory.We are in a forced situation to reduce memory
consumption,so we have to get a way to calculate free available memory
first.To add to the complexity,our application uses
heap,partitions,partition with in partitions.At any point of time we
have to give the user memory utilised like most RTOS tools
provide.Since Heap is dynamic how do u arrive at a fair judgment?
If we wanted to get total free space,we need to calculate free space in
heap,free space in partition and then arrive at it.

Are any one aware of any good way to achieve it in the given case of
RTOS vendor not providing tools for memory calculations?

Regards,
s.subbarayan

```
```> I was just wondering what could be the best possible way to calculate
> free memory while our code is running in SDRAM.I have used vxworks and
> it uses a approach to calculate the free memory as follows:
> Fill up the entire stack with 0xeeeee and then when you start using the
> stack,if at all a particular address in memory is used,the value of
> 0xeeeee in that location should be overwritten with real data.So if we
> are able to track the first occurance of the pattern 0xeeeee we should
> be able to subtract it from the entire size of memory and make a fair
> assesment of the available free memory.

It sounds like your 0xeeeee method you outline is perfectly suited for
your purposes.  Even if 0xeeeee is actually part of your real data that
happens to be in the stack, consider that it's highly unlikely that
0xeeeee will fill your stack to the extent that your stack usage figure
is way off.  The point is, even if 0xeeeee is real stack data, the
information you get from hunting for unused stack will be close enough.
Get your usage total and add some margin for safety and you're done.

> How ever I beleive such a algorithm has drawbacks.First drawback which
> I can think is
> 1)What is the guarentee that the real data is not ur pattern chosen(In
> vxworks case,its 0xeeeee,if 0xeeeee itself is real data,we cant find
> out real size!)?

yourself if the pattern you've selected is likely to be real data or
not.

> 2)What should you look for to identify unique pattern?Or other way how
> would you arrive at the pattern word to be used?

> 3)Are there any other good techniques for calculating free memory?

Don't know but...  This method is commonly used and usually works very
well.  A good white-box test designed to stress the stack usage will
give you the real info you need to accurately size your RAM.

> 4)We are making software for a consumer electronics appliance.It uses a
> premitive propreitory RTOS which does not give enough tools to find out
> free memory.We are in a forced situation to reduce memory
> consumption,so we have to get a way to calculate free available memory
> first.To add to the complexity,our application uses
> heap,partitions,partition with in partitions.At any point of time we
> have to give the user memory utilised like most RTOS tools
> provide.Since Heap is dynamic how do u arrive at a fair judgment?
> If we wanted to get total free space,we need to calculate free space in
> heap,free space in partition and then arrive at it.

IMO, it's like gambling when you size how much stack margin you want to
exhaustive), you will not need to add much stack margin.  If you don't
do SW testing, it's a roll of the dice.  You probably fit in the middle
of the two extremes.

> Are any one aware of any good way to achieve it in the given case of
> RTOS vendor not providing tools for memory calculations?

Good way is the way you outlined for run-time stack checking.  Maybe
some CPUs feature some sort of built-in high water mark stack pointer
register.  That would be nice but I don't know if it exists.

JJS

```
```"ssubbarayan" <ssubba@gmail.com> writes:

> Gurus,
> I was just wondering what could be the best possible way to calculate
> free memory while our code is running in SDRAM.I have used vxworks and
> it uses a approach to calculate the free memory as follows:
> Fill up the entire stack with 0xeeeee and then when you start using the
> stack,if at all a particular address in memory is used,the value of
> 0xeeeee in that location should be overwritten with real data.So if we
> are able to track the first occurance of the pattern 0xeeeee we should
> be able to subtract it from the entire size of memory and make a fair
> assesment of the available free memory.
>
> How ever I beleive such a algorithm has drawbacks.First drawback which
> I can think is
> 1)What is the guarentee that the real data is not ur pattern chosen(In
> vxworks case,its 0xeeeee,if 0xeeeee itself is real data,we cant find
> out real size!)?
>
> 2)What should you look for to identify unique pattern?Or other way how
> would you arrive at the pattern word to be used?

<SNIP>

--

John Devereux
```
```John Devereux <jdREMOVE@THISdevereux.me.uk> writes:

> "ssubbarayan" <ssubba@gmail.com> writes:
>
> > Gurus,
> > I was just wondering what could be the best possible way to calculate
> > free memory while our code is running in SDRAM.I have used vxworks and
> > it uses a approach to calculate the free memory as follows:
> > Fill up the entire stack with 0xeeeee and then when you start using the
> > stack,if at all a particular address in memory is used,the value of
> > 0xeeeee in that location should be overwritten with real data.So if we
> > are able to track the first occurance of the pattern 0xeeeee we should
> > be able to subtract it from the entire size of memory and make a fair
> > assesment of the available free memory.
> >
> > How ever I beleive such a algorithm has drawbacks.First drawback which
> > I can think is
> > 1)What is the guarentee that the real data is not ur pattern chosen(In
> > vxworks case,its 0xeeeee,if 0xeeeee itself is real data,we cant find
> > out real size!)?
> >
> > 2)What should you look for to identify unique pattern?Or other way how
> > would you arrive at the pattern word to be used?
>
> <SNIP>
>

Which has the same problem.  But really, if you pick a pattern that's
unlikely to turn up by coincidence, you'll come close enough to the
right answer for all practical purposes.  Don't use, say, 00000000,
but beyond that it doesn't much matter.
--
Joseph J. Pfeiffer, Jr., Ph.D.       Phone -- (505) 646-1605
Department of Computer Science       FAX   -- (505) 646-1002
New Mexico State University          http://www.cs.nmsu.edu/~pfeiffer
skype:  jjpfeifferjr
```
```"John Devereux" <jdREMOVE@THISdevereux.me.uk> wrote in message
news:87d5mtb06d.fsf@cordelia.devereux.me.uk...
> "ssubbarayan" <ssubba@gmail.com> writes:
>
> > Gurus,
> > I was just wondering what could be the best possible way to calculate
> > free memory while our code is running in SDRAM.I have used vxworks and
> > it uses a approach to calculate the free memory as follows:
> > Fill up the entire stack with 0xeeeee and then when you start using the
> > stack,if at all a particular address in memory is used,the value of
> > 0xeeeee in that location should be overwritten with real data.So if we
> > are able to track the first occurance of the pattern 0xeeeee we should
> > be able to subtract it from the entire size of memory and make a fair
> > assesment of the available free memory.
> >
> > How ever I beleive such a algorithm has drawbacks.First drawback which
> > I can think is
> > 1)What is the guarentee that the real data is not ur pattern chosen(In
> > vxworks case,its 0xeeeee,if 0xeeeee itself is real data,we cant find
> > out real size!)?
> >
> > 2)What should you look for to identify unique pattern?Or other way how
> > would you arrive at the pattern word to be used?
>
> <SNIP>
>
>

There is also a slight advantage to using this value because it is odd. On
many processors using an odd address for larger than byte size data will
thow an exception. As a result uninitialised data on the stack used as
pointers can be caught more often.

The bigest problem I have though is that I am a vegetarian so I prefer
0xFACEFEED.

Peter

```
```"John Devereux" <jdREMOVE@THISdevereux.me.uk> wrote in message
news:87d5mtb06d.fsf@cordelia.devereux.me.uk...
> "ssubbarayan" <ssubba@gmail.com> writes:
>
>> Gurus,
>> I was just wondering what could be the best possible way to calculate
>> free memory while our code is running in SDRAM.I have used vxworks and
>> it uses a approach to calculate the free memory as follows:
>> Fill up the entire stack with 0xeeeee and then when you start using the
>> stack,if at all a particular address in memory is used,the value of
>> 0xeeeee in that location should be overwritten with real data.So if we
>> are able to track the first occurance of the pattern 0xeeeee we should
>> be able to subtract it from the entire size of memory and make a fair
>> assesment of the available free memory.
>>
>> How ever I beleive such a algorithm has drawbacks.First drawback which
>> I can think is
>> 1)What is the guarentee that the real data is not ur pattern chosen(In
>> vxworks case,its 0xeeeee,if 0xeeeee itself is real data,we cant find
>> out real size!)?
>>
>> 2)What should you look for to identify unique pattern?Or other way how
>> would you arrive at the pattern word to be used?
>
> <SNIP>
>

Seen in various 16 bit codes:
#define STACKPAT 0x55aa        // Stack fill value for high water mark
checking.

--

... Hank

```
```"ssubbarayan" <ssubba@gmail.com> wrote in message
> Gurus,
> I was just wondering what could be the best possible way to calculate
> free memory while our code is running in SDRAM.

<snip>

> 4)We are making software for a consumer electronics appliance.It uses a
> premitive propreitory RTOS which does not give enough tools to find out
> free memory.We are in a forced situation to reduce memory
> consumption,so we have to get a way to calculate free available memory
> first.To add to the complexity,our application uses
> heap,partitions,partition with in partitions.At any point of time we
> have to give the user memory utilised like most RTOS tools
> provide.Since Heap is dynamic how do u arrive at a fair judgment?

<pedant_mode=ON>

If you want your appliance to be totally reliable, avoid using a heap at
all. Free memory calculation then becomes rather simple [1]. Logic: heaps
are (usually) non-deterministic and *will* cause eventual malloc failures
due to fragmentation [2].

[1] Excluding stack use, but that's not too hard either.
[2] Unless you allocate all memory at startup; a common trick. But then the

<pedant_mode=OFF>

Steve
http://www.fivetrees.com

```
```In article <T7adnWGND7X4PKLeRVnyiw@pipex.net>,
Steve at fivetrees <steve@NOSPAMTAfivetrees.com> wrote:
>
><pedant_mode=ON>
>
>If you want your appliance to be totally reliable, avoid using a heap at
>all. Free memory calculation then becomes rather simple [1]. Logic: heaps
>are (usually) non-deterministic and *will* cause eventual malloc failures
>due to fragmentation [2].
>
>[1] Excluding stack use, but that's not too hard either.
>[2] Unless you allocate all memory at startup; a common trick. But then the

< extreme_pedant_mode=ON >

Not so.  In Fortran 77, it was possible to allocate all memory statically,
but it isn't generally possible.  It can't be done in Fortran 90 or C.

Also, of the memory management schemes that are lumped under the term of
'heap' ones, there are several that have deterministic behaviour and do
not suffer from fragmentation.  The simplest one is if all allocations
are of the same size, and chains are used instead of arrays.

The whole area of memory management schemes is sadly neglected, and
much of garbage collection work is based on unrealistic assumptions,
so much of what is currently believed isn't so.

< extreme_pedant_mode=OFF >

><pedant_mode=OFF>

Regards,
Nick Maclaren.
```
```"Nick Maclaren" <nmm1@cus.cam.ac.uk> wrote in message
news:dhobjl\$e2v\$1@gemini.csx.cam.ac.uk...
> < extreme_pedant_mode=ON >
>
> Not so.  In Fortran 77, it was possible to allocate all memory statically,
> but it isn't generally possible.  It can't be done in Fortran 90 or C.

<even_more_extreme_pedant_mode=ON>

It *can* be done in C - by avoiding malloc ;).

<even_more_extreme_pedant_mode=OFF>

> Also, of the memory management schemes that are lumped under the term of
> 'heap' ones, there are several that have deterministic behaviour and do
> not suffer from fragmentation.  The simplest one is if all allocations
> are of the same size, and chains are used instead of arrays.

Agreed. I've used techniques such as these (effectively an application-level
memory manager) in certain situations where extreme reliability was
required, but the platform insisted on the use of its memory manager...
<sigh>...

> The whole area of memory management schemes is sadly neglected, and
> much of garbage collection work is based on unrealistic assumptions,
> so much of what is currently believed isn't so.

Completely agreed. I've often fantasized about a hardware memory manager
that remaps blocks in such a way that fragmentation cannot occur. OTOH, I've
also fantasized that one day, a majority will write good code that doesn't
leak memory and traps malloc errors properly...

Steve
http://www.fivetrees.com

```
```Nick Maclaren wrote:
> Also, of the memory management schemes that are lumped under the term of
> 'heap' ones, there are several that have deterministic behaviour and do
> not suffer from fragmentation.  The simplest one is if all allocations
> are of the same size, and chains are used instead of arrays.
>
> The whole area of memory management schemes is sadly neglected, and
> much of garbage collection work is based on unrealistic assumptions,
> so much of what is currently believed isn't so.

That's a worthlessly vague statement. As far as I can see, the problem isn't
lack of research; it's lack of application of that research in the most
commonly used systems.

--
David Hopwood <david.nospam.hopwood@blueyonder.co.uk>
```