does malloc cause swapping out?

David Mandala plug-devel@lists.PLUG.phoenix.az.us
Fri Apr 12 15:20:02 2002


As I recall and it has been a while malloc on a linux box is optimistic,
that is it does not actually get the memory requested until you actually
attempt to use the memory, thus malloc on Linux will never return null
due to the system being out of memory.

>From the man page:

Linux  follows  an  optimistic memory allocation strategy.  This means
that when malloc() returns non-NULL there is no guarantee that the
memory really is available. In case it turns out that the system is out
of memory, one or more processes will be killed by the infamous OOM
killer.

kmalloc works differently if I recall, it actually gets memory.

Davidm

On Fri, 2002-04-12 at 14:16, Monika wrote:
> Hello,
> 
> I would like to know if malloc can cause pages to be swapped out.
> 
> Here's an extended version of my question:
> 
> This is what I was able to find out and what hopefully is correct:
> 
> Each process gets a chunk of memory to manage at the beginning of its
> execution. There is a code segment, heap space that grows "upwards" and stack
> that grows "downwards". A space between a stack and a heap is sort of a
> no-man's land that gets allocated with mmap as the executions progresses.
> 
> Malloc can raise up the heap and it is implemented by a syscall brk.
> If brk wants to raise the end of the heap, but there is no free space,
> because there is something in there that was allocated with mmap, then
> brk returns an old address of the end of the heap.
> 
> Now here's where my confusion starts. I did a little bit of function tracing
> (on 2.2.x kernel) and noticed that kmalloc can eventaully cause pages to be
> swapped out (if the region immediatelly above the heap is allocated and we
> need to increase the heap space). 
> 
> I wanted to check if malloc() can eventually cause pages to be swapped out
> similarly to what kernel malloc (kmalloc) can do.
> 
> I did a search on google and didn't find anything that would give me a good
> explanation, nor a definite answer. I looked at malloc.c files on my machine
> and ehm... that didn't work too well either and caused a bit more
> confusion on my part (probably due to my lack of good knowledge of C). 
> 
> What I'm confused about is when kmalloc is invoked vs. when malloc is invoked
> and also how exactly libc malloc calls syscall brk and how it manages the
> situation when brk returns the same address (meaning that heap space cannot be
> raised any higher).
> 
> I did look at the implementation of brk and it didn't seem to do any attempts
> to swap out pages.
> 
> I would appreciate any ideas/redirections/enlightening answers, even if they
> will make me feel as if I asked a dumb question ;-)
> 
> thanks,
> my ears are open,
> Monika
> 
> _______________________________________________
> PLUG-devel mailing list  -  PLUG-devel@lists.PLUG.phoenix.az.us
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-devel