简体   繁体   中英

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.

But then is this page stored on DISK or 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.

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. 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.

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.

Is this page stored on DISK or 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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