5

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

user3004790
  • 748
  • 7
  • 10
user3552407
  • 361
  • 1
  • 4
  • 14
  • Were none of the 'About 10,600,000 results' returned from Googling your title any use? – Martin James Jul 21 '15 at 01:04
  • possible duplicate of [What are the differences between virtual memory and physical memory?](http://stackoverflow.com/questions/14347206/what-are-the-differences-between-virtual-memory-and-physical-memory) – Martin James Jul 21 '15 at 01:05
  • @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 Jul 21 '15 at 04:24

3 Answers3

14

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

Community
  • 1
  • 1
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • 1
    If someone finds something altered, please correct it for me. I have typed it in too hurry. Thanks for going through the answer... – Am_I_Helpful Jul 20 '15 at 20:04
  • "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." This is incorrect. Virtual memory and swap are different things and you can have virtual memory without swap (many embedded devices) and you can have swap without virtual memory (swap existed before virtual memory). – David Schwartz Jul 20 '15 at 20:05
  • The term mentioned as logical memory in whole description of chapter 8 then concerns with the hard disk space in general I guess? If yes, then we are implementing paging between the main memory (RAM) and hard disk. Some portion of it is reserved as the virtual memory which some operating systems use for swapping in and out (or in a way paging only). Is it? – user3004790 Jul 20 '15 at 20:18
  • @user3004790-Not toally as mentioned in the comments by David in his answer, Actually, to reduce the page-faults there are ways to implement it without extra space. But, believe me paging/segmentataion/swapping---all this is only possible with this hard disk thing. It is unlikely to occur without using hard disk. So, you can safely presume that. I'd also suggest you to skim through that Wikipedia Article once. – Am_I_Helpful Jul 20 '15 at 20:21
  • Reading through all, I think there's a difference between swap space and virtual memory. Swap space is the area on my hard disk where pages are stored which are not in use. While Virtual memory is that portion of hard disk where pages in active use are stored, like when swapping occurs in between the process' execution and its likely that the same page will be swapped in the near future. So, even without virtual memory, paging is possible but adding a separate memory as virtual is like a cherry on the cake as we are virtually increasing the size of RAM. But does that dec. memory access time? – user3004790 Jul 21 '15 at 04:57
  • @user3004790-*While Virtual memory is that portion of hard disk where pages in active use are stored, like when swapping occurs in between the process' execution and its likely that the same page will be swapped in the near future...*---This exactly is what I mentioned in my answer. And, you seem to get confused---when pages which are swapped from RAM, they are placed in virtual-memory(so called swap-partition in Linux,pagefiles in Windows ). Yes, but virtual memory is possible without paging(as mentioned by David in the comment above). ...... – Am_I_Helpful Jul 21 '15 at 06:18
  • @user3004790-Swap space have a slower access time than physical memory. Check this link from [Redhat](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Deployment_Guide/ch-swapspace.html), the swap's advantages supercedes the fast-accessable RAM. A virtual-memory is the extra space managed by kernel to allow the swapping,etc. It can be even under system-reserved spaces. And then there can be no paging/segmentation/swapping,etc. But, it would be for usage in minor devices.decreasing page faults was the basic reason for creation of virtual-memory in the OS. – Am_I_Helpful Jul 21 '15 at 06:27
  • @user3004790- I have improved my answer a lot, the first second paragraph. please revisit this to be aware of the changes. If you've any doubt left,please let me know... – Am_I_Helpful Jul 21 '15 at 06:43
3

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?

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • SIR-please check this accepted answer, http://stackoverflow.com/a/4970555/3482140. `Swap space is the portion of virtual memory that is on the hard disk, used when RAM is full`. – Am_I_Helpful Jul 20 '15 at 20:08
  • That doesn't say virtual memory is an area of hard disk, it says swap is. And, of course, that would only be correct on operating systems that implement swap through virtual memory. (The other answers help clarify how the accepted answer is confusing.) – David Schwartz Jul 20 '15 at 20:09
  • Also, even Wikipedia states that `Embedded systems and other special-purpose computer systems that require very fast and/or very consistent response times may opt not to use virtual memory due to decreased determinism; virtual memory systems trigger unpredictable traps that may produce unwanted "jitter" during I/O operations. ` and that Galvin book beautifully describes the points which I mentioned. Check ***usage*** on Wikipedia... – Am_I_Helpful Jul 20 '15 at 20:10
  • I'd have to see a link to see the context, but I think that's probably incorrect. Those systems *do* use virtual memory, they just don't use paging or swap. – David Schwartz Jul 20 '15 at 20:12
  • Also, I'd like to argue as to how you can implement paging without accessing extra hard-disk space? Where would you bring extra space from to implement paging/segmentation(old)??? Please read [this](https://en.wikipedia.org/wiki/Virtual_memory#Usage) SIR. The very last line SIR. Please,allow me to say that you might have mis-understood my point. I did also add Windows pagefile's approach, it's not all about swap. You can check I discussed that *nix systems use `swap` to implement virtual memory. I request you to re-read my contents SIR. – Am_I_Helpful Jul 20 '15 at 20:13
  • 1
    You can implement paging without extra hard disk space by only discarding clean pages (pages that hold unmodified copies of data on disk). But I'm not sure why you asked that. Yes, paging or swapping involves moving things between RAM and mass storage and some operating systems implement it using virtual memory, though you can have one without the other. What virtual memory *is* has nothing to do with paging or swapping, though virtual memory is often used to implement paging/swapping. – David Schwartz Jul 20 '15 at 20:15
  • Yes SIR, this last point I agree with SIR. It has nothing exactly to do with all of them,but, it is used to implement these things only. To improve page faults, to reduce less main memory usage. But, I don't see a way how could modern OS' not implement is using swap(*nix)/pagefile(Windows)! Please enlighten me if you think I'm still wandering incorrect SIR. Thanks. – Am_I_Helpful Jul 20 '15 at 20:19
  • Modern operating systems implement paging through their virtual memory systems. They basically use most of their RAM like a giant cache. But it's extremely important not to confuse how something is implemented with what it is. There are many systems with virtual memory and no paging/swap. For example, most small routers and modems. – David Schwartz Jul 20 '15 at 20:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83799/discussion-between-shekhar-suman-and-david-schwartz). – Am_I_Helpful Jul 20 '15 at 20:22
  • @David-I have attributed you in my answer if you don't mind. And, I have also substantially improved my answer. Also, I added the embedded-systems which you mentioned. Thanks a lot SIR. And, finally upvoted your answer, +1. – Am_I_Helpful Jul 21 '15 at 06:40
1

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.

user3344003
  • 20,574
  • 3
  • 26
  • 62