0

What is virtual memory and, how it differs from physical memory (RAM)? It says that physical memory is stored on sth on motherboard, while virtual memory is stored on disk.

Somewhere it also says that virtual spaces are used only when the physical memory is filled, which confused me a lot.

Then, why Windows uses virtual memory? Is it because the RAMs are small-spaced and not designed for big storage, so use the virtual to store more bigger-sized things?

The next thing is about the address. Since virtuals are on disk, they shouldn't share the address of physicals. So they have independent addresses. Is that right?

And,

When writing memory of another process, why recommend using VirtualAlloc instead of HeapAlloc?

Is it true that virtual memory is process-dependent and the physical memory shared through processes?

  • *"When collecting memory for other process"*, what does that mean? – hyde Oct 15 '19 at 05:28
  • Also, that duplicate seems it doesn't really answer the main points of this question... – hyde Oct 15 '19 at 05:33
  • @hyde sorry, I did mean **writing**. ive made an edit. –  Oct 15 '19 at 05:34
  • 2
    `why Windows uses virtual memory?` all modern multitasking desktop OSes use virtual memory. It's not because RAM is small. It's just a way to provide separation between processes – phuclv Oct 15 '19 at 05:36
  • I still don't know what *"writing memory of another process"* actually means. – hyde Oct 15 '19 at 16:20
  • @hyde okay, I think perhaps the concept of "memory dependent of process" only exists in virtual memory. so I actually mean "writing the virtual memory of another process". So this question is solved. because the concept of process_dependent memory only exists in virtual memory. –  Oct 16 '19 at 01:59

3 Answers3

3

"Virtual memory" means there is a valid address space, which does not map to any particular physical memory or storage, hence virtual. In context of modern common operating systems, each process has its own virtual memory space, with overlapping virtual memory addresses.

This address space is divided into pages for easier management (example size 4 KB). Each valid page can be in 3 different states:

  • not stored physically (assumed to be all 0). If process writes to this kind of page, it needs to be given a page of physical memory (by OS, see below) so value can be stored.
  • Mapped to physical memory, meaning some page-size area in computers RAM stores the contents, and they can be directly used by the process.
  • Swapped out to disk (might be a swap file), in order to free physical RAM pages (done automatically by the operating system). If the process accesses the page (read or write), it needs to be loaded to page in RAM first (see above).

Only when virtual memory page is mapped to physical RAM page, is there something there. In other cases, if process accesses that page, there is a CPU exception, which transfers control to operating system. OS then needs to either map that virtual memory page to RAM (possibly needing to free some RAM first by swapping current data out to swap file, or terminating some application if out of all memory) and load the right data into it, or it can terminate the application (address was not in valid range, or is read-only but process tries to write).

