Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How are processes' memory stored in RAM?

  1. Sep 16, 2015 #1
    How is memory of programs stored in RAM? The memory for a single program is represented:

    guuQTIn.jpg

    Where memory reserved for stack, data , bss and code segment are fixed, but heap can grow indefinitely. Is the free memory between stack and heap all of a computer available RAM? So where is the memory for second program stored?
     
    Last edited: Sep 16, 2015
  2. jcsd
  3. Sep 16, 2015 #2

    jedishrfu

    Staff: Mentor

  4. Sep 16, 2015 #3
    But how in this case heap can grow indefinitely ? If processes are in different segments like that:
    8CNTOc1.jpg
    How large is free memory between stack and heap for each seegment ? What happens when heap for 1 process needs more than that?
     
  5. Sep 16, 2015 #4

    jedishrfu

    Staff: Mentor

    The heap could be placed in a different segment from the stack and from the program code itself. In the realm of PC-DOS programs, it wasn't uncommon for a programs heap to overrun or be overrun the stack. Usually there were checks to prevent either one from stomping on the other.

    In a multitasking system, your map would be considered a logical map not the way the memory is actually allocated and placed in system memory.
     
    Last edited: Sep 16, 2015
  6. Sep 16, 2015 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The above image depicts the logical address space in a single thread application. (Note: multi-threaded applications share a common heap, bss, data, and code segments, but each thread has its own stack.) This is not a picture of how physical memory looks. The logical address space of an application (or thread) is partitioned into fixed sized blocks. How this is done and what these are named varies from operating system to operating system. Oftentimes, they're called "pages". The computer maintains a concept of how pages in a program's logical address space map to physical memory or disk (virtual memory).

    Accessing physical memory is very slow compared to CPU speeds. Modern CPUs have multiple levels of cache memory. Each cache level has a mapping, similar to the mapping from program logical address space to physical address space. That cache map maps chunks of that cache to chunks of higher level cache, or to physical memory in the case of the highest level cache. The block sizes in cache memory are generally smaller than the page sizes used to partition a program's logical address space.
     
  7. Sep 16, 2015 #6
    Thx for answers. I am not sure if I get it right but in this logical address space how large is free memory size between stack and heap, is there something that determines that? When heap will grow into stack (inside that logical address space) or reach check, what happens then (Is the new larger logical address space created or something)?
     
  8. Sep 16, 2015 #7
    Yes, a single command in the beginning of the program tells the computer the type of memory model to use for your program. You can only specify it yourself in the compiler options or by using assembly.
    Mod note: changed "quote" tags to "code" tags
    Code (Text):

    .model tiny       ;Use the smallest stack possible
    section .text     ;declare section
    global _start     ;must be declared for linker (ld)

    _start:               ;tell linker entry point

    mov edx,len     ;message length
    mov ecx,msg    ;message to write
    mov ebx,1        ;file descriptor (stdout)
    mov eax,4        ;system call number (sys_write)
    int 0x80           ;call kernel

    mov eax,1        ;system call number (sys_exit)
    int 0x80           ;call kernel

    section .data

    msg db 'Hello, world!',0xa   ;hello world
    len equ $ - msg                    ;length of our string
     
     
    Last edited by a moderator: Sep 16, 2015
  9. Sep 16, 2015 #8

    jim mcnamara

    User Avatar

    Staff: Mentor

    Note: When the process has the cpu the memory model includes the kernel memory objects. EX: it may include other things like a process specific kernel stack.

    For example, I/O --
    like writing characters on the screen or writing to a file or reading from a file --
    requires kernel mode routines. See the "system" and "kernel" comments in newjerseyrunners code example.

    The process model you show is user mode memory.
     
  10. Sep 16, 2015 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    This is bad. Very bad. Quoting (out of order) from the movie "Ghostbusters", with slight modifications,
    In short, you don't want to cross the streams and make the stack and heap collide. When you do cross the streams, it's bad. Very bad.


    On most modern computers, the heap management functions know the location of the bottom of the stack. If you try to allocate memory that would cross that boundary you will get nothing. The heap doesn't grow into the stack. The stack is simpler and more primitive. The stack can easily grow into the heap, at least conceptually. The result is the dogs and cats living together. Mass hysteria!

    To protect against crossing the streams, most modern computers limit the stack to a certain size. Exceeding this results in the dreaded stack overflow error. A core dump is better than corrupted memory.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: How are processes' memory stored in RAM?
Loading...