Reputation: 64205
When talking about a process' memory, I heard about things like code memory and data memory.
And for the data memory, there are 2 major managing mechanisms, stack and heap.
I am now wondering how is the code memory managed? And who manages it?
Pardon me if my statement is not so clear.
Thanks.
Upvotes: 13
Views: 733
Reputation: 4484
I recommend http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory (and the other memory related articles) if you're interested in finding out more about the details of process' memory management.
code memory = Text segment
Notice how the address space is 4GB. When the kernel creates a process it gives it virtual memory. Below is an example of a 32 bit OS. The kernel manages what addresses get mapped to actual RAM via the processor's MMU. So, the kernel and the MMU manage code memory, just as they manage the entire address space of a process.
(source: duartes.org)
Upvotes: 17
Reputation: 86651
The code memory doesn't need managing because it doesn't change. When an executable is loaded into the address space, the code is just read from the executable file into memory. In fact, on modern operating systems, the code segment is just memory mapped to the executable file.
Upvotes: 4
Reputation: 556
Your operating system provides so called system calls to dynamically allocate memory (malloc, free, etc.), it also provides the mechanism to load and execute your program.
When the program is loaded by the os the text segment (code memory) is set up and the statically allocated memory in your program is immediately available. As your code calls functions, the (statically allocated) variables in your functions are allocated on the stack and your dynamically allocated memory (using malloc() for example) is allocated on the heap. During the time your program runs, it is your (the programmer's) responsibility to manage the memory (lack of doing so will lead to memory leaks and will eventually cause a long running program to run out of memory and it will crash, or in extreme cases, depending on the OS, take the whole OS down with it).
See also this article: http://www.maxi-pedia.com/what+is+heap+and+stack
Upvotes: 0
Reputation: 21184
It is managed by the operation system. When a program is run, it's code is loaded from an executable file to some memory address. Depending on the nature of the program, some changes are applied to the code sections, e.g. jumps to dynamically linked libraries are resolved.
As proposed by Space_C0wb0y, check out en.wikipedia.org/wiki/Dynamic_linker for details on what is going on.
Upvotes: 2