[uClinux-dev] Reclaiming Memory

Gavin Lambert gavinl at compacsort.com
Thu Nov 1 19:43:20 EST 2007


Quoth Harry Gunnarsson:
> So here's the problem, if I run this download-uncompress routine 
> back-to-back, it always works fine the first time, but it could 
> bail on the second/third/fourth time due to allocation failure 
> on one of the big buffers I need. I never figured out why this 
> is and I haven't figured out how to circumvent this. Since I 
> have returned the buffers with free(), the system should be 
> able to hand them out again in the next malloc() call, right? 

How much "spare" RAM do you have?  Since the 5272 doesn't have an MMU, any
large memory requests must be satisfied by a single contiguous block of
memory.  If the block that you got last time has been broken up into smaller
blocks (for smaller allocations) in the meantime then it will no longer be
available for your big allocation (and due to memory fragmentation, may
never be again).

If you're trying to allocate a significant fraction of your total RAM in a
large chunk then your best bet is to allocate it once at program startup and
keep it around the whole time.  That way you're guaranteed that it'll always
be available.  (Assuming, of course, that you know in advance how much room
you'll need and that other processes won't start having RAM starvation from
this.)





More information about the uClinux-dev mailing list