问:我有一个生成大量子进程的windows服务。当进程数量到达约100个时,无法再启动更多进程。CreateProcess()调用返回一个pid,但是进程就是无法生成。如果我将我们的服务器作为控制台应用程序运行,此限制就会消失。此外,如果我设置DETACHED_PROCESS标志,则限制将增加一倍多。但是,如果我设置DETACHED_PROCESS并调用CreateProcesssWithLogonW(),就会出现故障。
问:我有一个生成大量子进程的windows服务。当进程数量到达约100个时,无法再启动更多进程。CreateProcess()调用返回一个pid,但是进程就是无法生成。如果我将我们的服务器作为控制台应用程序运行,此限制就会消失。此外,如果我设置DETACHED_PROCESS标志,则限制将增加一倍多。但是,如果我设置DETACHED_PROCESS并调用CreateProcesssWithLogonW(),就会出现故障。
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\SubSystems\Windows
你会看到一个字符串,例如:
%SystemRoot%\system32\csrss.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
关键部分是:
SharedSection=1024,20480,768
第二个数字(20480)是交互式会话的大小。第三个数字(768)是非交互式(服务)会话的大小。请注意,第三个数字比第二个数字小26倍。通过实验,我们发现将其更改为:
SharedSection=1024,20480,2048
将项目限制从106增加到270,与堆大小几乎完美地比例。选择反映您预计系统上所有用户同时打开的最大项目数的值。不要使此值过大,并且不要超过8192,因为系统中的每个服务都会消耗更多宝贵的资源。
您需要重新启动计算机以使这些新设置生效。
$newValue = "%SystemRoot%\system32\csrss.exe ObjectDirectory=\Windows SharedSection=1024,20480,2048 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3 ServerDll=sxssrv,4 ProfileControl=Off MaxRequestThreads=16"
$origValue = "%SystemRoot%\system32\csrss.exe ObjectDirectory=\Windows SharedSection=1024,20480,768 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3 ServerDll=sxssrv,4 ProfileControl=Off MaxRequestThreads=16"
if ($update) {
Clear-Variable update
}
$updateConfirm= [System.Windows.Forms.MessageBox]::Show("Update Desktop Heap Limitation to 2048?" , "Status" , 4)
if ($updateConfirm -eq "YES" ) {
$update = $true
} else {
$revertConfirm= [System.Windows.Forms.MessageBox]::Show("Revert Desktop Heap Limitation to 768?" , "Status" , 4)
if ($revertConfirm -eq "YES" ) {
$update = $false
}
}
if (($updateConfirm -ne "YES") -and ($revertConfirm -ne "YES")) {
Write-Host "User did not specify whether to update or revert Desktop Heap Limitation. Exiting Setup."
Read-Host "Press Enter to exit."
break
}
#Import Active Directory PowerShell module
if (Test-Path C:\Users\${env:USERNAME}\Documents\WindowsPowerShell\Modules\ActiveDirectory\ActiveDirectory.psm1) {
Import-Module ActiveDirectory -prefix AD
} else {
$s = New-PSSession -computerName DC01WDC01
Invoke-command { import-module ActiveDirectory } -session $s
Export-PSSession -session $s -commandname *-AD* -outputmodule ActiveDirectory -allowclobber
Import-Module ActiveDirectory -prefix AD
Remove-PSSession -session $s
}
$servers = Get-ADADComputer -Filter {(Name -Like "RDS*")} | Select -Expand Name
foreach ($server in $servers) {
Write-Host "Working on $server" -ForegroundColor Magenta
if(!(Test-Connection -ComputerName $server -Count 1 -quiet)) {
Write-Warning "$server : Offline"
Continue
}
if ($update -eq $true) {
Invoke-Command -ComputerName $server -ArgumentList $newValue -ScriptBlock {
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\SubSystems" -Name "Windows" -Value $args[0]
$result = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\SubSystems" | SELECT Windows
if ($result.Windows -like "*SharedSection=1024,20480,2048*") {
Write-Host "Successfully reverted Desktop Heap Limit to 2048" -ForegroundColor Green
} else {
Write-Warning "Update to registry value unsuccessful on $env:ComputerName"
}
}
} elseif ($update -eq $false) {
Invoke-Command -ComputerName $server -ArgumentList $origValue -ScriptBlock {
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\SubSystems" -Name "Windows" -Value $args[0]
$result = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\SubSystems" | SELECT Windows
if ($result.Windows -like "*SharedSection=1024,20480,768*") {
Write-Host "Successfully reverted Desktop Heap Limit to 768" -ForegroundColor Green
} else {
Write-Warning "Update to registry value unsuccessful on $env:ComputerName"
}
}
}
}