MSVC 放弃了模板繁重的代码,并显示“致命错误 C1060:编译器堆空间不足”

发布于 2024-10-10 17:09:43 字数 166 浏览 2 评论 0原文

我正在尝试使用 MSVC (2010) 编译一些相对模板较多的代码,但它最终因致命错误 C1060:编译器堆空间不足而退出。

整个事情只是一个翻译单元,相比之下,gcc 可以轻松处理它(在虚拟机内,资源少得多)。

有什么提示要寻找什么吗?有相关的编译器选项吗?

I am trying to compile some relatively template heavy code with MSVC (2010), and it eventually quits with fatal error C1060: compiler is out of heap space.

The whole thing is just one translation unit, and, in comparsion, gcc handles it quite easily (inside a VM, with significantly fewer resources).

Any hints what to look for? Are there any relevant compiler options?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

尬尬 2024-10-17 17:09:43

也许可以通过将 /Zm 选项显式添加到项目的 C/C++ 选项来解决这个问题。例如/Zm256

You might be able to counter this by explicitly adding the /Zm option to the C/C++ options of your project. e.g. /Zm256

在巴黎塔顶看东京樱花 2024-10-17 17:09:43

请参阅页面,了解使用 /Zm 编译器选项的可能解决方案,如下莫达柴提议道。
您可能想尝试降低预编译头的限制,而不是使用更高的限制,以便系统有更多的可用内存。

See this page for the possible solution with the /Zm compiler option, as Mordachai suggested.
Instead of using higher limit for the precompiled header, you may want to try lowering it, so the system has more free memory avaible.

池木 2024-10-17 17:09:43

通过减少模板上的模板参数数量,我能够使用适度的模板代码解决 C1060 错误。例如,如果 Foo 需要三种类型:

template< typename T1, typename T2, typename T3 > struct Foo
{
  T1 t1;
  T2 t2;
  T3 t3;
};

Foo< int, char, bool > foo;

则将这些类型封装到单个结构中,并使用该结构作为参数。

template< typename T_ARG > struct Foo
{
  typename T_ARG::T1 t1;
  typename T_ARG::T2 t2;
  typename T_ARG::T3 t3;
};

struct FooArgs
{
  typedef int  T1;
  typedef char T2;
  typedef bool T3;
};

Foo< FooArgs > foo;

如果需要专业化,则考虑:

  • 将专业化行为推入策略并从中继承。
  • 将需要专门研究的参数保留在参数列表中。
  • 将类型链接到类型列表中。富<类型列表< int, 类型列表<字符,类型列表<布尔值,NullType > > > >。

I was able to resolve a C1060 error with moderate template code by reducing the amount of template arguments on my templates. For example, if Foo expects three types:

template< typename T1, typename T2, typename T3 > struct Foo
{
  T1 t1;
  T2 t2;
  T3 t3;
};

Foo< int, char, bool > foo;

Then encapsulate the types into a single struct, and use that struct as the argument.

template< typename T_ARG > struct Foo
{
  typename T_ARG::T1 t1;
  typename T_ARG::T2 t2;
  typename T_ARG::T3 t3;
};

struct FooArgs
{
  typedef int  T1;
  typedef char T2;
  typedef bool T3;
};

Foo< FooArgs > foo;

If specialization is required, then consider:

  • Push specialized behavior into policies and inherit from them.
  • Keep arguments on which you need to specialize in the argument list.
  • Chain the types into a typelist. Foo< TypeList< int, TypeList< char, TypeList< bool, NullType > > > >.
ぃ弥猫深巷。 2024-10-17 17:09:43

如果有人遇到此问题,这里有一个可能的解决方案:

此错误的一个潜在原因是 cl.exe 遇到了 32 位版本的 Windows 中的 2 GB 应用程序内存限制。通常,Windows 将 4 GB 地址空间从中间分割,为操作系统提供 2 GB,为应用程序提供 2 GB。您可以将其更改为 1/3 分割。在 Windows 7 和 Vista 上,以管理员身份在命令提示符中运行以下命令:

bcdedit /设置IncreaseUserVa 3072

然后重新启动计算机。现在,MSVC 有 3 个 gig 可以使用,而不是 2 个。

