How are processes' memory stored in RAM?

  • Thread starter Thread starter jkok5657
  • Start date Start date
  • Tags Tags
    Memory Ram
AI Thread Summary
Memory for programs in RAM is organized into segments, including stack, data, BSS, and code segments, while the heap can grow indefinitely. The free memory between the stack and heap is not all available RAM, as each program runs in its own segment managed by the operating system in a multitasking environment. The logical address space depicted in discussions represents a single-thread application, where memory allocation varies by operating system, often using pages. Accessing physical memory is slower than CPU operations, necessitating multiple cache levels that map to physical memory.When the heap grows and approaches the stack, it can lead to critical issues if they overlap. Modern systems prevent the heap from encroaching on the stack by managing memory boundaries, with the stack being limited in size to avoid stack overflow errors. If the heap were to grow into the stack, it could result in severe program failures, often humorously described as "mass hysteria." Proper memory management is crucial to ensure stability and prevent catastrophic failures in applications.
jkok5657
Messages
3
Reaction score
0
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:
Technology news on Phys.org
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?
 
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:
jkok5657 said:
guuQTIn.jpg

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.
 
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)?
 
jkok5657 said:
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)?
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:
.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:
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.
 
jkok5657 said:
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)?

This is bad. Very bad. Quoting (out of order) from the movie "Ghostbusters", with slight modifications,
Egon Spengler: There's something very important I forgot to tell you.
Peter Venkman: What?
Spengler: Don't cross the streams. The heap and the stack must not cross one another.
Venkman: Why?
Spengler: It would be bad.
Venkman: I'm fuzzy on the whole good/bad thing. What do you mean, "bad"?

Venkman: Do you mean that the program is headed for a disaster of biblical proportions?
Mayor: What do you mean, "biblical"?
Ray Stantz: What he means is Old Testament, Mr. Mayor. Real wrath-of-God type stuff!
Venkman: Exactly.
Stantz: Fire and brimstone coming down from the skies! Rivers and seas boiling!
Spengler: Forty years of darkness! Earthquakes, volcanoes!
Zeddemore: The dead rising from the grave!
Venkman: Human sacrifice! Dogs and cats living together! Mass hysteria!

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.
 
Back
Top