Memory layout of OS processes

memory

  • Text: The executable code (source code compiled to binary)
  • Data: initialized or uninitialized data
  • Heap: Dynamic allocated memory during program run time
  • Stack: Temporary data storage when invoking functions (local variables, function parameters, return addresses)

The sizes of text and data are fixed.

However, heap and stack can grow and shrink during run time. Each time a function is called, a record contains local variables, function parameters, return addresses is pushed to the stack. When the function finished, that record will be popped from the stack.

Similarly, the heap will grow as memory is dynamically allocated, and shrink when the memory is returned to the system. Notice that stack and heap size can grow toward each other but the OS has to make sure that they won’t overlap one another.