How are processes' memory stored in RAM?

  • Thread starter jkok5657
  • Start date
  • Tags
    Memory Ram
In summary, the memory for a single program is represented as follows: Where memory reserved for stack, data, bss and code segment are fixed, but heap can grow indefinitely.
  • #1
jkok5657
3
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
  • #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?
 
  • #4
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:
  • #5
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.
 
  • #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)?
 
  • #7
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:
  • #8
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.
 
  • #9
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.
 

Related to How are processes' memory stored in RAM?

1. How is data stored in RAM?

Data is stored in RAM (Random Access Memory) through a process called "writing". This involves the CPU (Central Processing Unit) sending signals to the RAM to store data in specific locations called memory cells. Each memory cell can store a certain amount of data, and these cells are organized into rows and columns to form a grid-like structure.

2. What types of data can be stored in RAM?

RAM can store any type of data, including text, numbers, images, and program instructions. However, the data must be in binary form, which means it is converted into 0s and 1s before being stored in the memory cells.

3. How is data retrieved from RAM?

When the CPU needs to access data from RAM, it sends a request to read the data from specific memory cells. The data is then transferred back to the CPU through the memory bus, which is a high-speed data path between the CPU and RAM.

4. Can data be stored permanently in RAM?

No, data cannot be stored permanently in RAM. Unlike a hard drive or solid-state drive, RAM is a volatile memory, which means it only holds data temporarily. When the computer is turned off, the data in RAM is lost.

5. How much data can be stored in RAM?

The amount of data that can be stored in RAM depends on the size and type of the RAM modules installed in a computer. Most modern computers have at least 4GB of RAM, but high-end systems can have up to 128GB or more.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
374
  • Programming and Computer Science
Replies
18
Views
5K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
3K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
5K
Back
Top