I am compiling v0.12.0rc2 for Raspbian Jessie on a Raspberry Pi 2. Since it only has 1 GB of memory and no swap, I am following Luke-jr's workaround in issue 6658 to make gcc use less memory by options for more aggressive garbage collecting.
When I invoke make:
$ make CXXFLAGS="--param ggc-min-expand=1 --param ggc-min-heapsize=32768"
It solves the memory use problem for the first part of the build, however it gets to compiling leveldb objects and errors:
Building LevelDB ...
make[3]: Entering directory '/home/jarret/git/bitcoin/src/leveldb'
g++ --param ggc-min-expand=1 --param ggc-min-heapsize=32768 -c helpers/memenv/memenv.cc -o helpers/memenv/memenv.o
helpers/memenv/memenv.cc:5:35: fatal error: helpers/memenv/memenv.h: No such file or directory
#include "helpers/memenv/memenv.h"
^
I see that g++ is getting invoked here without the full set of normal flags including the '-I.' to find the memenv.h header in this error.
If I reissue the make in debug mode without specifying any manual CXXFLAGS:
$ make -d
I see that the normal compilation of this source file looks like this:
g++ -I. -I./include -std=c++0x -fno-builtin-memcmp -pthread -DOS_LINUX -DLEVELDB_PLATFORM_POSIX -DLEVELDB_ATOMIC_PRESENT -Wstack-protector -fstack-protector-all -fPIE --param ggc-min-expand=1 --param ggc-min-heapsize=32768 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS -D_FILE_OFFSET_BITS=64 -D__STDC_LIMIT_MACROS -c helpers/memenv/memenv.cc -o helpers/memenv/memenv.o
For which I find that it now doing this correctly, and it successfully builds. Somehow, it gets the ggc-min-expand and ggc-min-heapsize parameters in there too, which must be residual from the first invocation - strange.
Workaround 1:
Once leveldb is done building, I can Ctrl-C the build and then go back to the manual CXXFLAGS. This builds fine until the end without running out of memory.
Workaround 2:
Specify the CXXFLAGS at configure time:
$ ./configure --disable-wallet CXXFLAGS="--param ggc-min-expand=1 --param ggc-min-heapsize=32768"