虚拟内存和物理内存有什么区别?

sta*_*k92 90 virtualization ram operating-system memory-management virtual-memory

我经常对操作系统中的虚拟化概念感到困惑.考虑将RAM作为物理内存,为什么我们需要虚拟内存来执行进程?

当来自外部硬盘驱动器的进程(程序)被带到主存储器(物理存储器)以执行时,该虚拟存储器在哪里.

谁负责虚拟内存以及虚拟内存的大小?

假设RAM的大小是4GB(即2 ^ 32-1地址空间),虚拟内存的大小是多少?

Pin*_*ade 81

除其他外,虚拟内存是一种抽象,为程序员提供了在系统上拥有无限内存的错觉.

虚拟内存映射与实际物理地址相对应.该操作系统创建和这些映射的交易-利用其他的数据结构中的页表,以保持映射.虚拟内存映射总是在页表或某些类似的数据结构中找到(在虚拟内存的其他实现的情况下,我们可能不应该将其称为"页表").页表也在物理内存中 - 通常在内核保留空间中,用户程序无法写入.

虚拟内存通常比物理内存大 - 如果虚拟内存和物理内存大小相同,虚拟内存映射的理由就不多了.

只有程序所需的部分通常驻留在内存中 - 这是一个名为"分页"的主题.虚拟内存和分页紧密相关,但不是相同的主题.还有其他虚拟内存实现,例如分段.

我可能会在这里假设错误,但我敢打赌你发现难以解决的问题与虚拟内存的特定实现有关,很可能是分页.没有一种方法可以进行分页 - 有很多实现,你的教科书描述的那个可能与Linux/Windows等真实操作系统中出现的实现不一样 - 可能存在细微差别.

