简体   繁体   English

阵列如何存储在虚拟内存中?

[英]How are arrays stored in Virtual Memory?

If you have an array 如果你有一个数组

int arr[100];

How exactly is this stored in a modern machine, which obviously runs using virtual memory? 这如何精确地存储在现代机器中,该机器显然使用虚拟内存运行?

I understand we have to use paging with virtual memory and since an array is a contiguous block, if we have a 4kb page, this array arr will fit into 1 page. 我知道我们必须对虚拟内存使用分页,并且由于数组是连续的块,因此如果我们有4kb的页面,则此数组arr将适合1页。

But then is this page stored on DISK or RAM? 但是,此页面是否存储在DISK或RAM中?

In summary, the data may be in various locations, where it is may change automatically (by actions of the operating system), and, when working within normal programs, you generally do not need to know about it. 总而言之,数据可能在各个位置,这些位置可能会自动更改(通过操作系统的操作),并且在正常程序中工作时,通常不需要了解它。

First, the fact that you declare an array in source code does not necessarily mean the full array, or any array at all, is created in memory. 首先,您在源代码中声明一个数组的事实并不一定意味着在内存中创建了完整的数组或根本没有任何数组。 A compiler may optimize the source code in ways that eliminate part or all of the array. 编译器可以通过消除部分或全部数组的方式来优化源代码。

However, let us suppose the array is actual created. 但是,让我们假设该数组是实际创建的。 Virtual memory is created to present an illusion that a process has exclusive use of physical memory and/or that it has more memory than is actually available as physical memory. 创建虚拟内存是为了使人产生一种幻想,即一个进程专用于物理内存和/或它具有的内存比实际用作物理内存的内存更多。 A purpose of this illusion is that processes should not have to be concerned about where their data actually is. 这种错觉的目的是使流程不必关心其数据的实际位置。 And the vast majority of normal processes can neglect that. 绝大多数正常过程可以忽略这一点。

Also, for the most part, compilers will pay little attention to where small objects are placed relative to page boundaries. 同样,在大多数情况下,编译器将很少注意相对于页面边界放置小对象的位置。 It usually will make little difference whether an array of 100 int is placed so that it is entirely within one page or that it spans a page boundary. 通常,将100 int数组放置在整个页面内还是跨越页面边界,几乎没有什么区别。

When it is necessary to know or influence where the data actually is, a number of issues come into play. 当有必要了解或影响数据的实际位置时,就会出现许多问题。

There are ways to affect the location of data relative to page boundaries, either by using system or library calls for that purpose or by allocating excess memory and then putting the data at a chosen location within it. 有多种方法可以影响数据相对于页面边界的位置,方法是为此目的而使用系统或库调用,或者通过分配过多的内存,然后将数据放在其中的选定位置来进行。

If data is important and is preferred or required to remain in physical memory, there may be system calls (depending on the system, of course) to request that. 如果数据很重要并且需要或希望保留在物理内存中,则可能会有系统调用(当然取决于系统)来请求。

In the absence of such specific requests, where data is located depends on a number of factors. 在没有此类特定请求的情况下,数据的位置取决于许多因素。 If you declare a static array of int and initialize it with compile-time data, the data may appear in a section of the executable file that is ultimately generated. 如果声明一个int静态数组,并使用编译时数据对其进行初始化,则该数据可能会出现在最终生成的可执行文件的一部分中。 In some systems, when an executable file is started, the system does not load the entire file into memory. 在某些系统中,启动可执行文件时,系统不会将整个文件加载到内存中。 It loads various portions of data from the executable file only when they are referenced. 仅在引用可执行文件时,它才会从可执行文件中加载数据的各个部分。 Thus, this data may initially reside on disk. 因此,该数据最初可能驻留在磁盘上。 After it is loaded into memory, if the system is burdened with other things that need memory, the system may discard this data from memory, so that it again exists only in the executable file on disk. 在将数据加载到内存中之后,如果系统负担了其他需要内存的事情,则系统可能会从内存中丢弃此数据,从而使其仅再次存在于磁盘上的可执行文件中。

On the other hand, if the data is generated during program execution, it is of course in memory when the program generates it. 另一方面,如果数据是在程序执行期间生成的,则当程序生成数据时,它当然在内存中。 Again, though, if the system is burdened with other demands, the system may remove the data from memory. 同样,如果系统负担其他要求,则系统可能会从内存中删除数据。 In this case, since the data does not already exist on disk (as the data in the executable file did), the data is first written to a page or swap file on disk. 在这种情况下,由于数据在磁盘上不存在(就像可执行文件中的数据一样),因此首先将数据写入磁盘上的页面或交换文件。

Generally, where the data is may change over time. 通常,数据的位置可能会随时间变化。

I understand we have to use paging with virtual memory. 我了解我们必须对虚拟内存使用分页。

You don't have to using paging. 不必使用分页。 You can turn paging off. 您可以关闭分页。

If we have a 4kb page, this array arr will fit into 1 page. 如果我们有一个4kb的页面,则此数组arr将适合1页。

It may fit in a 4kb page, but it's very likely not going be aligned on (or near) a 4kb boundary, so in reality, it'll probably be stored across two pages. 它可能适合4kb页面,但很可能不会在4kb边界上(或附近) 对齐 ,因此实际上,它可能会存储在两个页面上。

Is this page stored on DISK or RAM? 此页面存储在DISK还是RAM中?

Arrays are no different from other data structures. 数组与其他数据结构没有什么不同。 They are stored in memory (RAM), unless swapped out to DISK by the OS, if it is running low on memory. 它们存储在内存(RAM)中,除非OS内存不足,除非被OS换成DISK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM