g++ "virtual memory exhausted: Cannot allocate memory"
The compiler tried to allocate more memory than was available and gave up with "virtual memory exhausted". Like an OOM kill, this is a resource ceiling - too much parallelism or a translation unit too heavy for the RAM.
What this error means
A compile aborts with "virtual memory exhausted: Cannot allocate memory". It tends to hit the largest file or the highest -j, and re-running on a bigger machine succeeds unchanged.
virtual memory exhausted: Cannot allocate memory
make[2]: *** [CMakeFiles/app.dir/codegen_all.cpp.o] Error 1Common causes
Insufficient RAM / swap for the compile
On a small runner with little or no swap, a memory-hungry compile exceeds available memory and the allocation fails outright.
High parallelism multiplying memory use
Many concurrent cc1plus processes sum to more than the runner has, so one of them cannot allocate.
How to fix it
Lower parallelism
Fewer concurrent compilers means more memory each.
make -j2
# or
ninja -j2 -C buildAdd memory or swap
Use a larger runner, or add swap as a stopgap. A self-healing CI platform can retry on a higher-memory runner automatically.
# stopgap: add swap on a Linux runner
fallocate -l 4G /swapfile && chmod 600 /swapfile
mkswap /swapfile && swapon /swapfileHow to prevent it
- Size runners by the build’s peak memory, not just CPU count.
- Cap
-jto fit memory. - Split or simplify translation units that dominate memory.