Re: Memory management strategy



On 30 May 2005 07:09:41 -0700, ira2402@xxxxxxxxx wrote:

>Hi All,
>We are developing sw for a small embedded OS and we have limited
>memory.
>We are looking for algorithms, links, and articles about this.
>The goal is efficient utilization of small amount of memory - means -
>allocation for fixed length blocks / variable length blocks.
>Thanks.

Well, as far as C goes, I could think of a couple of tips :

- Instead of allocating memory for each small object, allocate a big
piece of memory and manage it yourself. Each individual allocation
comes with some overhead and functions like malloc and calloc might
have a minimum size they allocate (for example, if you allocate memory
for 2 bytes, it might turn out that 16 bytes were allocated because
allocation works with such a granularity). Also, allocating one big
block also reduces the ever present danger of memory leaks.

- Don't use speed optimizations of your compiler. These might involve
trading memory for speed (loop unrolling, for example).

- Use the minimum sized datatype you need. All too often, int's are
used automatically by the programmer where short's and char's could
have been used. You might even be able to pack some values with
bitfields. As far as struct's go : note that you might lose some extra
memory due to padding.

- Avoid recursive functions, if you can. Although recursive functions
often reduce code size, they might turn out to be very memory hungry.

- Don't pass big objects (like struct's) by value but by reference, if
you can.

- Using global variables might reduce memory usage (in passing less
parameters) in some cases but this is considered a bit ugly and is a
bit of a micro optimization :-)

- Use const where appropriate (especially for arrays).

Furthermore : you could think of compression of data and writing data
to some mass storage device (if your embedded system has one) if the
data isn't used for a long time.

Now (I just can't help myself ;-) if all else fails then you might
want to resort to Assembly which often allows for all kinds of
trickery that can even further reduce code size (one of the most
notorious methods was self modifying code, but your system must be
able to allow such a thing).

.



Relevant Pages

  • Re: How to release heap memory that is marked as free
    ... As I said, fragmentation is a very serious problem, and one of the most serious problems ... my allocator was accused of using massive amounts of memory. ... I'm going to have to re-think the memory allocation that I'm ... process's 'working set'. ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH 00/28] Swap over NFS -v16
    ... memory they can consume. ... So we need the extra (skb) ... included in the reserve? ... if the allocation had to dip into emergency reserves, ...
    (Linux-Kernel)
  • Re: Memory leak with CAsyncSocket::Create
    ... read my essay on how storage allocators work. ... Create method is consuming system memory that is not released back to ... The memory consumption is either shown as "Mem Usage" on the Task ... many levels of allocation going ...
    (microsoft.public.vc.mfc)
  • Re: OT: C++ overloading operators
    ... dynamic allocation, no matter how many "clever tricks" are used... ... though there's enough memory in the system, ... all these "flexible data types" map into CPU command ... The computing environment is completely ...
    (comp.dsp)
  • Re: CF Bug / Limitation in XMLTextWriter (OutOfMemory)
    ... Your desktop can page memory to and from disk as needed in order to fulfill ... and code for the fact that an allocation request ... I've been doing embedded system development for the better part of 10 ...
    (microsoft.public.dotnet.framework.compactframework)