简体   繁体   English

为什么C ++全局变量不会影响程序的内存使用

[英]Why the C++ global variable not affect to memory usage of program

In my program I declare an initialized global variable (as an array). 在我的程序中,我声明了一个初始化的全局变量(作为数组)。 But it only affects the size of executable file, the memory usage by the program was not affected. 但它只影响可执行文件的大小,程序的内存使用量不受影响。

My program is like that 我的程序就是这样

char arr[1014*1024*100] = {1};

int _tmain(int argc, _TCHAR* argv[])
{
    while (true)
    {

    }
    return 0;
}

Size of executable file is 118MB but memory usage when running program was only 0.3MB 可执行文件的大小为118MB,但运行程序时的内存使用量仅为0.3MB

Can anyone explain for me? 有人能为我解释一下吗?

Most operating systems used demand-paged virtual memory. 大多数操作系统使用按需分页的虚拟内存。

This means that when you load a program, the executable file for that program isn't allow loaded into memory immediately. 这意味着当您加载程序时,该程序的可执行文件不允许立即加载到内存中。 Instead, virtual memory pages are set up to map the file to memory. 而是设置虚拟内存页面以将文件映射到内存。 When (and if) you actually refer to an address, that causes a page fault, which the OS then handles by reading the appropriate part of the file into physical memory, then letting the instruction re-execute. 何时(以及如果)实际引用地址会导致页面错误,操作系统随后通过将文件的相应部分读入物理内存来处理,然后让指令重新执行。

In your case, you don't refer to arr , so the OS never pulls that data into memory. 在您的情况下,您不参考arr ,因此操作系统永远不会将该数据提取到内存中。

If you were to look at the virtual address space used by your program (rather than the physical memory you're apparently now looking at), you'd probably see address space allocated for all of arr . 如果您要查看程序使用的虚拟地址空间(而不是您现在正在查看的物理内存),您可能会看到为所有arr分配的地址空间。 The virtual address space isn't often very interesting or useful to examine though, so most things that tell you about memory usage will tell you only about the physical RAM being used to store actual data, not the virtual address space that's allocated but never used. 虽然虚拟地址空间通常不是非常有趣或有用,但是大多数告诉您内存使用情况的事情只会告诉您用于存储实际数据的物理RAM,而不是分配但从未使用过的虚拟地址空间。

Even if you do refer to the data, the OS can be fairly clever: depending on how often you refer to the data (and whether you modify it), only part of the data may ever be loaded into RAM at any given time. 即使您确实参考了数据,操作系统也可能相当聪明:根据您引用数据的频率(以及是否修改数据),在任何给定时间,只有部分数据可能会被加载到RAM中。 If it's been modified, the modified portions can be written to the paging file to make room in RAM for data that's being used more often. 如果它被修改,可以将修改后的部分写入页面文件,以便在RAM中为更频繁使用的数据腾出空间。 If it's not modified, it can be discarded (because the original data can be re-loaded from the original file on disk whenever it's needed). 如果它没有被修改,它可以被丢弃(因为原始数据可以在需要时从磁盘上的原始文件重新加载)。

The reason your memory in use while your executable is executing is significantly smaller than the space required on your hard-drive (or solid-state drive) to store the executable is because you're not pulling the array itself into memory. 在执行可执行文件时使用内存的原因远远小于硬盘驱动器(或固态驱动器)上存储可执行文件所需的空间,因为您没有将数组本身拉入内存。

In your program, you never access or call your array—let alone bring into memory all at once in parallel. 在你的程序中,你永远不会访问或调用你的数组 - 更不用说同时并入内存了。 Because of that, the memory needed to run your executable is incredibly small when compared to the size of the executable (which has to store your massively large array). 因此,与可执行文件的大小(必须存储大型数组)相比,运行可执行文件所需的内存非常小。

I hope that makes sense. 我希望这是有道理的。 The difference between the two is that one is executing and one is stored on your computer's internal disk. 两者之间的区别在于一个正在执行,一个存储在计算机的内部磁盘上。 Something is only brought into execution when it's brought into memory. 只有当它被带入记忆时才会被执行。

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

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