这样做只是让我编译了一个由于错误 C1060 而失败的项目。根据资源监视器的显示,cl.exe 消耗的内存略高于 2 GB。在常规地址空间安排下它会失败。

将操作系统限制为仅 1 GB 可能会对计算机日常使用过程中的性能产生不利影响。要将分割更改回 2/2,请运行以下命令:

bcdedit /删除值IncreaseUserVa

In case anyone run into this problem, here's a possible solution:

One potential cause of this error is cl.exe is bumping against the 2 gig application memory limit in 32-bit version of Windows. Normally Windows split the 4 gig address space right in the middle, giving 2 gig to the OS and 2 gig to an application. You can change that to a 1/3 split instead. On Windows 7 and Vista, run the following in a command prompt as an administrator:

bcdedit /set IncreaseUserVa 3072

Then restart the computer. Now MSVC has 3 gig to work with instead of 2.

Doing so has just allowed me compile a project that was failing due to error C1060. According to Resource Monitor, cl.exe was consuming just above 2 gig of memory. It would have failed under the regular address space arrangement.

Limiting the OS to just 1 gig can have adverse impact on performance during everyday use of your computer. To change the split back to 2/2, run the following:

bcdedit /deletevalue IncreaseUserVa

她说她爱他 2024-10-17 17:09:43

我有一个类似的问题(也是模板重),并且我已经使用 /Zm1000 来编译我的代码(最初有效)。然而,在清理代码、将长函数划分为较小的函数、将内容放入命名空间等之后,编译器会吐出错误消息:

致命错误 C1060:编译器堆空间不足。

启动后立即执行,没有任何延迟(实际上似乎没有编译任何内容)。起初,我很困惑,因为我有 32 GB 的交换空间,但当时只使用了大约 6.1 GB。我还运行 x64 操作系统,因此应该有足够的内存和交换空间供每个人使用。

我参考了MSDN并发现我实际上需要降低/Zm800,现在效果很好。我的理解是,为预编译头缓冲区占用所有堆空间实际上会锁定内存空间;因此,使用 /Zm2000 将使 32 位编译器无法为其他内容动态分配内存(它在某种程度上也需要,这使得 /Zm 选项完全荒谬 - 使用谨慎)。

我正在使用 MSVC 6.0,但我希望这对 2010 年也有帮助。

I had a similar issue (also template-heavy), and I was already using /Zm1000 to compile my code (which worked initially). However, after cleaning-up the code, dividing long functions to smaller ones, putting stuff to namespaces / etc., the compiler would spit out the error message:

fatal error C1060: compiler is out of heap space.

right after starting, without any delay (not actually seeming to compile anything). At first, I was confused, as I have 32 GB of swap space and only about 6.1 GB was used at the time. I'm also running x64 OS, so there should be a plenty of memory and swap for everyone.

I referred to MSDN and found out that I actually needed to lower to /Zm800 and now it works great. My understanding is that taking up all heap space for the precompiled header buffer actually locks out the memory space; so using /Zm2000 would leave a 32-bit compiler without means to dynamically allocate memory for other stuff (which it somehow also needs, making the /Zm option completely ridiculous - use with caution).

I'm using MSVC 6.0, but I hope this helps in 2010 as well.

束缚m 2024-10-17 17:09:43

(VC2022 Community x64) 当使用深度 #define 嵌套实例化具有大量不同参数的模板树时,我的 C1060 由 2 个问题触发。 /Zm[x] 没有帮助。

它需要两个修复:

  1. 更改 Project->; C++ -> 预处理器 ->使用标准一致预处理器选择是(/Zc:preprocessor)

(没有它,C1060 几乎直接死掉,没有编译太多)。

  1. 之后,cl.exe 编译但最终耗尽内存(在我的设置中大约为 35GB),然后仍然导致 C1060。分配更多交换空间修复了它。

(VC2022 Community x64) my C1060 was triggered by 2 problems when using deep #define nesting to instantiate a tree of templates with lots of different arguments. /Zm[x] didn't help.

it required two fixes:

  1. change Project-> C++ ->Preprocessor -> Use Standard Conforming Preprocessor to Yes (/Zc:preprocessor).

(without it died with C1060 almost straight away without compiling much).

  1. after that, cl.exe compiles but eventually runs out of of memory (at around 35GB on my setup), and then still causes C1060. allocating more swap space fixed it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文