RAM如何分配? [英] How is RAM allocated?

查看:221
本文介绍了RAM如何分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有关计算机科学的一个新手问题:ram是如何分配的?

This is a noob question about computer science: How is ram allocated?

例如,我使用Windows.我可以知道程序使用了哪些地址吗? Windows如何分配内存?连续还是不连续? 在Linux操作系统上是同一回事吗?

Fo example, I use Windows. Can I know which adresses are used by a program? How does Windows allocate memory? Contiguous or non contiguous? Is it the same thing on Linux OSes ?

而且,我可以通过程序访问整个ram吗? (我不相信,但是...)

And, can I have access to the whole ram with a program? (I don't believe in that, but...)

您知道与此相关的任何出色的讲座/文档吗?

Do you know any good lectures/documentation on this?

推荐答案

首先,当您认为正在分配RAM时,您实际上不是.我知道这很令人困惑,但是一旦您了解了它的工作原理,它实际上并不复杂.继续阅读.

First, when you think you are allocating RAM, you really are not. This is confusing, I know, but it's really not complicated once you understand how it works. Keep reading.

RAM由操作系统以称为页面"的单位分配.通常,这意味着4kiB的连续区域,但是其他大小也是可能的(更复杂的是,现代处理器上支持大页面"(通常为1-4MiB)),并且操作系统可能会有分配粒度不同于页面大小,例如Windows的页面大小为4kiB,粒度为64kiB).
让我们忽略这些额外的细节,只考虑具有一种特定大小(4KiB)的页面".

RAM is allocated by the operating systems in units called "pages". Usually, this means contiguous regions of 4kiB, but other sizes are possible (to complicate things further, there exists support for "large pages" (usually on the order of 1-4MiB) on modern processors, and the operating system may have an allocation granularity different from the page size, for example Windows has a page size of 4kiB with a granularity of 64kiB).
Let's ignore those additional details and just think of "pages" that have one particular size (4KiB).

如果您分配和使用的区域大于系统页面的大小,通常没有具有连续的内存,但是您仍然视为连续的,因为您的程序只能思考"虚拟地址.实际上,您可能正在使用两个(或多个)根本不连续的页面,但看起来似乎是连续的.这些虚拟地址由MMU透明地转换为实际地址.
此外,并非您认为分配的所有内存始终都一定存在于RAM中,并且相同的虚拟地址可能在不同的时间对应于完全不同的RAM(例如,当页面换出并随后又换入时) -您的程序将在相同的地址看到它,但实际上它很可能在不同的RAM中.

If you allocate and use areas that are greater than the system's page size, you will usually not have contiguous memory, but you will nevertheless see it as contiguous, since your program can only "think" in virtual addresses. In reality you may be using two (or more) pages that are not contiguous at all, but they appear to be. These virtual addresses are transparently translated to the actual addresses by the MMU.
Further, not all memory that you believe to have allocated necessarily exists in RAM at all times, and the same virtual address may correspond to entirely different pieces of RAM at different times (for example when a page is swapped out and is later swapped in again -- your program will see it at the same address, but in reality it is most likely in a different piece of RAM).

虚拟内存是一种非常强大的工具.虽然您程序中的一个地址只能(最多)引用RAM中的一个物理地址(在特定页面中),但RAM的一个物理页可以映射到程序中的几个不同地址,并且即使在几个独立的程序中.
例如,可以创建圆形"内存区域,并且共享库中的代码通常被加载到一个内存位置,但同时被许多程序使用(并且在这些不同的程序中它将具有不同的地址).或者,您可以使用该技术在程序之间共享内存,这样,当一个程序写入某个地址时,另一个程序的内存位置中的值就会更改(因为它是完全相同的内存!).

Virtual memory is a very powerful instrument. While one address in your program can only refer to [at most] one physical address (in a particular page) in RAM, one physical page of RAM can be mapped to several different addresses in your program, and even in several independent programs.
It is for example possible to create "circular" memory regions, and code from shared libraries is often loaded into one memory location, but used by many programs at the same time (and it will have different addresses in those different programs). Or, you can share memory between programs with that technique so when one program writes to some address, the value in the other program's memory location changes (because it is the exact same memory!).

