Skip to content

WA5. Memory Management

Statement

  • Scenario: You are helping a colleague who is a junior programmer in your firm.
  • He just started programming in the real-world and has difficulty understanding how memory is managed in his computer system in regard to stacks and heaps.
  • Draw out a diagram showing the memory process explaining how memory and virtual memory addressing work.

Answer

  • The question is so big that it is not possible to answer without hours and days of research. However, I will try to answer it as best as I can with close proximity to the real answer.
  • Below is a diagram of the memory process explaining how memory and virtual memory addressing work.
  • I will explain the diagram in text as well.

Memory Process

  • When a program is set to be executed, a corresponding process is loaded into memory.
  • A chunk of memory is allocated for the process, OS keeps track of the minimum and maximum address of the process. This is called the address space.
  • The process is loaded into its address space, and this space is divided into three parts: stack and heap, and code.
  • Code is where the actual program is stored.
  • Stack is where the program stores its local variables and function calls.
  • Heap is where the program stores its global variables and dynamically allocated memory.
  • There is still a part of the address space that is not used, this is called free space.
  • Stack and heap are both growing in nature, meaning that they grow as the program runs.
  • Stack grows downwards, and heap grows upwards, until they meet each other.
  • When they meet each other, the process is said to have stack overflow, and the process has used up all the memory it can use, and the process will crash.
  • To prevent this, the OS will allocate more memory to the process, and this is called virtual memory or secondary memory in the diagram.
  • The OS will allocate a chunk of the secondary memory to this process, and move some of its stack and heap to the secondary memory.
  • The OS will keep track of the minimum and maximum address of the secondary memory, and this is called the virtual address space.
  • When the process needs to access a variable thats located outside of its main address space, the OS will translate the address to the virtual address space, and the process will access the variable in the secondary memory.
  • There are multiple techniques to organize moving data between the main memory and the secondary memory, some of them include paging, segmentation, Swapping, and Fragmentation.
  • Secondary memory may be a hard disk, a cloud storage, or even another memory chip.
  • Depending on the technique used, there may be a few ways to organize addressing in the main and secondary memory, but the combined address space of the process includes both the main and secondary memory. OS keeps track of boundaries of all pieces of memory allocated to the process.

References

  • Arpaci-Dusseau, R. H., & Arpaci-Dusseau, A. C. (2018). Operating systems: three easy pieces (1.01 ed.). Arpaci-Dusseau Books. Retrieved June 16, 2022, from https://pages.cs.wisc.edu/~remzi/OSTEP/