简体   繁体   English

C ++中的全局变量内存分配

[英]Global variables memory allocation in C++

I have this test code: 我有以下测试代码:

#include <thread>
#include <cstdint>
#include <cstdlib>

int32_t global_buffer[1024][1024][256];

int main()
{
    std::this_thread::sleep_for(std::chrono::seconds(5));

    for (size_t i = 0; i < 1024; ++i)
        for (size_t j = 0; j < 1024; ++j)
        for (size_t k = 0; k < 256; ++k)
                global_buffer[i][j][k] = rand();

    std::this_thread::sleep_for(std::chrono::seconds(5));

    int32_t* heap_buffer = new int32_t[1024 * 1024 * 256];
    for (size_t i = 0; i < 1024 * 1024 * 256; ++i)
        heap_buffer[i] = rand();

    std::this_thread::sleep_for(std::chrono::seconds(5));

    delete[] heap_buffer;

    std::this_thread::sleep_for(std::chrono::seconds(5));
}

If I run the program, compiled with VS2013 on 32bits, on Windows 7 I have the following memory behavior: 如果我在Windows 7上运行32位VS2013编译的程序,则我将具有以下内存行为:

  1. memory usage is very low, several KB in the first ~5 seconds 内存使用率非常低,在最初的约5秒内只有几KB
  2. memory usage increases in time until it hits ~1 GB 内存使用量会随着时间增加,直到达到〜1 GB
  3. memory usage stays ~1 GB for 5 seconds 内存使用率持续约1 GB,持续5秒
  4. memory usage jumps to ~2 GB and stays there for ~5 second 内存使用量跃升至〜2 GB,并停留约5秒
  5. memory usage jumps to ~1 GB 内存使用量跃升至〜1 GB

As I don't understand why 1. and 2. are happening I have several questions: 我不明白为什么1.和2.会发生,所以我有几个问题:

  1. Why doesn't the program start with ~1 GB memory usage? 为什么程序不以〜1 GB的内存使用量开头?
  2. Is memory for global variables allocated on demand? 是否按需分配用于全局变量的内存?
  3. Is this a VS/Windows specific behavior or other compilers/OS have the same behavior? 这是VS / Windows特定的行为还是其他编译器/ OS具有相同的行为?
  4. Is this behavior correct? 这种行为正确吗? The std says: 性病说:

3.7.1 Static storage duration 3.7.1静态存储期限

All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. 所有没有动态存储持续时间,没有线程存储持续时间并且不是本地变量的变量都具有静态存储持续时间。 The storage for these entities shall last for the duration of the program 这些实体的存储应在程序期间持续进行

3.6.2 Initialization of non-local variables 3.6.2非局部变量的初始化

  1. There are two broad classes of named non-local variables: those with static storage duration (3.7.1) and those with thread storage duration (3.7.2). 有两大类命名的非局部变量:具有静态存储持续时间(3.7.1)的变量和具有线程存储持续时间(3.7.2)的变量。 Non-local variables with static storage duration are initialized as a consequence of program initiation. 具有静态存储持续时间的非局部变量将由于程序启动而初始化。 Non-local variables with thread storage duration are initialized as a consequence of thread execution. 具有线程存储持续时间的非局部变量由于线程执行而被初始化。 Within each of these phases of initiation, initialization occurs as follows. 在初始化的每个阶段中,初始化发生如下。

  2. Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place. 在进行任何其他初始化之前,应将具有静态存储持续时间(3.7.1)或线程存储持续时间(3.7.2)的变量进行零初始化(8.5)。

Is memory for global variables allocated on demand? 是否按需分配用于全局变量的内存?

Linkers store only initialized data in the binaries. 链接器仅在二进制文件中存储初始化的数据。 Space for uninitialized data with static storage duration is allocated at run-time by the run-time linker, and even then the physical memory is committed only when you start accessing that data. 运行时链接程序会在运行时为具有静态存储持续时间的未初始化数据分配空间,即使这样,也只有在您开始访问该数据时才提交物理内存。 See data segment for more details. 有关更多详细信息,请参见数据段

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

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