在较高的级别上,您要求标准库提供内存(例如malloc),并且标准库以某种或多或少未指定的方式来管理已保留的区域池(有许多不同的分配器实现,它们的共同点是您可以要求它们提供内存,并且它们还提供一个地址-这就是您认为在不分配RAM的地方.
当分配器需要更多内存时,它会要求操作系统保留另一个块.在Linux下,可能是sbrkmmap,在Windows下,可能是VirtualAlloc.

On a high level, you ask your standard library for memory (e.g. malloc), and the standard library manages a pool of regions that it has reserved in a more or less unspecified way (there are many different allocator implementations, they all have in common that you can ask them for memory, and they give back an address -- this is where you think that you are allocating RAM when you are not).
When the allocator needs more memory, it asks the operating system to reserve another block. Under Linux, this might be sbrk and mmap, under Windows, this would for example be VirtualAlloc.

通常,您可以对内存执行三件事,并且在Linux和Windows(以及所有其他现代OS)下,内存通常可以相同,尽管所使用的API函数有所不同,并且还有一些细微的差异.

Generally, there are 3 things you can do with memory, and it generally works the same under Linux and Windows (and every other modern OS), although the API functions used are different, and there are a few more minor differences.

您可以保留它,除了在逻辑上划分您的地址空间(仅您的进程在乎这一点)之外,它几乎不执行任何操作.
接下来,您可以 commit ,这并没有太大的作用,但是会在一定程度上影响其他进程.系统对可以为所有进程分配的内存有一个总限制(物理RAM加页面文件大小),并且它会对此进行跟踪.这意味着您提交的内存与另一个进程可以提交的限制相同.否则,再也不会发生什么事情.
最后,您可以访问内存.最后,这具有明显的作用.首次访问页面时,会发生错误(因为该页面根本不存在!),并且操作系统要么从文件中获取某些数据(如果该页面属于映射),要么清除某些页面(可能在第一次访问后)将其保存到磁盘).然后,操作系统会调整虚拟内存系统中的结构,以便在您访问的地址上看到RAM的此物理页面.

You can reserve it, this does more or less nothing, apart from logically dividing up your address space (only your process cares about that).
Next, you can commit it, this again doesn't do much, but it somewhat influences other processes. The system has a total limit of how much memory it can commit for all processes (physical RAM plus page file size), and it keeps track of that. Which means that memory you commit counts against the same limit that another process could commit. Otherwise, again, not much happens.
Last, you can access memory. This, finally, has a noticeable effect. Upon first accessing a page, a fault occurs (because the page does not exist at all!), and the operating system either fetches some data from a file (if the page belongs to a mapping) or it clears some page (possibly after first saving it to disk). The OS then adjusts the structures in the virtual memory system so you see this physical page of RAM at the address you accessed.

从您的角度来看,这些都不可见.就像魔术一样.

From your point of view, none of that is visible. It just works as if by magic.

可以检查进程以了解其地址空间中使用了哪些区域,并且有可能(但毫无意义)将其转换为物理地址.请注意,在不同时间运行的同一程序可能会存储例如一个位于不同地址的特定变量.在Windows下,例如,您可以使用 VMMap 工具来检查进程内存分配.

It is possible to inspect processes for what areas in their address space are used, and it is possible (but kind of meaningless) to translate this to physical addresses. Note that the same program run at different times might store e.g. one particular variable at a different address. Under Windows, you can for example use the VMMap tool to inspect process memory allocation.

如果编写自己的操作系统,则只能使用所有RAM,因为OS总是保留很少的内存,用户进程无法使用.
否则,原则上您可以使用[几乎]所有内存.但是,能否直接使用 取决于您的进程是32位还是64位.如今的计算机通常具有比32位地址更多的RAM,因此您要么需要使用地址窗口扩展名,要么您的进程必须是64位.同样,即使给定的RAM量原则上可以使用32位寻址,某些地址空间因素(例如fragentation,内核保留)也可能会阻止您直接 使用所有内存.

You can only use all RAM if you write your own operating system, since there is always a little memory which the OS reserves that user processes cannot use.
Otherwise you can in principle use [almost] all memory. However, whether or not you can directly use that much depends on whether your process is 32 or 64 bits. Computers nowadays typically have more RAM than you can address with 32 bits, so either you need to use address windowing extensions or your process must be 64 bits. Also, even given an amount of RAM that is in principle addressable using 32 bits, some address space factors (e.g. fragentation, kernel reserve) may prevent you from directly using all memory.

这篇关于RAM如何分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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