Re: Understanding Linux addr space, malloc, and heap

From: Arjan van de Ven (arjan_at_infradead.org)
Date: 10/21/05

  • Next message: Steven Rostedt: "Re: question about code from the linux kernel development ( se ) book"
    To: "Vincent W. Freeh" <vin@csc.ncsu.edu>
    Date:	Fri, 21 Oct 2005 15:00:02 +0200
    
    

    On Fri, 2005-10-21 at 08:46 -0400, Vincent W. Freeh wrote:
    > I am trying to understand the Linux addr space. I figured someone might
    > be able to shed some light on it. Or at least point me to some sources
    > that will help.
    >
    > I don't understand what is happening with malloc and the heap in my
    > process. According to /proc/<pid>/maps the memory from heap to stack
    > initially looks like that. I only show the four "maps" from the heap
    > and above. (This is a slightly altered form consisting of start_addr,
    > end_addr, size_in_pgs, permissions, and path_if_one):
    >
    > 0x08d42000 - 0x08d63000 (33 pgs) rw-p path `[heap]'
    > 0xb7ef8000 - 0xb7ef9000 (1 pgs) rw-p
    > 0xb7f09000 - 0xb7f0b000 (2 pgs) rw-p
    > 0xbfaf5000 - 0xbfb0b000 (22 pgs) rw-p path `[stack]'
    >
    > First, please fix any erroneous statements/assumptions above. Next I
    > have many questions. A few follow.
    >
    > * How does the heap work? I learned/teach that heap is a contiguous
    > chunk of memory that holds dynamically-allocated memory. Doesn't appear
    > to be the case.

    that's the old school 1970's stuff

    the "heap" is still brk in linux, however there is no 1:1 relation
    between heap and malloc. malloc in glibc is implemented both using brk
    and mmap, depending on the size of your allocation.

    >
    > * Man pg says can only mprotect mmap-able pages. But what are these?
    > How can I tell?

    you need to mmap these yourself to be sure.. eg you cannot mprotect the
    output of malloc, at least not reliably. Only of mmap.

    >
    > * Why does mprotect silently fail?

    no it has sideeffects; eg it most likely affects more memory than just
    your malloc()'d part

    > * I thought brk indicated the top of the heap and that all dynamic
    > memory would be between bss end and brk. That's not true. What is brk
    > for then?

    see definition of heap vs malloc above

    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at http://www.tux.org/lkml/


  • Next message: Steven Rostedt: "Re: question about code from the linux kernel development ( se ) book"

    Relevant Pages

    • Re: Whats the substitution of brk system call in POSIX standard?
      ... malloc() and mmap, I guess. ... imagine them being portable to OSes with different memory layouts. ... mallocis a library routine and is originally implemented with brk() ... By allocating from a continous ...
      (comp.unix.programmer)
    • malloc realloc and pointers
      ... When we perform malloc(), the memory allocated dynamically comes from ... the heap area of the process in concern. ... perticular position in a buffer then on performing a ...
      (comp.lang.c)
    • Re: stack and heap
      ... books explain that local stack variables for each function are automatically allocated when function starts and deallocated when it exits. ... malloc() always takes memory in the heap. ...
      (comp.programming)
    • malloc and realloc
      ... When we perform malloc(), the memory allocated dynamically comes ... from the heap area of the process in concern. ... perticular position in a buffer then on performing a ...
      (comp.lang.c)
    • Re: Can Anyone Explain This Memory Leak?
      ... and if I remove the guard the ram doesn't do this. ... when the threads can be created and die quickly the maximum memory used by ... puts "after malloc" ... using the brk() or sbrksystem calls. ...
      (comp.lang.ruby)