[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory size and allocation

Francesco Orsenigo wrote:

> > I think that if you truly expect your program to *NEED* to do this (very
> > few do), then you should write your own memory allocator.
> How can i know if i *NEED* to do this?
> I've evaluated that the program may reach a peak of 80 Mb of RAM usage.
> Maybe i should finish it to get a better idea of the requirements?

Well, not knowing what you are doing makes it difficult to advise - but
since you seem to have some kind of a situation where you have lots of
stuff in memory that you know you *could* free up - but for some reason
do not wish to unless you absolutely *have* to.

So I deduce that this is some kind of a cache for data that you've
created at some expense, may need later but don't need *know*.

I don't think that you should be trying to consume *all* of memory before you
start giving it back. That sort of thing is pretty antisocial in a multi-tasking
system...what happens if X-windows needs some memory when your program
has it *all*?  Also, Linux has virtual memory - which means that before
the operating system finally admits that there isn't a single byte left
anywhere, it'll be tossing chunks of your program out to disk into the
swap partition.

Also, Linux uses free RAM for disk caches and such - so other system
activities slow down significantly when memory starts to get full.

OK - so what *should* you do?

I think you should pick an amount of memory that your program would
*like* to have - and try to allocate that as one huge chunk at the
start of the run.  Tell your users that this amount (plus - let's
say 16Mb for safety) is the minimum memory requirement for the

If you succeed in allocating that amount then do all your internal
memory allocations out of that block.  If you don't succeed then
either punt with a nice friendly "Insufficient Memory" error - OR
have your program reduce it's expectations of the amount of cache
it can use - and try to allocate that new, smaller, amount.

Keep reducing the amount you try to grab until either you get it
or there simply isn't enough for even the most basic operations.

There was a good article about this in the Feb 2002 edition of
Game Developer magazine.  Dunno if there is an online version
of that though.

----------------------------- Steve Baker -------------------------------
Mail : <sjbaker1@airmail.net>   WorkMail: <sjbaker@link.com>
URLs : http://www.sjbaker.org
       http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net
       http://prettypoly.sf.net http://freeglut.sf.net
       http://toobular.sf.net   http://lodestone.sf.net