The address space is an abstraction of physical memory. This means a program views its available memory as a huge contiguous memory as that is often more significant than available physical memory. The address space contains all memory states: code, stack, and heap.
Code: The program's code lives inside the memory and address space—this stores instructions relating to the program.
Stack: This keeps track of where the code is during its calls. It also allocates local variables and passes and returns values from functions.
Heap: This is used for dynamically allocating and managing user memory, such as the malloc()
and free()
calls in C. It also initializes static variables.
The size of an address space is determined by the number of unique addresses that can be generated. We can calculate this by checking our system's bit size. For example, a 32-bit operating system has
Let us take a deeper dive into what address spaces are.
The diagram above shows that the first 1KB is filled with code data. This is static and easy to place in memory. Therefore, we place it at the top as we know we will not anymore.
After that, we divide the remaining address space into two portions that can increase or decrease in size. The first is our heap which we will place after our code, and stack, which we will place at the very end. We configure these arrangements as it gives us the most space for either portion to grow. If we were to assign a stack or heap a value in the middle, that would cap both portions to a specific range. This arrangement allows for maximum growth for both portions as needed. The heap increases when a user requests more memory, while the stack increases when a user makes a procedure call.
Note: It is only a convention to place the heap and the stack on the top and the bottom respectively.
Moreover, it is essential to remember that what we describe as the address space is just an abstraction of what the operating system provides us. It is not the real memory location, i.e., it does not start at 0 KB and end at 16 KB, but it is some arbitrary physical addresses.
Address spaces are essential for modern operating systems. Let us discuss what they provide and why it is beneficial for us.
Virtual memory: This address space enables us to create and use virtual memory. Virtual memory is when the running program thinks it is loaded into memory at a particular address, for example, 0KB. It also thinks that it has a potentially large address space, so it can grow as much as it wants. This is quite different from reality.
Memory isolation: Address spaces allow each process to have its virtual memory. This provides isolation from other processes and prevents communication or modification between processes.
Protection: As we just read, address spaces provide isolation. This enables operating systems to enable memory protection mechanisms. Moreover, the process cannot access memory locations outside their allocated spaces. This further creates a barrier between processes and their memories, protecting our programs.
Memory management: Due to address spaces, the operating system can allocate and release memory from processes as needed. This creates a dynamic allocation of memory that is faster and more memory efficient.
In our modern computer systems, we must address spaces to utilize our memory resources while providing efficient memory protection. This allows us to run multiple processes concurrently without having memory issues and them interfering with each other. This enhances our operating system's security, performance, isolation, and stability.
What is the primary purpose of an address space?
To determine the size of the RAM in a computer system
To allow processes to access secondary storage devices
To enable memory management and provide isolation for processes
To define the size of the data that can be stored in memory