如何为Azure Worker角色将gcAllowVeryLargeObjects设置为true?

时间:2014-10-31 14:36:17

标签: azure gcallowverylargeobjects

我无法理解如何为worker角色设置gcAllowVeryLargeObjectsruntime参数。我在app.config中设置了这个婴儿车。但它不起作用。据我所知,我需要以某种方式在wotker主机的配置文件中配置它。

更新:基于答案的最终解决方案

ServiceDefinition.csdef中

<WorkerRole name="XXX" vmsize="Standard_D4">
<Startup>
  <Task commandLine="myStartup.cmd" executionContext="elevated" taskType="simple" >
    <Environment>
      <Variable name="yyy" value="yyy" />
    </Environment>
  </Task>
</Startup>

应将下一个文件添加到工作项目中,作为“始终复制本地”

的内容

startup.ps1

#Configure Server GC and Background GC settings 
Function ChangeGCSettings
{
 param ([string]$filename,[bool]$enableServerGC,[bool]$enableBackgroundGC,[bool]$enableVeryLargeGC)

 $xml = New-Object XML
 if (Test-Path ($filename))
 {
 $xml.Load($filename)
 }
 else
 {
 #config file doesn't exist create a now one
 [System.Xml.XmlDeclaration]$xmlDeclaration = $xml.CreateXmlDeclaration("1.0","utf-8",$null)
 $xml.AppendChild($xmlDeclaration)
 }

 # Check if we already have a configuration section - if not create it
 $conf = $xml.configuration 
 if ($conf -eq $null)
 {
 $conf = $xml.CreateElement("configuration")
 $xml.AppendChild($conf)
 }

 # Check if we already have a runtime section - if not create it
 $runtime = $conf.runtime
 if ($runtime -eq $null)
 {
 #runtime element doesn't exist 
 $runtime = $xml.CreateElement("runtime")
 $conf.AppendChild($runtime)
 }

 # configure ServerGC
 $gcserver = $runtime.gcServer
 if ($gcserver -eq $null)
 {
 $gcserver = $xml.CreateElement("gcServer")
 $gcserver.SetAttribute("enabled",$enableServerGC.ToString([System.Globalization.CultureInfo]::InvariantCulture).ToLower())
 $runtime.AppendChild($gcserver);
 }
 else
 {
 $gcserver.enabled=$enableServerGC.ToString([System.Globalization.CultureInfo]::InvariantCulture).ToLower()
 }

 # configure gcAllowVeryLargeObjectsruntime 
 $gcAllowVeryLargeObjects = $runtime.gcAllowVeryLargeObjects 
 if ($gcAllowVeryLargeObjects -eq $null)
 {
 $gcAllowVeryLargeObjects = $xml.CreateElement("gcAllowVeryLargeObjects")
 $gcAllowVeryLargeObjects.SetAttribute("enabled",$enableVeryLargeGC.ToString([System.Globalization.CultureInfo]::InvariantCulture).ToLower())
 $runtime.AppendChild($gcAllowVeryLargeObjects);
 }
 else
 {
 $gcAllowVeryLargeObjects.enabled=$enableVeryLargeGC.ToString([System.Globalization.CultureInfo]::InvariantCulture).ToLower()
 }

 # configure Background GC
 # .NET 4.5 is required for Bacground Server GC 
 # since 4.5 is an in-place upgrade for .NET 4.0 the new Background Server GC mode is available 
 # even if you target 4.0 in your application as long as the .NET 4.5 runtime is installed (Windows Server 2012 or higher by default)
 # 
 # See http://blogs.msdn.com/b/dotnet/archive/2012/07/20/the-net-framework-4-5-includes-new-garbage-collector-enhancements-for-client-and-server-apps.aspx

 $gcConcurrent = $runtime.gcConcurrent 
 if ($gcConcurrent -eq $null)
 {
 $gcConcurrent = $xml.CreateElement("gcConcurrent")
 $gcConcurrent.SetAttribute("enabled",$enableBackgroundGC.ToString([System.Globalization.CultureInfo]::InvariantCulture).ToLower())
 $runtime.AppendChild($gcConcurrent);
 }
 else
 {
 $gcConcurrent.enabled=$enableBackgroundGC.ToString([System.Globalization.CultureInfo]::InvariantCulture).ToLower()
 }
 $xml.Save($filename)
}

# Enable Background Server GC
ChangeGCSettings "${env:RoleRoot}\base\x64\WaWorkerHost.exe.config" $true $true $true

myStartup.cmd

    REM   Attempt to set the execution policy by using PowerShell version 2.0 syntax.

   REM   PowerShell version 2.0 isn't available. Set the execution policy by using the PowerShell version 1.0 calling method.

   PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog1.txt" 2>&1
   PowerShell .\startup.ps1 >> "%TEMP%\StartupLog2.txt" 2>&1


REM   If an error occurred, return the errorlevel.
EXIT /B %errorlevel%

1 个答案:

答案 0 :(得分:1)

查看http://blogs.msdn.com/b/cie/archive/2013/11/14/enable-server-gc-mode-for-your-worker-role.aspx,它使用启动脚本在工作者角色中启用Server GC。您应该可以轻松修改它以更改gcAllowVeryLargeObjectsruntime属性。