4
votes

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

3
Were none of the 'About 10,600,000 results' returned from Googling your title any use?Martin James
@MartinJames Yes, they were, but certain terms had to be explicitly mentioned for the explanation. That;s why after every understanding of mine, I'm asking if I got it right.user3552407

3 Answers

11
votes

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).

3
votes

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.

Essentially correct.

It's basically an area of hard disk some processes(which are seldom used) from RAM are swapped out.

No, that's paging or swap, which has almost nothing to do with virtual memory. You can have swap and paging without virtual memory (in fact, swap came first historically). You can have virtual memory without paging or swap (as many embedded devices do).

While many modern operating systems use virtual memory to implement paging/swapping, what virtual memory is has nothing to do with paging or swapping.

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?

RAM is not so cheap that we want to waste it holding data that will likely never be accessed. Consider, for example, all the services on your computer that start when your OS boots. Each service loads some code into memory and occupies some RAM to hold its structures. Many of those services won't run for days, if ever. Do you really want all that stuff cluttering RAM forever?

1
votes

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.

If you made it to chapter 8 reading that book before getting confused, I salute you!

Physical: The actual hardware memory on the system.

Logical: Is a mapping of linear memory addresses to physical memory that is independent of the physical memory addresses.

Logical memory is what provides the basic security for a system. Each process has its own logical address space. This usually divided into a user space and a system space. In the user space, a the same logical address usually maps to a different physical address. In the system space, the same logical addresses map to the same physical address.

Thus, my RAM can accommodate 2^64-1 addresses. Is this what I call physical address space? Also, what exactly is logical address space?

That is the logical address space. The virtual address can be the same as the logical address space but it is usually limited smaller by the operating system through system parameters or process quotas.

Virtual Memory: Logical to Physical memory mapping is simulated in software. Each page of logical memory maps to blocks of disk space. A logical address may or may not refer to an actual physical memory 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?

Kinda sorta. When you access a logical memory address and its has not been mapped into memory it triggers a page fault (restartable instruction). The operating system's fault handler gets invoked (usually using the same method for handing divide by zero or interrupts). That handler tries to load the memory from disk.

When a program starts up it page faults like mad then usually levels off.

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?

When the page is loaded it is to a physical address. The operating system sets up the process page table so that it maps the logical address to the correct physical address.

Does it(virtual memory) exists in real or not?

Yes. Try running a Mac with 8GB of memory and 4GB devoted to a Windoze emulator. You'll run out of physical memory real fast.

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?

In ye olde days of 32 virtual memory and 8MB of physical memory, virtual memory was critical. In the early 1980's 8MB of memory required a cabinet the size of a refrigerator. In the future virtual memory is likely to go away entirely because, as you say, memory is (or will be) cheap.

I expect that when we start seeing memory in TB disk based operating systems (e.g., eunuchs and Windoze) go the way of the dodo.

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.

That's really logical memory. Virtual memory is the simulation of some part of the logical address space. On a 32-bit system, you may have 2^31 logical addresses for the user space. However, the system may limit you to 2^20 (picked a number) virtual addresses.

It's basically an area of hard disk where some processes(which are seldom used) from RAM are swapped out.

No. All processes must have a disk backing for all their memory. The size of the disk space (page file, swap partition) imposes limits on the amount of virtual memory available.

Then what determines the size of this area on hard disk?

There generally has to be as much disk space as there is virtual memory.