Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

createprocess - How to increase the maximum number of child processes that can be spawned by a windows service -- desktop heap limits

Q: I have a windows service that spawns a lot of child processes. There seems to be some limit, around 100, where the process cannot be launched. The CreateProcess() call comes back with a pid, but then the process just fails to materialize. If I run our server as a console application instead, this limit goes away. Also, If I set the DETACHED_PROCESS flag, this limit more than doubles. However, I get failures if I set DETACHED_PROCESS and call CreateProcesssWithLogonW().

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a "desktop heap" problem. A very good discussion can be found here:

Desktop Heap Overview and Desktop Heap, Part 2

Note that this only applies to programs that are running as services, because the default desktop heap size for services is so much smaller than that for applications.

In our case, we were able to launch about 100 child processes before running out of resources without the change. With the change, this number can be increased considerably.

This is the answer we've given to our end users on our knowledgebase:

WARNING: this affects the desktop heap of all services! Do not make it larger than necessary or you will push the system to consume more resource and you may bump up against problems in the total available desktop heap size.

If you find that you cannot open more than about 100 total projects, even on a very large RAM server, you may have run into a limit of the Windows "desktop heap size".

The problem is that service sessions under windows (where the services run) have less of this "desktop heap" space available for creating windows.

The short version is:

  • Services get smaller desktop heaps than interactive sessions.

  • Desktop heap size limits the number of windows

  • Each sub-server creates one or more “windows” even if we can’t see them.

Solution:

  1. Backup your registry before making any changes!

  2. Run regedit.exe as administrator

  3. Edit the registry value:

    HKEY_LOCAL_MACHINESystemCurrentControlSetControlSession ManagerSubSystemsWindows
    
  4. You will see a string like:

    %SystemRoot%system32csrss.exe ObjectDirectory=Windows SharedSection=1024,20480,768 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3 ServerDll=winsrv:ConServerDllInitialization,2 ServerDll=sxssrv,4 ProfileControl=Off MaxRequestThreads=16
    

The critical bit is:

SharedSection=1024,20480,768

The second number (20480) is the size for interactive sessions. The third number (768) is the size of non-interactive (services) sessions. Note how the third number is 26x smaller than the second. Experimentally, we found that changing this to:

SharedSection=1024,20480,2048

Increased the project limit from 106 to 270, almost perfectly scaling with the heap size. Pick a value that reflects the maximum number of projects that you expect to be opened simultaneously by all users on the system. Do not make this value larger than necessary, and no larger than 8192, as each service in your system will consume more of a precious resource.

You will need to reboot for these new settings to take effect.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...