简体   繁体   English

在任务管理器上看到分配的 memory

[英]Allocated memory seen on Task Manager

I want to allocate 1 GB of memory.我想分配 1 GB 的 memory。 Assuming int takes 4 bytes, to allocate 1 GB I need an array of 250000000 elements:假设 int 需要 4 个字节,要分配 1 GB,我需要一个包含 250000000 个元素的数组:

int main() { 
    string s;

    cout << "Program paused" << flush;
    getline(cin, s);
    int * p = new int[250000000]{};
    cout << "Program paused" << flush;
    getline(cin, s);
    delete [] p;
    cout << "Program paused" << flush;
    getline(cin, s);
}

Windows Task Manager show that my program uses 954 MB. Windows 任务管理器显示我的程序使用了 954 MB。 Why is it less than 1 GB?为什么它小于 1 GB?

EDIT: The task manager also shows that OS reserved 979,100 MB of virtual memory for the process what is still less than 1 GB.编辑:任务管理器还显示操作系统为进程保留了 979,100 MB 的虚拟 memory,但仍小于 1 GB。

1 GB is 1024x1024x1024 bytes (by convention) 1 GB 是 1024x1024x1024 字节(按照惯例)

So 250000000 x 4 is less than 1GB.所以 250000000 x 4 小于 1GB。

So you are using (250000000 x 4) / (1024x1024) MB which is about 953 MB所以你正在使用 (250000000 x 4) / (1024x1024) MB 大约 953 MB

250,000,000 bytes * 4 is not 1GB. 250,000,000 bytes * 4不是1GB。 Remember a Gigabyte ( more accurately, a Gibibyte ) is 1024 megabytes (mebibytes), and each mebibyte is 1024 kilobytes (kibibytes) which is 1024 bytes.记住一个千兆字节(更准确地说,一个千兆字节)是 1024 兆字节(mebibytes),而每个兆字节是 1024 千字节(kibibytes),也就是 1024 字节。

To allocate 1 GiB (assuming sizeof(int) == 4 ) you want:要分配 1 GiB(假设sizeof(int) == 4 ),您需要:

const size_t len = ( 1024 * 1024 * 1024 ) / sizeof(int); // 268,435,456 
int* p = new int[ len ]{};

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

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