25

I'm building a couple of C++ files in xcode that take a lot of memory to compile (+1 GB / file). Because I do this on my dual core laptop, xcode uses 2 threads for building. The two threads will eventually be building the files that take a lot of memory simultaneously so the system suffers memory starvation and the compilation grinds to a near halt.

A sufficient solution for me would be to force Xcode to use only one build thread. Does anybody know a way to change how many build threads Xcode uses?

For those who are interested, the C++ files contain a sizable boost::spirit::qi parser.

Dries Staelens
  • 351
  • 1
  • 3
  • 7

4 Answers4

29

The number of threads Xcode is using to perform tasks is controlled by PBXNumberOfParallelBuildSubtasks option. You can change it with the following command: - defaults write com.apple.Xcode <key> <value>. For example:

defaults write com.apple.Xcode PBXNumberOfParallelBuildSubtasks 8

See Xcode User Defaults for more details.

There are also many other ways to speed up a compilation, from precompiled headers to distributed builds. Read Reducing Build Times for more information on this.

Good luck!

David Zorychta
  • 13,039
  • 6
  • 45
  • 81
  • This is exactly what I was looking for, thank you very much. Also the part on reducing build times is an interesting read. – Dries Staelens Dec 03 '10 at 18:05
  • 2
    Try this one defaults write com.apple.dt.Xcode IDEBuildOperationMaxNumberOfConcurrentCompileTasks ` `sysctl -n hw.ncpu` ` – Maximelc May 30 '17 at 11:31
12

With XCode 5, you can use -parallelizeTargets and -jobs NUMBER with xcodebuild. According to xcodebuild --help:

-parallelizeTargets     build independent targets in parallel
-jobs NUMBER            specify the maximum number of concurrent build operations
Jifeng Zhang
  • 5,037
  • 4
  • 30
  • 43
  • 1
    With the new build system in Xcode 9+ enabled, `-parallelizeTargets` appears to be redundant and, in my tests, has no effect on compile time. – friedbunny Nov 21 '18 at 22:29
4

For Xcode 4 you must set the IDEBuildOperationMaxNumberOfConcurrentCompileTasks user default, for example:

defaults write com.apple.dt.Xcode IDEBuildOperationMaxNumberOfConcurrentCompileTasks 4

Note the "dt". This won't affect xcodebuild on the command line. To do that, use something like

xcodebuild -IDEBuildOperationMaxNumberOfConcurrentCompileTasks=4 ...

(See http://lists.apple.com/archives/xcode-users/2011/Apr/msg00403.html and http://lists.apple.com/archives/xcode-users/2011/Jul//msg00377.html )

Mimika Oh
  • 261
  • 2
  • 3
1

A single build task should never do the same work twice, and certainly not simultaneously! Factor out the massive chunk of common code into a static library so it can be recompiled only when it changes. Set a target dependency in your application on the static library and link in the static library product. Changes to the rest of your application will then no longer require rebuilding the static library, which should speed up build times tremendously.

Try to exhaust all project-level solutions before manipulating Xcode as a whole. It is too easy to cripple Xcode to using only a single thread and forget to change it back when you move on to a new project. The Xcode User Default Reference documents many options that are not exposed via the Preferences interface, including:

  • PBXNumberOfParallelBuildSubtasks (positive integer)

    This allows you to limit Xcode to using only n build threads on every project it compiles.

  • BuildSystemCacheSizeInMegabytes (positive integer, default 1024)

  • BuildSystemCacheMinimumRemovalAgeInHours (positive integer, default 24)

    Upping the PCH cache size and retention time could help speed up your builds.

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
  • It's not the same work, they're actually several files with huge compilation memory usage. The same files are also built twice because Xcode builds both a 64-bit and a 32-bit version and then merges them afterwards. A static lib won't help me much as I'm currently working on those files. Thanks for your answer though, the PBXNumberOfParallelBuildSubtasks is exactly what I needed. A more permanent solution would be to add some RAM to my PC, I didn't realize that compilation tasks could take that much memory. – Dries Staelens Dec 03 '10 at 18:13
  • Does anyone know if BuildSystemCacheSizeInMegabytes still has any effect? – Mark Bridges May 29 '20 at 11:10