我可以写一千段有关分页的内容......但我认为最好留给另一个专门针对该主题的问题.

  • 虚拟内存和物理内存大小相同是有原因的.VM允许不同的进程拥有自己的地址空间.这可以保护一个进程的数据不被另一个进程写入.它还允许您为不同的地址空间提供不同的权限,因此系统的某些用户可以具有比其他用户更高的读/写权限.但是,具有相同数量的虚拟内存和物理内存确实消除了VM的存储优势. (4认同)
  • 添加到 almel 的评论中:即使虚拟内存比物理内存更小或相同大小:除了安全性和稳定性优势之外,多个 32 位程序可以在内存中运行,否则无法运行(例如在内存中) 64 位系统),可以更好地管理物理内存以避免一些碎片问题,透明的写时复制内存技术需要 VM 等... (2认同)
  • 请注意,虚拟内存绝不是"无限的",这样的设计也不会激发任何这样的幻想.AMD64架构目前允许对虚拟内存进行48位寻址([AMD APM Vol 2.第120页](http://support.amd.com/TechDocs/24593.pdf))虽然用例不尽相同,但人们可能会争论一个主要优点是能够保留比物理空间中通常可能的更大的,连续的地址空间运行.然后根据需要提交此保留范围,这可以消除对链接结构的需要以及重新分配. (2认同)

Sex*_*ast 67

Softwares run on the OS on a very simple premise - they require memory. The device OS provides it in the form of RAM. The amount of memory required may vary - some softwares need huge memory, some require paltry memory. Most (if not all) users run multiple applications on the OS simultaneously, and given that memory is expensive (and device size is finite), the amount of memory available is always limited. So given that all softwares require a certain amount of RAM, and all of them can be made to run at the same time, OS has to take care of two things:

  1. That the software always runs until user aborts it, i.e. it should not auto-abort because OS has run out of memory.
  2. The above activity, while maintaining a respectable performance for the softwares running.

Now the main question boils down to how the memory is being managed. What exactly governs where in the memory will the data belonging to a given software reside?

Possible solution 1: Let individual softwares specify explicitly the memory address they will use in the device. Suppose Photoshop declares that it will always use memory addresses ranging from 0 to 1023 (imagine the memory as a linear array of bytes, so first byte is at location 0, 1024th byte is at location 1023) - i.e. occupying 1 GB memory. Similarly, VLC declares that it will occupy memory range 1244 to 1876, etc.

Advantages:

  1. Every application is pre-assigned a memory slot, so when it is installed and executed, it just stores its data in that memory area, and everything works fine.

Disadvantages:

  1. 这不规模.从理论上讲,应用程序在执行非常重要的操作时可能需要大量内存.因此,为了确保它永远不会耗尽内存,分配给它的内存区域必须始终大于或等于该内存量.如果最大理论内存使用量2 GB(因此需要2 GB从RAM分配内存)的软件安装在只有1 GB内存的机器上会怎么样?该软件应该在启动时中止,并说可用的RAM小于2 GB?或者它应该继续,并且在需要的内存超过的那一刻2 GB,只是中止并挽救没有足够内存可用的消息?

  2. It is not possible to prevent memory mangling. There are millions of softwares out there, even if each of them was allotted just 1 kB memory, the total memory required would exceed 16 GB, which is more than most devices offer. How can, then, different softwares be allotted memory slots that do not encroach upon each other's areas? Firstly, there is no centralized software market which can regulate that when a new software is being released, it must assign itself this much memory from this yet unoccupied area, and secondly, even if there were, it is not possible to do it because the no. of softwares is practically infinite (thus requiring infinite memory to accommodate all of them), and the total RAM available on any device is not sufficient to accommodate even a fraction of what is required, thus making inevitable the encroaching of the memory bounds of one software upon that of another. So what happens when Photoshop is assigned memory locations 1 to 1023 and VLC is assigned 1000 to 1676? What if Photoshop stores some data at location 1008, then VLC overwrites that with its own data, and later Photoshop accesses it thinking that it is the same data is had stored there previously? As you can imagine, bad things will happen.

So clearly, as you can see, this idea is rather naive.

Possible solution 2: Let's try another scheme - where OS will do majority of the memory management. Softwares, whenever they require any memory, will just request the OS, and the OS will accommodate accordingly. Say OS ensures that whenever a new process is requesting for memory, it will allocate the memory from the lowest byte address possible (as said earlier, RAM can be imagined as a linear array of bytes, so for a 4 GB RAM, the addresses range for a byte from 0 to 2^32-1) if the process is starting, else if it is a running process requesting the memory, it will allocate from the last memory location where that process still resides. Since the softwares will be emitting addresses without considering what the actual memory address is going to be where that data is stored, OS will have to maintain a mapping, per software, of the address emitted by the software to the actual physical address (Note: that is one of the two reasons we call this concept Virtual Memory. Softwares are not caring about the real memory address where their data are getting stored, they just spit out addresses on the fly, and the OS finds the right place to fit it and find it later if required).

Say the device has just ben turned on, OS has just launched, right now there is no other process running (ignoring the OS, which is also a process!), and you decide to launch VLC. So VLC is allocated a part of the RAM from the lowest byte addresses. Good. Now while the video is running, you need to start your browser to view some webpage. Then you need to launch Notepad to scribble some text. And then Eclipse to do some coding.. Pretty soon your memory of 4 GB is all used up, and the RAM looks like this:

                                   在此输入图像描述

Problem 1: Now you cannot start any other process, for all RAM is used up. Thus programs have to be written keeping the maximum memory available in mind (practically even less will be available, as other softwares will be running parallelly as well!). In other words, you cannot run a high-memory consuming app in your ramshackle 1 GB PC.

Okay, so now you decide that you no longer need to keep Eclipse and Chrome open, you close them to free up some memory. The space occupied in RAM by those processes is reclaimed by OS, and it looks like this now:

                                    在此输入图像描述

Suppose that these two frees up 700 MB space - (400 + 300) MB. Now you need to launch Opera, which will take up 450 MB space. Well, you do have more than 450 MB space available in total, but...it is not contiguous, it is divided into individual chunks, none of which is big enough to fit 450 MB. So you hit upon a brilliant idea, let's move all the processes below to as much above as possible, which will leave the 700 MB empty space in one chunk at the bottom. This is called compaction. Great, except that...all the processes which are there are running. Moving them will mean moving the address of all their contents (remember, OS maintains a mapping of the memory spat out by the software to the actual memory address. Imagine software had spat out an address of 45 with data 123, and OS had stored it in location 2012 and created an entry in the map, mapping 45 to 2012. If the software is now moved in memory, what used to be at location 2012 will no longer be at 2012, but in a new location, and OS has to update the map accordingly to map 45 to the new address, so that the software can get the expected data (123) when it queries for memory location 45. As far as the software is concerned, all it knows is that address 45 contains the data 123!)! Imagine a process that is referencing a local variable i. By the time it is accessed again, its address has changed, and it won't be able to find it any more. The same will hold for all functions, objects, variables, basically everything has an address, and moving a process will mean changing the address of all of them. Which leads us to:

Problem 2: You cannot move a process. The values of all variables, functions and objects within that process have hardcoded values as spat out by the compiler during compilation, the process depends on them being at the same location during its lifetime, and changing them is expensive. As a result, processes leave behind big "holes" when they exit. This is called External Fragmentation.

精细.假设某种方式,通过一些奇迹般的方式,你确实设法将进程向上移动.现在700 MB底部有自由空间:

                        在此输入图像描述

Opera平稳地适应底部.现在你的RAM看起来像这样:

                                    在此输入图像描述

Good. Everything is looking fine. However, there is not much space left, and now you need to launch Chrome again, a known memory-hog! It needs lots of memory to start, and you have hardly any left...Except.. you now notice that some of the processes, which were initially occupying large space, now is not needing much space. May be you have stopped your video in VLC, hence it is still occupying some space, but not as much as it required while running a high resolution video. Similarly for Notepad and Photos. Your RAM now looks like this:

                                        在此输入图像描述

Holes, once again! Back to square one! Except, previously, the holes occurred due to processes terminating, now it is due to processes requiring less space than before! And you again have the same problem, the holes combined yield more space than required, but they are scattered around, not much of use in isolation. So you have to move those processes again, an expensive operation, and a very frequent one at that, since processes will frequently reduce in size over their lifetime.

Problem 3: Processes, over their lifetime, may reduce in size, leaving behind unused space, which if needed to be used, will require the expensive operation of moving many processes. This is called Internal Fragmentation.

Fine, so now, your OS does the required thing, moves processes around and start Chrome and after some time, your RAM looks like this:

在此输入图像描述

Cool. Now suppose you again resume watching Avatar in VLC. Its memory requirement will shoot up! But...there is no space left for it to grow, as Notepad is snuggled at its bottom. So, again, all processes has to move below until VLC has found sufficient space!

Problem 4: If processes needs to grow, it will be a very expensive operation

Fine. Now suppose, Photos is being used to load some photos from an external hard disk. Accessing hard-disk takes you from the realm of caches and RAM to that of disk, which is slower by orders of magnitudes. Painfully, irrevocably, transcendentally slower. It is an I/O operation, which means it is not CPU bound (it is rather the exact opposite), which means it does not need to occupy RAM right now. However, it still occupies RAM stubbornly. If you want to launch Firefox in the meantime, you can't, because there is not much memory available, whereas if Photos was taken out of memory for the duration of its I/O bound activity, it would have freed lot of memory, followed by (expensive) compaction, followed by Firefox fitting in.

Problem 5: I/O bound jobs keep on occupying RAM, leading to under-utilization of RAM, which could have been used by CPU bound jobs in the meantime.

So, as we can see, we have so many problems even with the approach of virtual memory.


There are two approaches to tackle these problems - paging and segmentation. Let us discuss paging. In this approach, the virtual address space of a process is mapped to the physical memory in chunks - called pages. A typical page size is 4 kB. The mapping is maintained by something called a page table, given a virtual address, all now we have to do is find out which page the address belong to, then from the page table, find the corresponding location for that page in actual physical memory (known as frame), and given that the offset of the virtual address within the page is same for the page as well as the frame, find out the actual address by adding that offset to the address returned by the page table. For example:

在此输入图像描述

On the left is the virtual address space of a process. Say the virtual address space requires 40 units of memory. If the physical address space (on the right) had 40 units of memory as well, it would have been possible to map all location from the left to a location on the right, and we would have been so happy. But as ill luck would have it, not only does the physical memory have less (24 here) memory units available, it has to be shared between multiple processes as well! Fine, let's see how we make do with it.

When the process starts, say a memory access request for location 35 is made. Here the page size is 8 (each page contains 8 locations, the entire virtual address space of 40 locations thus contains 5 pages). So this location belongs to page no. 4 (35/8). Within this page, this location has an offset of 3 (35%8). So this location can be specified by the tuple (pageIndex, offset) = (4,3). This is just the starting, so no part of the process is stored in the actual physical memory yet. So the page table, which maintains a mapping of the pages on the left to the actual pages on the right (where they are called frames) is currently empty. So OS relinquishes the CPU, lets a device driver access the disk and fetch the page no. 4 for this process (basically a memory chunk from the program on the disk whose addresses range from 32 to 39). When it arrives, OS allocates the page somewhere in the RAM, say first frame itself, and the page table for this process takes note that page 4 maps to frame 0 in the RAM. Now the data is finally there in the physical memory. OS again queries the page table for the tuple (4,3), and this time, page table says that page 4 is already mapped to frame 0 in the RAM. So OS simply goes to the 0th frame in RAM, accesses the data at offset 3 in that frame (Take a moment to understand this. The entire page, which was fetched from disk, is moved to frame. So whatever the offset of an individual memory location in a page was, it will be the same in the frame as well, since within the page/frame, the memory unit still resides at the same place relatively!), and returns the data! Because the data was not found in memory at first query itself, but rather had to be fetched from disk to be loaded into memory, it constitutes a miss.

Fine. Now suppose, a memory access for location 28 is made. It boils down to (3,4). Page table right now has only one entry, mapping page 4 to frame 0. So this is again a miss, the process relinquishes the CPU, device driver fetches the page from disk, process regains control of CPU again, and its page table is updated. Say now the page 3 is mapped to frame 1 in the RAM. So (3,4) becomes (1,4), and the data at that location in RAM is returned. Good. In this way, suppose the next memory access is for location 8, which translates to (1,0). Page 1 is not in memory yet, the same procedure is repeated, and the page is allocated at frame 2 in RAM. Now the RAM-process mapping looks like the picture above. At this point in time, the RAM, which had only 24 units of memory available, is filled up. Suppose the next memory access request for this process is from address 30. It maps to (3,6), and page table says that page 3 is in RAM, and it maps to frame 1. Yay! So the data is fetched from RAM location (1,6), and returned. This constitutes a hit, as data required can be obtained directly from RAM, thus being ver

  • 这是对VM /分页的很好的深入说明(应该是某处的博客文章)。VM映射/分页的一部分使我感到困惑,因为它对于每个页面错误或交换仍然(似乎)需要大量磁盘访问。每个页面交换(从VM到磁盘,反之亦然)是否调用对磁盘的读/写操作?对我来说,这似乎是一笔巨大的开销。 (2认同)

Cle*_*oys 16

我无耻地复制顶部手册页的摘录

VIRT - 虚拟映像(kb)任务使用的虚拟内存总量.它包括所有代码,数据和共享库以及已换出的页面和已映射但未使用的页面.

SWAP - 交换大小(kb)非常驻但存在于任务中的内存.这是已被换出但可能包含额外非驻留内存的内存.通过从虚拟内存中减去物理内存来计算此列


Rei*_*amn 6

请参见此处: 物理与虚拟内存

虚拟内存存储在硬盘驱动器上,并在RAM填满时使用.物理内存仅限于安装在计算机中的RAM芯片的大小.虚拟内存受硬盘驱动器大小的限制,因此虚拟内存可以提供更多存储空间.

  • @BruceJohnJennerLawso:不,虚拟=交换+物理 (2认同)

小智 6

物理内存:物理内存是指计算机中的RAM或主内存。物理内存是易失性内存。因此,需要持续的电力来保留数据。

虚拟内存是逻辑内存。换句话说,它是操作系统执行的一种内存管理技术。虚拟内存允许程序员为程序使用比可用物理内存更多的内存。如果物理内存为4GB,虚拟内存为16GB,则程序员可以使用16GB虚拟内存来执行程序。使用虚拟内存,他可以执行比物理内存需要更多内存的复杂程序。

物理内存和虚拟内存之间的主要区别在于,物理内存是指连接到主板的系统的实际RAM,但虚拟内存是一种内存管理技术,允许用户执行大于实际物理内存的程序。