4

I am trying to install LLVM on my CentOS machine. In the installation tutorial of LLVM, a flag -jn is specified along with make.

It says to perform make -jn and also says "Choose n such that make doesn’t run in to swap space issue."

What is the use of the -j flag and how can I choose the value of n?

4 Answers 4

5

The -j make flag denotes how many threads you want to allot for compiling.

n is, in this case, a place-holder for the number of processes.

The classic rule of thumb is that it's safe to make n = the number of cores your CPU has. So if you are on a dual core machine, you might use -j2, while on an 8-core machine -j8

In practise, I have found that to be a good starting place, but you should probably feel free to experiment a bit and see what works best for you.

5
  • 2
    I like cores + 1
    – WorBlux
    Commented Jan 24, 2016 at 23:32
  • 1
    Won't it be processes not threads in a strict sense?
    – heemayl
    Commented Jan 25, 2016 at 1:27
  • yes good point @heemayl Commented Jan 25, 2016 at 2:58
  • 1
    Cores+1 is usually a better option: it lets you have one process stalled on disk I/O while still fully utilizing your CPU.
    – Mark
    Commented Jan 25, 2016 at 4:53
  • -j$(nproc) to always give it the number of cores the system has. Commented Sep 2, 2023 at 13:43
2

Whether it's safe to use n = number of cores also depends on whether you have enough memory for all the parallel compile/link jobs. It could also cause issues for disk I/O. If this is a make job you only expect to need to run once it's probably better to choose a lower n and just let it take its time.

2

The -j flag tells make(1) how many processes to run in parallel. Best value depends on the tasks run, dependencies, ... a rule of thumb is the number of processors. If you give just -j (no n), make starts as many processes in parallel as possible.

1

Just as a concrete example of how the -j flag affects a build operation and a reason to be careful with it:

I forgot I had MAKEFLAGS=-j5 and tried to build LLVM; once it reached 97% percent it spawned 5 ld processes at the same time, each consuming 1.5GB+ RAM. The first one crashed within 20 minutes, and the rest kept consuming more and more memory until my computer was unusable and I finally decided to Ctrl+C it and try again with a smaller -j. With -j3 my computer is still struggling with three ld processes but at least I can still type this with only a few hiccups.

1
  • Thank you for sharing this experience. It's helpful
    – Rick
    Commented Jul 21, 2020 at 9:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .