虚拟内存概念 [英] virtual memory concepts

查看:110
本文介绍了虚拟内存概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一些有关虚拟内存的话题感到困惑.因此,我将逐点列出它们并提出问题.在回答时,如果您也列出一些我可以消除疑问的资料,我将更愿意.我将参考Linux elf可执行文件进行讨论.

I am confused about some topics regarding virtual memory. So, i am going to pointwise list them and ask questions. While answering i will prefer if you also list some source where i can clear that doubt. I will be talking with reference to a linux elf executable file.

  1. 我听说在32位系统中,每个进程的地址空间均为4gb.当我检查了我的可执行可重定位文件之一的objdump时,我看到它的限制从00000000到ffffffff.它还包含内核空间.这是文件的地址空间. 这是我们所说的虚拟内存吗?如果是,那么我读到虚拟内存机制允许运行非常大的进程,并且该进程的大小不受主内存大小的限制(我们可以要求提供按需分页到主内存的页数). 然后,如果虚拟内存仅为4gb,难道它不会将程序的最大大小限制为4gb吗?另外,我检查了另一个文件的objdump,它具有相同的地址(即,从00000000到ffffffff). 那么,这是什么意思?这是否意味着我们的文件是某种可重定位文件,将再次向其添加起始地址(尽管这看起来很荒谬,因为它已经是可执行的可重定位目标文件了.)

  1. I have heard that every process has the address space of 4gb in a 32 bit system. When i checked the objdump of one of my executable relocatable file i saw that it had limits from 00000000 to ffffffff. Also it contained the kernel space. This is the address space of the file. Is this the virtual memory we talk about? If yes then i had read that virtual memory mechanism allows processes of very big sizes to run and that process size is not limited by main memory size(we can bring required pages to main memory upon demand- demand paging). Then if virtual memory is just 4gb, doesn't it limit the maximum size of programs to 4gb? Also, i checked another file's objdump and it had the same address(i.e. 00000000 to ffffffff). So, what does this mean? Does that mean that our file is some kind of relocatable file to which starting addresses will again be added(although this seems absurd because it is already an executable relocatable object file).

我读到在执行分段的内存中,CPU产生虚拟(逻辑)地址.该地址分为三部分-段,段内的偏移量.另外,这里要讨论的部分是代码,数据,堆栈等.

I had read that in a memory where segmentation has been implemented, the cpu produces virtual(logical) address. This address has three parts - the segment, the offset within the segment. Also, the segments being talked about here are code, data, stack etc.

在过程地址空间中,这些段是从特定位置开始定位的.那么,cpu虚拟地址的内容是什么? 产生的虚拟地址范围是000000000到ffffffff吗?如果是,则正在访问虚拟地址内容的过程是,如下:-

In the process address space, these segments are located starting from specific locations. So, what are the contents of the virtual address of the cpu? Does the virtual address produced range from 00000000 to ffffffff? If yes then is the process of accessing the content at the virtual address, the folllowing:-

The segment part is looked up in the segment descriptor table to find the segment's
starting address in linear address space. Then the offset is indexed within the segment and
the resulting address is the linear address. Then, we look up the page table and map the
address to physical address. If the page is not currently in the main memory, it is 
brought. 

这又一次出现了这样一个事实,即任何时候都无法在主内存中完全充满进程,因为那样一来,整个内存将只被一个进程占用(因为进程的地址空间本身是4gb ).

This again arises the fact that no process can be fully in the main memory at any time, because then entire memory will be occupied by just one process(as the address space of process is itself 4gb).

此外,如果所有进程的地址空间都从00000000到ffffffff,并且一次在主内存中可以存在多个进程,则所有进程都应该有自己的段描述符表,该表将返回段地址.线性地址空间

Also, if all process have address space from 00000000 to ffffffff, and more than one process can exist in main memory at a time, then all processes should have their own segment descriptor table which returns the segment's address in the linear address space

推荐答案

这是一个开放性很强的问题,对不同术语有许多混淆的用法.我将尽力解决您的大部分问题,并提供其他可能有用的信息.

This is a very open-ended question that has many confused uses of different terms. I'll try to address as much of your question as I can, and provide some other useful information that may help.

  1. 我听说在32位系统中,每个进程的地址空间均为4gb."并非完全正确.在32位系统中,每个进程的最大可寻址空间为3.2GB.这并不意味着该内存曾经被分配过,并且肯定不会在进程启动时立即分配它. 这是我们谈论的虚拟内存吗?"否.虚拟内存与进程的可寻址空间没有直接关系.稍后会对此进行更多介绍.

  1. "I have heard that every process has the address space of 4gb in a 32 bit system." Not precisely true. Every process has a maximum addressable space of 3.2GB in a 32-bit system. That doesn't mean that this memory is ever allocated, and it certainly isn't allocated as soon as a process launches. "Is this the virtual memory we talk about?" No. Virtual memory is nothing directly to do with the addressable space of a process. More on this later.

