Search code examples
azuregcallowverylargeobjects

How to set gcAllowVeryLargeObjects to true for AzureWorker role?


I\m unable to understand how to set gcAllowVeryLargeObjectsruntime param for worker role. I set this pram in app.config. But it doesnt work. As I understand I need to somehow config it in config file of wotker host.

Update: Final solution based on answer

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>

Next files should be added to worker project as content with Copy Local Always

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%

Solution

  • Check out http://blogs.msdn.com/b/cie/archive/2013/11/14/enable-server-gc-mode-for-your-worker-role.aspx which uses a start script to enable Server GC in a worker role. You should be able to easily modify this to change the gcAllowVeryLargeObjectsruntime property.