Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual Memory and Physical Memory

I am studying the concept of Memory Management Unit(MMU) from the book titled "Operating System Concepts" - by Abraham Silberschatz and Galvin. Though things were fine till chapter 8. As soon I started with chapter 9, things started messing up.

I am not clear about what my virtual memory is? Also, physical and logical addresses seems to be confusing now? Does it(virtual memory) exists in real or not? As per my understanding of now, the RAM of my system is what I call Physical(or main) Memory. I have 8GB RAM and 64- bit OS. Thus, my RAM can accommodate 2^64-1 addresses. Is this what I call physical address space? Also, what exactly is logical address space?

Every process has to be in main memory for execution otherwise it resides on hard disk. Are the addresses given to instructions of my code residing on hard disk is what I call logical address? And when it's loaded in RAM, because location is not fixed and hence the code can be loaded anywhere, the addresses assigned here(RAM) called Physical Addresses? This mapping I suppose is referred to as Logical-Physical address mapping.

Now, because size of my code or process can be large than the size of RAM available, here comes the use of virtual memory. As I understood, it's an abstraction to give the programmer a view that he has an infinite amount of memory available on the system. It's basically an area of hard disk where some processes(which are seldom used) from RAM are swapped out. Simultaneously desired pages are brought in the main memory. Is it so? Then what determines the size of this area on hard disk? Also, RAM is cheap, then why do we need to have such a mechanism? Can't we increase our RAM size instead of including this overhead of swapping?

I have searched much on web but didn't find the exact definition and difference between these terms. Please help!

Thanks

like image 809
user3552407 Avatar asked Jul 20 '15 19:07

user3552407


1 Answers

I am not clear about what my virtual memory is? Also, physical and logical addresses seems to be confusing now? Does it(virtual memory) exists in real or not?

You can read a decent explanantion on Wikipedia about Virtual Memory. I am not going to discuss the whole thing here.

Yes, virtual memory exists in real. It maps memory addresses used by a program, called virtual addresses, into physical addresses in computer memory. Main storage as seen by a process or task appears as a contiguous address space or collection of contiguous segments.

The primary benefits of virtual memory include freeing applications from having to manage a shared memory space, increased security due to memory isolation, and being able to conceptually use more memory than might be physically available, using the technique of paging.

Thanks to David Schwartz for helping me improve the content. Still in embedded devices, virtual-memory is used just for the page-mapping, it's native purpose for which it was defined. But, now in modern OS', it has taken a totally different form. People are exploiting the usage of virtual-memory in paging/segmentation,thus swapping being the most-important.

The extra memory area is also known as swap-area or swap-partition nowadays which is generally reserved for usage by OS(Unix/Linux) for swapping process in and out of the main-memory. Windows has pagefiles for achieving the same.

I have 8GB RAM and 64- bit OS. Thus, my RAM can accommodate 2^64-1 addresses. Is this what I call physical address space?

You should talk about processor here, and not OS or the RAM directly. In principle, a 64-bit microprocessor can address 16 EiBs (16 × 2^60 bytes) of memory. In practice, it is less than that. This memory is what you can possibly use as RAM.

Also, what exactly is logical address space?

Logical address space is the address space consisting of addresses at which items (memory cell, storage element, network host) appear to reside from the perspective of an executing application program.

A logical address may be different from the physical address due to the operation of an address translator or mapping function. Such mapping functions may be, in the case of a computer memory architecture, a memory management unit (MMU) between the CPU and the memory bus, or an address translation layer, e.g., the Data Link Layer, between the hardware and the internetworking protocols (Internet Protocol) in a computer networking system.

In a system supporting virtual memory, there may actually not be any physical memory mapped to a logical address until an access is attempted. The access triggers special functions of the operating system which reprogram the MMU to map the address to some physical memory, perhaps writing the old contents of that memory to disk and reading back from disk what the memory should contain at the new logical address. In this case, the logical address may be referred to as a virtual address.

Every process has to be in main memory for execution otherwise it resides on hard disk. Are the addresses given to instructions of my code residing on hard disk is what I call logical address? And when it's loaded in RAM, because location is not fixed and hence the code can be loaded anywhere, the addresses assigned here(i RAM) called Physical Addresses? This mapping I suppose is referred to as Logical-Physical address mapping.

Nowadays, almost all systems support virtual memory(there are a few exceptions). So, yes, when your processes are swapped out from RAM so that other processes could execute, they are separately kept in that reserved portion of hard-disk called virtual memory. And, there is an addressing scheme for virtual-memory which is what you call as a logical address.

Page tables are used to translate the virtual addresses seen by the application into physical addresses used by the hardware to process instructions; such hardware that handles this specific translation is often known as the memory management unit. Each entry in the page table holds a flag indicating whether the corresponding page is in real memory or not. If it is in real memory, the page table entry will contain the real memory address at which the page is stored.

Now, because size of my code or process can be large than the size of RAM available, here comes the use of virtual memory. As I understood, it's an abstraction to give the programmer a view that he has an infinite amount of memory available on the system. It's basically an area of hard disk some processes(which are seldom used) from RAM are swapped out. Simultaneously desired pages are brought in the main memory. Is it so? Then what determines the size of this area on hard disk?

Exactly the same as described above. And, this size of reserved hard-disk space(virtual-memory) is recommended by different types of OS differently. But, generally it is defined to be on a different partition on Unix/Linux OS(swap partition). Windows has pagefile compared to *nix's swap partition; although there are many technical differences between the two.. This is OS-specific but the concept is almost the same. In *nix systems I have seen that it is recommended to keep the size of swap partition to be double of the size of the RAM in the system. I can't argue more about this,maybe someone can suggest more detail.

Also, RAM is cheap, then why do we need to have such a mechanism? Can't we increase our RAM size instead of including this overhead of swapping?

No, as compared to the cost of the RAM, the price of hard-disk is still much economical. Also, RAM is expensive and not all PCs can be upgraded to increase RAM. Luckily increasing virtual memory is the best option when you are low on memory. Also, Microsoft recommends that you set virtual memory to be no less than 1.5 times and no more than 3 times the amount of RAM on your computer. (Source of the last line).

like image 72
Am_I_Helpful Avatar answered Oct 05 '22 08:10

Am_I_Helpful