码迷,mamicode.com
首页 > 其他好文 > 详细

Virtual memory and Pagefile

时间:2020-06-20 16:46:08      阅读:46      评论:0      收藏:0      [点我收藏+]

标签:href   back   通过   ram   sof   location   连续   html   运行   

Virtual memory and Pagefile

32位的Windows

参考https://support.microsoft.com/en-us/help/2160852/ram-virtual-memory-pagefile-and-memory-management-in-windows

  • 所有的虚拟地址都通过硬件(MMU)来翻译成RAM上的地址,但操作系统kernel的core部分除外,他们是直接使用真实的RAM内存地址
  • 对于32位的系统而言,虚拟地址空间最大为$ 4,294,967,295 (2*32-1 = 4 GB) $,不管实际安装的RAM内存大小有多少
  • 在默认配置下,4GB虚拟地址空间中的2GB用来给各个进程单独使用,剩下的2GB由操作系统以及各个进程共享。也就是实际上一个进程能够支配的虚拟地址空间并没有完整的4GB
  • Physical Address Extension (PAE)技术用来扩展physical memory(RAM)的地址位到36位,这样的话,处理器能够支配的物理内存空间就更大了。PAE技术并不影响虚拟地址空间

Pagefile

RAM是有限的,而虚拟内存则是无限的,因为可能有很多进程在运行,他们都需要各自的虚拟内存。这样可能就会出现RAM耗尽的情况。在此种情况下,系统则会把部分虚拟内存的pages存储到磁盘hard disk上去,每个page的大小为4KB。这些存储到磁盘的文件对应的就是系统上的Pagefiles.sys文件。注意,page对应的是虚拟地址的概念,而不是RAM的地址。

虚拟地址和Page file的区别

https://arstechnica.com/civis/viewtopic.php?t=925099

Virtual memory is a whole lot more than a page file.

Each byte of computer memory has an address.

In a system without virtual memory, the addresses that a program can see directly correspond to a physical memory location. This has a number of side-effects. One big one is that a program can see all the memory, even things that don‘t concern it -- it just has to look at the addresses that other programs are located. If a program inadvertantly writes to the wrong address, it might write on top of another program, damaging that program (that is, no memory protection). A program can‘t easily load a file that‘s bigger than the amount of memory installed in the computer. If your system has an ugly memory access scheme (for instance, segmented memory) you have to deal with that.

Virtual memory virtualizes memory addresses. It breaks the direct correlation between the memory addresses that programs can see, and the physical memory.

When done properly (MacOS since version 7 (IIRC) has virtualized addresses to make the memory space contiguous, where once it had holes, but doesn‘t really do anything beyond that -- this is "not properly") this gives you all sorts of benefits.

The way it‘s done (glossing over the details) is to have a look-up table, that maps virtual addresses to real addresses. It divides the set of addresses along 4 kbyte borders, and each 4 kbyte section -- called a page -- is treated as a single unit (so, for instance, if the OS wants to rearrange things how they lie in memory then it has to move whole pages around; if it wants to make a bit of memory read-only, it does it for a whole page at a time, etc.).

The first thing you do is to give each program its own set of virtual addresses. When you do this, you make it such that the addresses that one program can see have no relationship to the addresses another program can see. This means that each program is isolated from the other programs. You have protected memory. This introduces other problems (like, sharing data between programs), but they‘re solvable.

Because you have this indirection scheme (memory addresses get looked up), you can implement a disk paging scheme. Rather than a virtual address resolving to somewhere in-memory, it can resolve to somewhere on-disk -- you can have a system whereby you pull the information into memory from disk as and when needed. The opposite can also happen -- if a page isn‘t needed for a long time, and something else needs the memory, it can write the page out to disk -- the only drawback being, next time it needs to read it, it has to read it from the disk again.

With executables, it doesn‘t actually have to write anything out to disk -- it can get rid of its copy of the executable in memory, because it knows that it can read the original file again if it needs it back.

But with data, it can‘t do this, because the data has no on-disk representation. So what the OS does is to create a big file called the pagefile, which it can use to store data pages in this way.

Virtual memory doesn‘t require a pagefile -- it‘s a lot more than that (there are other things you can do that I haven‘t mentioned, that use the same system of pages and indirection), but a pagefile is a common part of an OS using virtual memory.

该回答的要点:

  1. 如果没有虚拟内存,让每个进程直接访问RAM,那样就可能造成一个进程可以访问不属于它的RAM内存,从而造成别的进程别破坏。
  2. 采用虚拟地址也可以避免从进程角度的地址不连续问题,虽然可能连续的虚拟地址在RAM地址上是不连续的
  3. 把整个虚拟地址空间,按照4KB的大小分成多个块,这些块就叫做Page。每个Page会被看做一个整体,比如同时映射在RAM上或者磁盘上,同时被设置为只读等
  4. 让每个进程都有自己独立的虚拟地址空间,这样就可以让进程之间相互不干扰,各自的内存都被保护以避免被别的进程污染。虽然这会引入一些问题,比如跨进程的数据分享,但是都是可以解决的
  5. Page即可以被resolving到RAM,也可以被resolving到磁盘。这样就可以根据当前的需要,合理的使用有线的RAM资源。
  6. 对于executables,它们实际上是不需要写入到磁盘的,即避免了从RAM到磁盘的拷贝。因为它们本来就是以文件的形式存在于磁盘上,当需要加载到RAM的时候,直接从对应的文件读取就可以了,即程序源文件本身就可以看做是一种pagefile。而对于data就不行,当它们对应的虚拟内存要被移出RAM的时候,就必须以pagefile的形式保存到磁盘上。
  7. 虚拟内存本身不需要pagefile这个概念,但是通过这个概念,可以更好的利用虚拟内存。

https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_chapter/libc_3.html

Virtual memory and Pagefile

标签:href   back   通过   ram   sof   location   连续   html   运行   

原文地址:https://www.cnblogs.com/willhua/p/13168888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!