HowTo: How to investigate and solve Windows Desktop Heap Problems

Compatibility :1.0.0
Editions :Professional EditionStandard EditionCommunity Edition
Platforms :WindowsMac OSX (Not Supported)Linux (Not Supported)IBM AIX (Not Supported)FreeBSD (Not Supported)HP-UX (Not Supported)Solaris (Not Supported)IBM z/OS (Not Supported)IBM z/Linux (Not Supported)

When installing and starting several services on Windows, you may encounter a problem where it is no longer possible to start additional services.

Each Service allocates a certain amount of memory from the internal desktop heap space used to store things such as Windows, Fonts, API elements, etc. When this desktop heap is exhausted, user32 system calls will begin to fail. Unfortunately, it is not currently possible to query the Windows system for the amount of available desktop heap space in advance. When the desktop heap available to the Wrapper process is exhausted, you will most likely encounter one of the following errors:

  • Attempting to start a service results in the Service Manager immediately timing out with the following error:

    Error 1053: The service did not respond to the start or control request in a timely fashion
    
  • The Service starts, but the JVM fails to launch completely and exits with code 6. The wrapper.log file will usually contain an error like the following:

    STATUS | wrapper  | 2011/08/31 16:54:53 | Launching a JVM...
    INFO   | jvm 1    | 2011/08/31 16:54:54 | Error loading: C:\Sun\jdk1.6.0_24\jre\bin\server\jvm.dll
    ERROR  | wrapper  | 2011/08/31 16:54:54 | JVM exited while loading the application.
    
  • The Service starts, but fails to allocate a console.

A detailed description of the internal desktop heap can be found on the external site. If the service is launched as non-interactive Service, on a 32-bit Windows XP system, the default heap space is 512 KB, but this varies greatly depending on the specific version of Windows that you are running. The Wrapper itself allocates between 10-15 KB for each instance. However, the JVM being launched also allocates at least 20 KB. The exact value varies depending on what the Java application being run is doing. If the heap space is exhausted while the Wrapper process is being loaded, you will get the timeout described above. If it is exhausted while the JVM is being loaded, then you will see one of the other problems.

As of this writing, we have not been able to find a way for the Wrapper to query Windows for the amount of remaining desktop heap. It is possible to monitor and diagnose the desktop heap usage using the "Desktop Heap Monitor" tool provided by Microsoft. This tool works by running as a Kernel level driver, which requires a level of access that we do not wish to require the Wrapper to have.

Please read over the instructions on the Microsoft site on using dheapmon.exe. The basic steps are as follows:

  • The Desktop Heap Monitor requires access to the Windows system symbols to do its magic. In most cases, the easiest thing to do is access Microsoft's symbol server.

    dheapinst.exe -y srv*http://msdl.microsoft.com/download/symbols
    
  • Next, it is necessary to load the Desktop Heap Monitor kernel driver.

    dheapmon.exe -l
    
  • Finally, run to collect the current state of all desktop heaps.

    dheapmon.exe
    

When dheapmon.exe is run, you should see something like the following:

dheapmon.exe
Desktop Heap Information Monitor Tool (Version 8.1.2925.0)
Copyright (c) Microsoft Corporation.  All rights reserved.
-------------------------------------------------------------
  Session ID:    0 Total Desktop: (  5824 KB -    7 desktops)

  WinStation\Desktop            Heap Size(KB)    Used Rate(%)
-------------------------------------------------------------
  WinSta0\Default                    3072              8.2
  WinSta0\Disconnect                   64              4.5
  WinSta0\Winlogon                    128              9.8
  Service-0x0-3e7$\Default            512             28.8
  Service-0x0-3e4$\Default            512              2.1
  Service-0x0-3e5$\Default            512              3.1
  SAWinSta\SADesktop                 1024              0.3
-------------------------------------------------------------

In the above example, the Wrapper is being run as a non-interactive service, running as the System user, in Service-0x0-3e7$\Default. Starting additional non-interactive services as the default System User will increase the heap usage of this desktop accordingly.

Running the service as an interactive service will cause the WinSta0\Default heap space usage to increase.

Running the Service under a certain user account will create a new WinStation\Desktop session for each individual service.

There are 2 workarounds currently available:

  • Running as a specific service user account with the wrapper.ntservice.account property. Each Service will have its own Desktop and its Desktop Heap. This will let you run more services, but each service will require more Desktop Heap than when run together in the same Desktop. Once the total of all Desktop Heaps reaches the size of the Session View Space, resource problems will happen again. For Windows XP and newer versions of Windows, the total default Session View Space size is 48 MB. It can be increased with a registry change.

  • It is possible to increase the non-interactive desktop heap space by editing the registry as described in the instructions on Microsoft site. As always, be very careful modifying your registry.

64-bit versions of Windows have much larger default desktop heap sizes, so you are much less likely to encounter problems on these systems.