How to reduce Visual Studio build process priority

2020-07-15 16:01发布

问题:

We are compiling big C++ projects using Visual Studio (2013) on a regular basis. Building the source can take up to 45 mins on a developer machine. During this time, the machine often becomes unresponsive due to 100% CPU load. Is there any way to tell Visual Studio to run the compile/build tools with a lower process priority so that the Windows UI isn't slowed down? I'd happily have the machine spend a bit more time overall on the build process if i could do other things meanwhile.

The problem has even become more annoying since installing an SSD! Although the overall build time is significantly improved, now that there is no more disk bottleneck, the build process runs into CPU max load even more often, with an unresponsive system as a consequence.

How to solve this issue?

回答1:

Update

I've just found the following thread, where someone by the name of Mikhail Virovets provided an automatic solution: https://developercommunity.visualstudio.com/idea/436208/limit-cpu-usage-of-visual-studio.html

In the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\cl.exe\PerfOptions, create a DWORD value called CpuPriorityClass and set it to 5. This will set the base priority of all newly created processes with that name to below normal. You should probably also create a similar entry for the linker.

Of course this is a little bit more invasive as it will affect all processes with those names, regardless of where, when and how they are created. But I think it will probably work fine.

Just don't forget you have added those values in case it causes some unwanted slowdowns in the future, otherwise you may be looking for the problem for a long time.

You can use the following .reg file to set the CPU and IO priority of the compiler and linker to "below normal"/"low":

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\cl.exe]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\cl.exe\PerfOptions]
"CpuPriorityClass"=dword:00000005
"IoPriority"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\link.exe]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\link.exe\PerfOptions]
"CpuPriorityClass"=dword:00000005
"IoPriority"=dword:00000001

Original Answer

I was facing the same problem and it really annoyed me. As a stop gap solution, I have created a small batch file that simply lowers the process priority of the most important build tools:

wmic process where name="msbuild.exe" CALL setpriority "idle"
wmic process where name="cl.exe" CALL setpriority "idle"
wmic process where name="link.exe" CALL setpriority "idle"

(This is for C++ builds, if you build something else you may want to change the .exe names to whatever your main build tools are.)

Of course this only affects processes that are currently running, so you have to run it every time you start a build. The priorities of newly created sub-processes (compiler, linker, ...) are being inherited from the main MSBuild process though, so this will not only affect the currently running compiler/linker instances but the whole build.

I've added an "external tool" entry with Command: cmd.exe and Arguments: /c C:\Path\To\My\LowerBuildPriority.bat so I can start the batch file from within Visual Studio.

A more convenient solution would probably be to add a custom task to the build that alters the process priority. Since AFAIK tasks are executed from within the main MSBuild process, this should work. But I haven't gotten around to trying it yet.