Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack and Heap locations in RAM

What happens in the middle? This image and others like it have been bothering me for a while now. When I use malloc, this should be a part of the dynamic data, the heap. However, this seems to be bounded from above by the stack, which seems very ineffective. A program cannot predict how much memory I plan on allocating, so how does the program judge how far up to put the stack? It seems as though all of the memory in the middle is wasted, and I would like to know how this works for programs that could potentially range from a small service program that doesn't use any dynamic memory verses a videogame that could potentially allocate huge sections of memory.

Say, for example, I open up Microsoft paint. If I paste a high resolution picture into it, the memory allocation of paint skyrockets. Where was this memory taken from? What I would truly like is a snapshot of my entire RAM stick labelled as above to visualize how the many programs of a computer partition the computer's memory as a whole, but I can only find diagrams like this one for a single process and a single section of RAM.

like image 390
Nicholas Pipitone Avatar asked Sep 05 '15 23:09

Nicholas Pipitone


2 Answers

Your picture is not of the RAM, but of the address space of some process in virtual memory. The kernel configures the MMU to manage virtual memory, provide the virtual address space of a process, and do some paging, and manage the page cache.

BTW, it is not the compiler which grows the stack (so your picture is wrong). The compiler is generating machine code which may push or pop things on the call stack. For malloc allocated heap, the C standard library implementation contains the malloc function, above operating system primitives or system calls allocating pages of virtual memory (e.g. mmap(2) on Linux).

On Linux, a process can ask its address space to be changed with mmap(2) -and munmap and mprotect. When a program is started with execve(2) the kernel is setting its initial address space. See also /proc/ (see proc(5) and try cat /proc/$$/maps....). BTW mmap is often used to implement malloc(3) and dlopen(3) -runtime loading of plugins, both heavily used in the RefPerSys project (it is a free software artificial intelligence project for Linux).

Since most Linux systems are open source, I suggest you to dive into implementation details by downloading then looking inside the source code of GNU libc or musl-libc: both implement malloc and dlopen above mmap and other syscalls(2).

Windows has similar facilities, but I don't know Windows. Refer to the documentation of the WinAPI

Read also Operating Systems: Three Easy Pieces, and, if you code in C, some good book such as Modern C and some C reference website. Be sure to read the documentation of your C compiler (e.g. GCC). See also the OSDEV website.

Be aware that modern C compilers are permitted to make extensive optimizations. Read what every C programmer should know about undefined behavior. See also this draft report.

like image 196
Basile Starynkevitch Avatar answered Sep 18 '22 09:09

Basile Starynkevitch


In modern systems, a technique called virtual memory is used to give the program its own memory space. Heap and stack locations are at specific locations in virtual memory. The kernel then takes care of mapping memory locations between physical memory and virtual memory. Your program may have a chunk of memory allocated at 0x80000000, but that chunk of memory might be stored in location 0x49BA5400. Your actual RAM stick would be a jumbled mess of chunks of all of those sections in seemingly random locations.

like image 45
Vincent Damewood Avatar answered Sep 22 '22 09:09

Vincent Damewood