Hi everyone, welcome back to SrishCodes where I teach you interesting tech concepts that you should know about as a current or aspiring software developer. Today I will be sharing with you about physical and virtual memory, as well as the differences between them.
What is physical memory?
Due to advancements in integrated circuit (IC) technology, semiconductor memory has become increasingly competitive. This has led to the modern random access memory (RAM) of a computer, known as physical memory or primary memory. It has a limited size because of the RAM chip and as a result, it needs a consistent flow to store data. Power issues and interruptions have the potential to erase data from the physical memory.
Physical memory address increases in a linear fashion. It holds the instructions of programs to execute and it is the only storage type that the CPU can access directly. The hard disk generally stores the programs. It takes longer for the CPU to access the hard disk every time in order to execute programs. Thus if physical memory initially stores the programs, the CPU can process them more quickly. We call them processes when they start to execute. The programs return to the hard drive when they have completed their execution.
Introduction to virtual memory
The primary memory such as RAM is limited and costly in the system. However, secondary memory such as disk is cheaper and large, and we can increase its size easily.
Virtual memory is a memory management strategy that creates an image of a very extensive memory for users so that these resources are really accessible on a given computer. This allows us to access secondary memory as if it were a part of the main memory. One approach for extending this is to temporarily move unused RAM storage to secondary storage.
Why is virtual memory useful?
Virtual memory essentially enables access to more memory than what is actually stored on the system. In other words, it allows processes to use more memory than what is physically available on the machine. It maintains this by moving data from the RAM to the disk, with the help of the operating system and the software and hardware installed on the computer. More on this later.
Systems that use virtual memory improve the utilization of RAM in cases of multiple tasks running at the same time. The benefits of using virtual memory are:
- Ability to share the memory between the processes
- Improved security due to memory isolation
- Possibility to use more memory that is physically available on the machine
How does virtual memory work?
Virtual memory exists in the form of pages with the help of the OS. These pages are atomic units that can store enormous programs. There might be a lot of storage in physical addresses for software. Since only specific parts are utilized for an application, only those parts are loaded into the RAM. Hence, at only one time only a part of the process (that the process is actually using) is loaded into the RAM of the computer.
Virtual memory creates an illusion that the process is working in a single and continuous memory area. Whereas, physically it can be non-continuous and partially stored on the mass storage devices.
Processes and resources kept in the memory are not continuous in real scenarios. In fact, the system breaks down and spread across them throughout the physical memory. Both the hardware and the software work together to map virtual addresses to physical addresses as shown above. Additionally, the memory management unit (MMU) helps while translating these addresses.
We see logical addresses instead of physical addresses in the operating system. At load, execute, or compile-time, the system translates these logical addresses dynamically. This can be implemented in two ways - demand segmentation and demand paging.
Paged virtual memory
Paging is a technique that allows storing and retrieving data from secondary storage. The physical memory is being split into fixed-size blocks called frames. The logical memory is being split into fixed-size blocks called pages. Subsequently, pages and frames are the same sizes.
While the process is executing, the pages are mounted into appropriate frames if needed. Therefore, the process is called demand paging or paging on-demand.
This method decreases the amount of I/O operations and RAM memory consumption. That way, a single execution of a large process could require just a little part of its code. Therefore, the system’s response time is improved. Subsequently, more users can be handled.
While the process is referring to the memory following situations can occur:
- The reference is invalid: the request is rejected
- The reference is valid and the required page is present (success scenario): request is handled correctly
- The reference is valid and the required page is absent: system must bring the required page from the disk into the memory
Validation Bit
A validation bit is a hardware support mechanism for verifying the page’s state. The validation bit is set for each record in the table of pages. The bit can take only two possible values: zero or one.
Initially, all bits are set to zero. It means that the page isn’t present in the RAM or the reference is invalid. On the other hand, when the bit is set to 1, the reference is valid and the page is present. If the bit is set to 0 during the address translation, the situation it’s called a page fault.
Page fault
When the page fault occurs the processor transfers the control to the operating system. Firstly, the system locates the required data on the disk. Secondly, it looks for a free frame in the RAM and then loads the page into that frame. After that, it updates the table of pages. Finally, the process continues to execute. If there is no free frame, a page replacement algorithm is used to find a free frame. The optimal page replacement algorithm exists only theoretically. You can find more information about page replacement here.
A process that is spending more time paging than executing is said to be thrashing. In other words, it means, that the process doesn't have enough frames to hold all the pages for its execution, so it is swapping pages in and out very frequently to keep executing. This leads to:
- low CPU utilization because most of the processes are waiting for pages
- operating system spends most of its time swapping to disk
Segmented virtual memory
Segmentation is the process of splitting the physical memory into continuous blocks called segments. The segments can be of different sizes. Therefore logical address is represented by two values: a segment number and an offset.
The system creates the segments on the application’s request. It passes the segment’s indexes to the application. The processes refer to memory areas within the segments and are not aware of the location of the data in the physical memory to which those segments refer. The processes are also unable to access segments of others processes.
Paging vs Segmentation
Physical vs virtual memory
- Physical memory is the actual RAM while virtual memory is a memory management technique
- Physical memory uses swapping while virtual memory uses paging
- Physical memory can access the CPU directly while virtual memory cannot
- Physical memory is limited to the size of the RAM while virtual memory is limited by the size of the disk
- Physical memory is faster than virtual memory
And that was all for this now! Make sure to leave a comment and follow me for more content. Until next time