7 minutes read

We've all seen the dreaded error message: "Storage space full. Please delete files to create more space". But have you ever heard of an error saying the computer's main memory is full? To deal with this problem, our computers use various memory management techniques. One of which is virtual memory. Implemented since the 1960s in its truest form, virtual memory is currently used in all types of computer systems. In this topic, we are going to look at virtual memory and its advantages, how it is implemented using the concept of logical and physical addresses, and how programs are loaded into the main memory based on this concept.

Virtual memory

Virtual memory is a memory management technique where a section of the computer's secondary storage is used to emulate its main memory. This gives an illusion of larger main memory compared to what is physically present. Since the memory we have at hand might not be enough for all circumstances, we can use virtual memory to aid us.

Virtual memory has been used in nearly all types of computers as it provides significant advantages. The computer can allocate more memory than what is physically present. Because of this, we can run processes that require more memory than the total capacity of the main memory itself. Other advantages of using virtual memory are:

  • Only a part of the user program needs to be stored in the main memory for execution. The rest can be stored as virtual memory.

  • No longer constrained by limited memory capacity.

  • Allows for more efficient process creation.

  • More programs can run concurrently.

We can alleviate stressing the main memory by using virtual memory. This means more physical memory is free, which in turn allows us to load more data and run more programs. It is a cost-effective way of increasing performance by managing memory as compared to simply adding more memory. However, we should not rely solely on virtual memory as it is very slow when compared to the actual speed of the main memory. This is because the speed of accessing data stored as virtual memory is limited by the speed of the secondary storage in use.

Now let's look at how virtual memory can be implemented.

Logical address and physical address

Virtual memory makes uses two types of addresses to reference a memory space. "What is an address?" you might ask. When we want to know where someone lives or where a building is located, we think of its address. A similar thing exists for memory also. Locations in the memory component where blocks of data reside are known as memory addresses.

These two types are physical addresses and logical addresses. The memory addresses concerning the physical/real addresses in a memory component are called physical addresses. Whereas the ones generated by the CPU are known as logical addresses or virtual addresses. A logical address is a reference to a physical address. Meaning that a logical address points to a unique physical address. As the name might suggest, virtual addresses are the centerpiece of implementing virtual memory.

Dividing memory into logical and physical addresses makes implementing virtual memory easier. Processes can be stored physically in the secondary memory, but we can reference those addresses logically as if they were the computer's main memory. The operating system assigns a logical/virtual address to a physical address. But when we need to access them, the computer hardware; specifically the Memory Management Unit or in short MMU, is used to translate those logical addresses back to physical addresses.

virtual memory blurring the line between different types of physical memory

Using virtual memory blurs the line between what we should actually consider as the computer's main memory. So, we have two ways of measuring memory consumption.

Resident Set Size and Virtual Memory Size

How much memory space does a process require? Let's assume we need to run a program larger than the main memory's size. If we have 4 GBs of main memory, then maybe 3 GBs. The rest will be stored as virtual memory in the secondary storage. If we have 8 GBs, then certainly more than 3 GBs. Then how do you measure how much space a process might use? How much of it is actually necessary for smooth functionality?

Resident Set Size or RSS is the amount of memory a process uses in the computer's main memory. This means all the memory used by a process that is stored as virtual memory in the secondary storage is not taken into consideration.

On the other hand, Virtual Memory Size or VSZ is the total amount of memory allocated for a process. This includes memory physically present inside the main memory, memory that is stored in the form of virtual memory, memory that is allocated for a process but not used, and memory shared between multiple processes.

The major difference between these two is that VSZ takes into account part of a process that is stored separately as virtual memory. We will now take a look at how processes are loaded into the memory with this in mind.

Static loading and dynamic loading

Let's say we want to create a simple program on our computer. For example; a C program that prints hello world!, we compile the program to execute it. All the code in our program and the required data are loaded into the main memory and are referenced via their respective logical addresses. But if we were to load large applications like a video games. Are the program and all of its data completely loaded into the memory? Certainly not.

The first scenario of loading a simple program is an example of static loading. Static loading is the process of copying the entire program and data into a fixed memory address in the main memory. Whereas the second scenario, where we have to take a huge amount of data into consideration is an example of dynamic loading. In dynamic loading, parts of programs/modules are dynamically loaded into the main memory i.e. only the necessary data are loaded into the memory, while others are loaded when they are called. This means that programs that use static loading will have the same amount of RSS and VSZ. But those using dynamic loading will have smaller RSS compared to VSZ.

The need for dynamic loading arises when there is limited physical memory, but processes require huge amounts of data to work with. Since unused memory addresses are not loaded, we save a lot of time and memory space. And this free memory space can be used by other processes.

Conclusion

Our discussion regarding virtual memory can be summarized in the following points:

  • Virtual memory is a memory management technique that uses a section of the computer's storage as main memory, which allows the computer to allocate more memory than is physically present.

  • We use virtual addresses and physical addresses to implement virtual memory.

  • A virtual/logical address is a reference to its respective physical address.

  • RSS is the amount of memory a process uses in the computer's main memory whereas VSZ is the total amount of memory allocated for a process.

  • While implementing virtual memory, programs are dynamically loaded into the main memory. All the necessary physical addresses are referenced via their logical addresses. But only the necessary data are loaded into the main memory, while others are loaded when necessary.

15 learners liked this piece of theory. 1 didn't like it. What about you?
Report a typo