由于我将在下面解释的原因,这个问题实际上没有任何意义.但是,值得注意的是,多个进程一次很容易地装入内存,因为这些进程不会自动分配其全部潜在的可用内存. (如果文本编辑器一打开就分配了4GB的内存,它将不会成为流行的文本编辑器!)

This question doesn't really make sense, for reasons I will explain below. It's worth noting, though, that multiple processes clealy do fit in memory at one time, because the processes don't automatically allocate their full potentially-available memory. (If a text editor allocated 4GB of memory as soon as it was opened, it would not be a popular text editor!)

我不是专家,但是我非常怀疑每个程序在运行时都有自己的内核代码副本.仅安全性和性能问题就使它成为不太可能的解决方案.

I'm no expert, but I highly doubt that every program has its own copy of kernel code at runtime. The security and performance issues alone make this a very unlikely solution.

现在,一些定义可能会对您有所帮助.

So now, some definitions that may help you.

  • 物理内存(通常是!)是PC中的RAM.运行任何程序时,CPU可以直接使用快速的物理内存.当您指定物理内存地址时,您将根据内存硬件本身在内存中指定确切位置.
  • 虚拟内存(通常是!)存储在较慢的介质(如硬盘驱动器)上(通常称为页面文件).当计算机的内存不足以运行进程时,它将通常从空闲或后台应用程序中将某些当前物理内存内容复制到页面文件中.这样可以在物理内存中留出空间,以便可以运行活动进程.如果不再存在于物理内存中的程序需要处理数据,则必须将其数据从页面文件重新加载到物理内存中,这又可能需要将另一个程序分页到物理内存中.腾出空间.术语虚拟"内存与物理"内存用于强调该内存确实不存在,但仍可供计算机使用.就性能而言,虚拟内存的使用非常昂贵,但是它可以支持更大的大小:确实,可能有任意数量的可用虚拟内存,但是性能受到影响,这使它成为超出某些限制的实用解决方案. /li>
  • 逻辑内存地址是单个进程使用的地址,它使进程可以寻址自己的内存,而不必关心进程在物理内存中的加载位置.您的00000000fffffff范围是该进程可用的逻辑范围,并且这是将在该进程内用于引用内存的地址.内核会根据进程内存的物理偏移量(和分段),将其转换为CPU在实际执行代码时使用的物理地址.该物理位置可以位于可用内存空间中的任何位置,并且如果将应用逐页调出和移入,则该物理位置可能会在应用程序的生存期内发生变化.但是,应用程序本身仅需要引用其自己的逻辑地址空间.术语逻辑"与物理"地址用于强调一个地址不是 real 地址,而是相对于内存相关子集的地址,即相对于进程自身的地址内存空间.
  • Physical memory is (typically!) the RAM in your PC. It is fast, physical memory that your CPU works directly with when running any program. When you specify a physical memory address you are specifying an exact position in memory according to the memory hardware itself.
  • Virtual memory is (typically!) stored on slower media like your Hard Disk Drive (in what's often called a paging file). When your computer is running low on memory for running processes, it will copy some of the current physical memory contents to the page file, typically from an idle or background application. This makes room in physical memory so that an active process can run. If a program that is no longer in physical memory needs to process data, its data must be reloaded from the page file into physical memory - which may in turn require another program to be paged out of physical memory to make room. The term "Virtual" versus "Physical" memory is used to highlight that this memory doesn't really exist, but it is nonetheless available to the computer. Virtual memory use is very costly in terms of performance, but it can support much larger sizes: indeed, it is possible to have an arbitrarily large amount of virtual memory available, but the performance hit prevents this being a practical solution beyond certain limits.
  • Logical memory addresses are those used by a single process and allow a process to address its own memory without having to care about where in physical memory the process has been loaded. Your 00000000 to fffffff range is the logical range available to the process, and this is the address that will be used within the process to reference memory. The kernel will translate this to a physical address that is used by the CPU when actually executing code, based on the physical offset (and segmentation) of the process' memory. This physical location could be located anywhere in the available memory space and, if the aplication is paged out and in, the physical location may change during the lifetime of the application. However, the application itself need only ever refer to its own logical address space. The term "logical" versus "physical" address is used to highlight that an address is not the real address, but is the address relative to the relevant subset of memory - that is, to the process' own memory space.

我不是这方面的专家,但我希望这有助于澄清您的一些问题.

I'm no expert on this, but I hope this helps clarify some of your questions.

这篇关于虚拟内存概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