Same page of memory can also be mapped to several places at once, for example with shared memory, so same data can be accessed by several processes at once (virtual address is probably different, so can't share pointer variables).

Another special case of virtual memory use is mapping a regular file on disk to virtual memory (same thing which happens with swap file, but now controlled by normal application process). Then OS takes care of actually reading bytes (in page-sized chunks) from disk and writing changes back, the process can just access the memory like any memory.


Every modern multi-tasking general purpose operating system uses virtual memory, because the CPUs they run support it, and because it solves a big bunch of problems, for example memory fragmentation, transparently using swapping to disk, memory protection... They could be solved differently, but virtual memory is the way today.


Physical memory is shared between processes the same way as computer power supply is shared, or CPU is shared. It is part of the physical computer. A normal process never handles actual physical memory addresses, all that it sees is virtual memory, which may be mapped to different physical locations.

The contents of virtual memory are not normally shared, except when they are (when using shared memory for example).


Not sure you mean by "When collecting memory for other process", so can't answer that.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • 1
    To slightly expand on his special case example, it's an optimization technique to avoid unnecessary additional allocations to memory. When modules are loaded from disk the OS will determine if that module already exists somewhere in physical memory. If it does then there's no point allocating a whole new region of physical memory for it, instead the virtual address range is reserved and points to the previously allocated physical memory. If you modify this module at all in memory then it will get remapped into a separate physical page with the changes. – Pickle Rick Oct 15 '19 at 04:40
1

Virtual memory can essentially be thought of as a per process virtual address that's mapped to a physical address. In the case of x86 there is a register CR3 that points to the translation table for that process. When allocating new memory the OS will allocate physical memory, which may not even be contiguous, and then set a free contiguous virtual region to point to that physical memory. Whenever the CPU accesses any virtual memory it uses this translation table in CR3 to convert it to the actual physical address.

More Info https://en.m.wikipedia.org/wiki/Control_register#CR3
https://en.m.wikipedia.org/wiki/Page_table

Pickle Rick
  • 808
  • 3
  • 6
  • Is it true that virtual memory is process-dependent and the physical memory shared through processes? –  Oct 15 '19 at 03:59
  • 1
    Virtual memory is process dependent, physical memory is not, but can only be accessed from kernel mode. – Pickle Rick Oct 15 '19 at 04:02
  • 1
    To clarify a little further, physical memory is of course where the data actually resides in RAM. Virtual memory is supported by the CPU but doesn't actually point anywhere real, it's more of a *concept* with the virtual address itself reserving several bit ranges that encode entries into the translation table. – Pickle Rick Oct 15 '19 at 04:14
  • 1
    (Cont'd...) The CPU has a lot of special registers and flags to determine how it should execute code and what privilege level it's at. Whenever code is executing in user mode, there is no way to directly read physical memory. As far as the CPU is concerned the full address space available is internal process virtual memory. In kernel you can map a physical address to a virtual address and then read it. – Pickle Rick Oct 15 '19 at 04:18
  • Saying that "physical memory ... can only be accessed from kernel mode" could be misconstrued to imply that a process has to go through the kernel to read/write physical memory. But the kernel is only involved if there's a page fault, in order to load pages from the paging file back to physical memory. For valid pages, address translation in a process context is handled automatically by the memory-management unit in the CPU, which even has dedicated caches to minimize the cost, called [translation lookaside buffers](https://en.wikipedia.org/wiki/Translation_lookaside_buffer). – Eryk Sun Oct 15 '19 at 15:36
  • That's true and I can see how what I said might be a little confusing. I actually considered clarifying that when I initially commented but between being on mobile and limited characters I just left it as is. What I meant is that physical memory is a foreign concept from the ring3 privilege level -- the CPU handles the translation, as mentioned in my original answer. To join in on the technical corrections, I would also add that the kernel is not only involved for page faults, it's responsible for all physical page management -- allocation, freeing, paging, etc. – Pickle Rick Oct 15 '19 at 20:41
-1

To quote Wikipedia:

In computing, virtual memory (also virtual storage) is a memory management technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which "creates the illusion to users of a very large (main) memory."

Because virtual memory is an illusory memory (so, non-existent), some other computer resources rather than RAM is used. In this case, the resource used is the disk, because it has a lot of space, more than RAM, where the OS can run its VM stuff.

Somewhere it also says that virtual spaces are used only when the physical memory is filled, which confused me a lot.

It shouldn't. VM uses the disk and I/O with the disk is much slower than I/O with the RAM. This is why physical memory is preferred nowadays and VM is used when physical memory is not enough.

Then, why Windows uses virtual memory? Is it because the RAMs are small-spaced and not designed for big storage, so use the virtual to store more bigger-sized things?

This is one of the main reasons, yes. In the past (the 70s), computer memory was very expensive so workarounds had to be conceived.

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
  • 2
    Your first comment references paging of memory, it's not directly related to virtual memory at all. Your second comment once again mistakes paging for virtual memory. Your final answer isn't accurate. Virtual memory is primarily used to securely separate memory access for each individual process, but there's more to the history. – Pickle Rick Oct 15 '19 at 03:54
  • Thanks a lot for your answer. I still have a question that is When collecting memory for other process, why recommend using VirtualAlloc instead of HeapAlloc? –  Oct 15 '19 at 03:55
  • @AkutaHinako unfortunately, I don't know how to answer here. – Bogdan Doicin Oct 15 '19 at 03:56
  • To append my earlier comment, paging refers to memory being 'paged out' if the OS determines it's not likely to be accessed any time soon. In this case the OS will copy that memory to disk (C:\Pagefile.sys iirc) while the physical memory it's mapped to is freed. If the memory is accessed later then it will trigger a page fault and the OS will remap the physical memory from the page file. – Pickle Rick Oct 15 '19 at 03:57
  • VirtualAlloc leads to NtAllocateVirtualMemory which allocates a new physical page (minimum PAGE_SIZE or 0x1000 on Windows) and sets up virtual address translations. HeapAlloc is more optimized, only allocating a new page when needed. – Pickle Rick Oct 15 '19 at 03:58
  • Neither VirtualAlloc or HeapAlloc can operate on different processes. You need to use VirtualAllocEx for the that. Why? Because it is designed for that task. It accepts a process handle parameter to allow caller to identify the target process. – David Heffernan Oct 15 '19 at 06:19