Re: memory management related system calls

From: Kasper Dupont (kasperd_at_daimi.au.dk)
Date: 09/04/03


Date: Thu, 04 Sep 2003 08:33:46 +0200

VINAY wrote:
>
> Hi all,
> linux kernel gives handle only to alter the heap size ( brk()
> syscall).
> What if I want to deallocate a chunk of memory within a heap ( i.e not
> on the boundaries of heap). Is there any syscall to do this ?

It is possible to use munmap, but I would strongly advice against it.
In particular if you intend to use brk again at a later time, it is
probably not a good idea to mess with it's allocations. Instead you
should use mmap to allocate rather than brk. There is a reason brk
is not in the posix standard.

> How does free () in glibc manage this.

That depends on how the memory was allocated. It can either allocate
using brk or mmap. For small allocations it will primarily use brk
and for large allocations it will use mmap. If it was allocated
using brk, free will put it on a free list for use by later calls of
malloc in the same process. It is never returned to the OS. In case
it was allocated with mmap it can be freed again using munmap. I
don't however know whether free actually does free the memory, or
whether it is put on the free list. You can find out by allocating
a large (a few MB) chunk, free it again, and look on the maps.

-- 
Kasper Dupont -- der bruger for meget tid paa usenet.
For sending spam use mailto:aaarep@daimi.au.dk
Their business was zero and it was shrinking.


Relevant Pages

  • Re: dont let mmap allocate down to zero
    ... > use mmap for small allocations? ... A returned value of 0 is perfectly correct for mmap() ... The seg-fault you get when you de-reference a pointer to NULL ... You are attempting to access memory ...
    (Linux-Kernel)
  • malloc, mmap, and unexec issue
    ... I have a problem regarding mallocusing mmap() for large memory ... allocations. ... Arguments can be passed to 'foo' to change ...
    (comp.os.linux.development.system)
  • Re: memory management related system calls
    ... For small allocations it will primarily use brk ... > and for large allocations it will use mmap. ... happens to be at the top of memory and is reasonably large. ...
    (comp.os.linux.development.system)
  • Re: Heavy malloc()/free() load and threads
    ... Memory allocations which originally happened by calling brk/ ... Switched to getting memory from mmap (or mmap-like things ... threshold seemed to depend a lot on the way the product was used; ...
    (comp.unix.programmer)
  • RE: 2003 SBS stalling randomly
    ... A memory leak occurs in an application using the Volume Shadow Copy Service ... Poolmon displays data that the ... The data is grouped by pool allocation tag. ... Press P twice to display allocations from only the paged pool. ...
    (microsoft.public.windows.server.sbs)