Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ld: fatal error: failed to allocate memory

Tags:

c++

ld

I'm building a 3rd Party app via CMake and I'm passing in -DCMAKE_BUILD_TYPE=MinSizeRel to CMake to attempt to get around this issue but that is not working out.

I always end up with an error when it comes to linking.

/usr/lib/gcc/armv7a-hardfloat-linux-gnueabi/4.9.3/../../../../armv7a-hardfloat-linux-gnueabi/bin/ld: fatal error: clientserver: mmap: failed to allocate 791230396 bytes for output file: Cannot allocate memory

The PC building this has 1gb RAM and a 6gb SWAP. Trying to free that much of the physical ram is proving difficult, so is there a way to get the linker to either use the SWAP or limit what it uses?

I've switched to ld.gold as well just to see if that had any benefit but still no luck. Adding more RAM is unfortunately not an option.

like image 322
Elarys Avatar asked Oct 28 '25 15:10

Elarys


1 Answers

Not the ideal solution, but poking around the help manual for ld and some experiments over a few days revealed a solution to this myself.

I renamed /usr/bin/ld to /usr/bin/ld.old (which is ultimately a symlink to ld.gold), I then created a shell script called /usr/bin/ld which simply called /usr/bin/ld.old and forced through a few extra parameters.

--no-map-whole-file --no-keep-memory --no-keep-files-mapped

And this resolved my issue, this was nothing to do with 32/64bit allocation and was the fact that the linker was unable to find enough space. Yes, the options above increase I/O usage instead of memory, but the fact remains that it did indeed work.

like image 167
Elarys Avatar answered Oct 30 '25 06:10

Elarys



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!