Fatal Error while building LLVM

I am building LLVM in Ubuntu 22.04 LTS by following instructions from llvm-project/flang/docs/GettingStarted.md at main · llvm/llvm-project (github.com).

After using ninja command from Building flang in tree method, I am getting fatal error and the build stops.

This is caused by out of memory. You may try below methods:

  1. Increase the Linux swap size;
  2. Reduce the number of threads by -jN;
  3. Use lld or gold as the linker;
  4. Build Release type instead of Debug;