3

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?

uj2
  • 2,255
  • 2
  • 21
  • 32
  • 1
    Don't use ridiculous templates? – Adam Rosenfield Jan 05 '11 at 22:13
  • I've had such error on an older MSVC version with a very simple template, but that was due to a mistake in my code what is apparently not your case – Roman L Jan 05 '11 at 22:17
  • 2
    you might have infinite (deep) recursion in template instantiations – Gene Bushuyev Jan 05 '11 at 22:18
  • Have you tried the VS 2010 SP1 beta? Perhaps it fixes the issue (though it could be infinite recursion, as Gene suggests) – Mordachai Jan 05 '11 at 22:20
  • 1
    @Gene: As said, it compiles with gcc, so it's not the case. I think MSVC also has an appropriate message for too deeply nested templates. – uj2 Jan 05 '11 at 22:24
  • It might help to post your template code. – John Dibling Jan 05 '11 at 22:34
  • @uj2 -- each compiler has its own limits on recursion, so the fact it compiles with gcc doesn't mean it's not the reason. There was also a bug in VC with premature instantiation which also could cause infinite recursion. So instead of guessing, check your templates for possibility of infinite (deep) recursion. – Gene Bushuyev Jan 05 '11 at 22:38
  • uj2 - that doesn't necessarily mean anything. Maybe your end-point isn't being recognized in MSVC and so it just keeps instantiating a recursive case. – Edward Strange Jan 05 '11 at 22:52
  • Possible duplicate of [How to work around Visual Studio Compiler crashes](http://stackoverflow.com/questions/1388608/how-to-work-around-visual-studio-compiler-crashes) – phuclv Apr 25 '16 at 16:57

6 Answers6

0

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

cleong
  • 7,242
  • 4
  • 31
  • 40
0

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.

the swine
  • 10,713
  • 7
  • 58
  • 100
0

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

Mordachai
  • 9,412
  • 6
  • 60
  • 112
  • Actually, this may not be applicable to you? Try it, but it's documented as "precompiled header memory". We had similar errors until we set this value. But your case may be different? – Mordachai Jan 05 '11 at 22:19
  • Tried /Zm2000, and it diden't help :P Thanks, though. – uj2 Jan 05 '11 at 22:22
0

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.

Jan Holecek
  • 2,131
  • 1
  • 16
  • 26
0

(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.
gl-
  • 41
  • 3
0

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 > > > >.
Tanner Sansbury
  • 51,153
  • 9
  • 112
  • 169