31

Every time I compile using visual studio the rest of my computer crawls because visual studio is hogging all the processors. Is there a way to limit the number of processors that visual studio is using so I can still get some work done during the compilation time?

By the way, I am using visual studio 2013 and 2015 and programming in C++.

Thanks!

OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
Phaino
  • 501
  • 1
  • 5
  • 10
  • 3
    At Tools/Options/Projects and Solutions/Build and Run, you can set the maximum number of concurrent builds. There are also command line options if you're invoking builds that way (such as a formal build config system). – WhozCraig Mar 13 '16 at 19:03
  • That didn't seem to work. Visual studio is still spawning a bunch of compiler driver processes. – Phaino Mar 13 '16 at 19:51
  • It does appear to reduce the amount the compiler drivers, but even when I set it to one, there are four compiler driver processes and they take up 100% of the cpu. (If it as at 4, 12+ compiler driver processes are spawned) – Phaino Mar 13 '16 at 19:57
  • For msbuild there is the `/m:1` switch. – Chris O Dec 14 '17 at 02:22
  • Highly related to https://stackoverflow.com/questions/53503593/how-to-reduce-visual-studio-build-process-priority-to-prevent-unresponsive-syste (best answer = just run a `.reg`) – javaLover Sep 23 '19 at 09:38
  • ur lucky, for me the MP doesnt work at all – Martin Kosicky Mar 30 '23 at 18:54

9 Answers9

30

Setting the "Maximum number of parallel project builds" is not the answer if you have a single C++ project with lots of .cpp files and you don't want 8 building at once. To control that, go to Tools > Options > Projects and Solutions > VC++ Project Settings, and in the Build section, set Maximum Concurrent C++ Compilations to the max number of .cpp files you want to compile in parallel. The default setting appears to be 0, which apparently means there is no maximum. I have 4 cores/8 threads, and set this value to 4, and VS now only compiles 4 files at a time instead of 8.

These instructions are based on Visual Studio 2017, but I think it's been this way for a few releases.

Setting Max Concurrent C++ Compilations

Eric Hill
  • 661
  • 7
  • 11
  • 2
    I'm afraid it is not enough, sence VS also parallelize project builds (see answer by Digital_Reality). Each project spawns this number of `cl.exe`. – Mikhail Jan 22 '19 at 10:54
  • 2
    Yup, seems like a massive oversight, you can only limit the number of projects and number of compilation threads per project, but not the total number of threads. – riv Jan 30 '19 at 11:51
21

For Visual Studio 2015, change "Maximum number of parallel project builds" to desired number. (May be half number of processers in your m/c)

Menu> Tools > Options > Projects and solutions > Build and Run. Edit value.

Screenshot from VS2015

enter image description here

Further, maximum concurrent c++ compilation can be restricted in

Menu> Tools > Options > Projects and solutions > VC++ Project Settings > Maximum concurrent c++ compilation > Edit value.

Please note, if used 0 then all CPU will be used.

enter image description here

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
  • 4
    That seems pointess. It limits the number of simultaneous project builds, but it can still launch 8 instances of cl.exe at once. So instead of constant 100% CPU usage I'm getting 100% while it's compiling, and very low when it's linking. – riv Dec 10 '18 at 15:41
  • 3
    Changing of "Maximum number of parallel project builds" does not help because cpp files in one project are compiled in parallel. I set this value to 1 but CPU load on my PC is still 100% during a build. – Andrey Epifantsev Aug 15 '19 at 05:02
  • As mentionned above, concurrent cl.exe are not decreased by this option, answer below is better – Juicebox Apr 06 '20 at 12:37
11

I found a workaround that actually works for me. Manually restrict affinity for VS process. Open Task Manager, go to Details tab, right click on devenv.exe, select "Set affinity". In the dialog untick several cores. That's it. All spawned cl.exe processes will inherit affinity, and thus won't run on unticked cores.

enter image description here enter image description here

Also, go and cast your vote for a feature request for Visual Studio: https://developercommunity.visualstudio.com/content/idea/436208/limit-cpu-usage-of-visual-studio.html

Mikhail
  • 20,685
  • 7
  • 70
  • 146
  • 1
    This basically works, it limits the total physical CPUs VS can use. However, its a bit dumb because VS still tries to compile as many things as there are physical cores. I limited it to 6 CPU cores but its still trying to compile 8 things at once. – O'Rooney Oct 09 '19 at 01:46
8

For C++ Use

*msbuild /p:CL_MPCount=X 

Where X is the number of compiler driver. I use this to limit the CPU utilization when compiling the Tensorflow source code.

Read this for more details: ms's blog on vs2010 c++ parallel building

Graham
  • 7,431
  • 18
  • 59
  • 84
Hi_CF
  • 81
  • 1
  • 5
5

A more optimal answer comes from Mikhail Virovets at https://developercommunity.visualstudio.com/comments/497084/view.html

Decrease the compiler process priority to below normal. This means it will use as much CPU as possible but without interfering with other things you're trying to use.

Define a registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\cl.exe\PerfOptions

Then create a DWORD value named CpuPriorityClass = 5 (5 means "below normal").

Note that this affects all processes named "cl.exe".

O'Rooney
  • 2,878
  • 2
  • 27
  • 41
1

The /MP option might do it. It limits the number of processes that are spawned when you build a project. So, in your case, you would use it like this /MP1 (/MP[processMax], where processMax is the maximum amount of processes that you want to use).

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • Currently, I am using the /MP, but I modified it to /MP1. However, it still is spawning 4 processes and taking up 100% of the CPU. – Phaino Mar 21 '16 at 03:21
0

From VS2022 version 17.3 and above, the best way to achieve this is to limit build processes priority.

If the build is affecting you from having a responsive work environment, then this is the feature for you!

Starting in Visual Studio 2022 version 17.3 Preview 1, a new option under the Tools -> Options -> “Projects and Solutions” -> “Build And Run” -> “Run build at low process priority” allows you to lower the priority of the your next builds. If a MSBuild node is already running with the previous priority, then give them a few minutes to time out or restart the IDE to close them.

More info here.

Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
0

Another solution that worked for me, BEFORE starting "Build" or "Rebuild", search for "devenv.exe" in task manager, go to details and lower its affinity to fewer cores. (setting affinity of devenv.exe AFTER starting build will not have any effect)

After that it doesnt matter how many proccesses are spawned, it will not use more cores/threads.

enter image description here

enter image description here enter image description here

-1

If you're running Windows Vista/7 (possibly XP, but not sure) it's really rather simple. Type in: Control+Shift+Esc to get your taskmanager up. Click on the Processes tab Find the process that needs its processor affinity changed Right-click on the process Click on "Set Affinity" Here you can select which processor(s) your process will use. EDIT: You have to be administrator to get this to work.

  • 1
    This actually worked. However, I needed to do some research to get this to work on Windows 10. Basically, you need to got to the details tab and this allows you to Right Click on a given process and set it's affinity. – Phaino Mar 21 '16 at 03:24
  • What process do you change affinity ? MSBuild or devenv ? – Steven Dec 07 '16 at 14:00