使用大型Object数组的极端内存使用情况[重复]

时间:2021-08-18 00:13:04

This question already has an answer here:

这个问题在这里已有答案:

Why is it that when compiling Sample 1, it uses all my RAM and crashes my computer yet Sample 2 compiles instantly without doing so?

为什么在编译Sample 1时,它会占用我的所有内存并崩溃我的计算机,而Sample 2会立即编译而不会这样做?

Sample 1:

class Foo
{
    int a = 0;
};

class Test
{
    Foo foo[4000000] = {};
};

int main()
{
    Test t;
}

Sample 2:

class Foo
{
    int a = 0;
};

int main()
{
    Foo foo[4000000] = {};
}

Lastly, is there any way to stop Sample 1 from using tons of RAM when compiling? I'm using gcc version 5.3.0 and I compiled the above with -std=c++11. Note that class Test should only require a mere 16 MB of memory.

最后,有没有办法阻止样本1在编译时使用大量的RAM?我正在使用gcc版本5.3.0,我用-std = c ++ 11编译了上面的内容。请注意,类Test应该只需要16 MB的内存。

For any

1 个解决方案

#1


-2  

This is definitely a bug. I can reproduce this with 5.3 on my system. RAM usage rapidly increases, but I closed the program because I don't want my system to crash. On the other hand, if I compile it in Clang 3.8, it compiles almost instantly.

这绝对是个错误。我可以在我的系统上使用5.3重现这一点。 RAM使用率迅速增加,但我关闭了程序,因为我不希望我的系统崩溃。另一方面,如果我在Clang 3.8中编译它,它几乎立即编译。

I suggest reporting this to gcc.gnu.org/bugzilla. As indicated here take a look at bug reports 59659, 68203, and 56671. I'm pretty sure they all point to the same problem of GCC's inability to have a large array of non-trivial class type.

我建议将此报告给gcc.gnu.org/bugzilla。如下所示,请查看错误报告59659,68203和56671.我很确定它们都指向GCC无法拥有大量非平凡类类型的同一问题。

#1


-2  

This is definitely a bug. I can reproduce this with 5.3 on my system. RAM usage rapidly increases, but I closed the program because I don't want my system to crash. On the other hand, if I compile it in Clang 3.8, it compiles almost instantly.

这绝对是个错误。我可以在我的系统上使用5.3重现这一点。 RAM使用率迅速增加,但我关闭了程序,因为我不希望我的系统崩溃。另一方面,如果我在Clang 3.8中编译它,它几乎立即编译。

I suggest reporting this to gcc.gnu.org/bugzilla. As indicated here take a look at bug reports 59659, 68203, and 56671. I'm pretty sure they all point to the same problem of GCC's inability to have a large array of non-trivial class type.

我建议将此报告给gcc.gnu.org/bugzilla。如下所示,请查看错误报告59659,68203和56671.我很确定它们都指向GCC无法拥有大量非平凡类类型的同一问题。