it's usually the linker that uses a ton of memory rather than the compiler. I wonder if setting CFLAGS=-Wl,--no-keep-memory or LDFLAGS=--no-keep-memory helps. Alternate linkers exist but I don't think anyone has done work for their use to be convenient to switch out on the netbsd+pkgsrc case.