4

Sometimes I get the error

Win32Exception Not enough storage is available to process this command

after the application has been running for around a month or so.

This corresponds to the system error

ERROR_NOT_ENOUGH_MEMORY (8)

Usually, it's run as a Windows service, and changing the user it's logged on as still makes the error occur. However, if I run the application by double clicking on it, it works fine. The only way it can run as a Windows service again is if the server is restarted.

The error appears in the logs that the application writes to. The full error is:

System.ComponentModel.Win32Exception: Not enough storage is available to process this command
   at System.Windows.Forms.NativeWindow.WindowClass.RegisterClass()
   at System.Windows.Forms.NativeWindow.WindowClass.Create(String className, Int32 classStyle)
   at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Application.MarshalingControl..ctor()
   at System.Windows.Forms.Application.ThreadContext.get_MarshalingControl()
   at System.Windows.Forms.WindowsFormsSynchronizationContext..ctor()
   at System.Windows.Forms.WindowsFormsSynchronizationContext.InstallIfNeeded()
   at System.Windows.Forms.Control..ctor(Boolean autoInstallSyncContext)
   at System.Windows.Forms.ScrollableControl..ctor()
   at System.Windows.Forms.ContainerControl..ctor()
   at System.Windows.Forms.Form..ctor()
   at LicensingModule.LicenseKeyValidator..ctor(String Name, String Path)
   at MIDICOMExporter.Program.Main(String[] args)

I've seen other answers say it's related to a memory leak on the server, but if the application can still run when double clicked on, does that still mean there may be a memory leak?

Also, this is running on Windows Server 2012.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Trisha
  • 141
  • 1
  • 2
  • 10
  • "The only way it can run as a Windows service again is if the server is restarted." Do you mean if the **service** is restarted? Or do you really mean that you have to restart the Windows Server program? – RenniePet Mar 08 '15 at 02:16
  • I mean the server has to be shut down and started again. Restarting the service before then didn't work. – Trisha Mar 08 '15 at 05:19
  • That sounds pretty strange. One suggestion: try running ProcMon while getting your program to fail, and see if it logs anything that gives you a clue as to what the problem is. https://technet.microsoft.com/en-us/library/bb896645.aspx – RenniePet Mar 08 '15 at 06:11
  • When you look in the system's event log, isn't there more information than just “Win32Exception Not enough storage is available to process this command”? Where exactly are you seeing that message? – RenniePet Mar 08 '15 at 06:17
  • 1
    I added the full error message in the question. I see this message in the application's logs. – Trisha Mar 10 '15 at 23:03
  • It could have something to do with the fact that you are trying to display a form from LicensingModule.LicenseKeyValidator(). – 500 - Internal Server Error Mar 11 '15 at 00:44
  • As @500-InternalServerError points out, there seems to be something in your program that is a WinForms program. In general, it is not a good idea to run a WinForms program as a Windows service. – RenniePet Mar 11 '15 at 00:48
  • 1
    Looks like the service is trying to put up a Windows dialog. Does it have the "Allow service to interact with the desktop" flag set? (In the Services control panel, right-click the service, choose Properties, then the "Log On" tab.) – Jeremy McGee Apr 15 '15 at 21:59
  • No, the "Allow service to interact with the desktop" box isn't checked. It's actually grayed out since the service is running under a username. – Trisha May 26 '15 at 23:52

2 Answers2

4

Yes, the problem might be related to a memory leak on the server. Maybe the desktop heap is depleted. The desktop heap for "non-interactive" window sessions is different from the desktop heap for real (interactive) desktop sessions. This explains why double clicking the application is still possible.

Try finding the registry key \System\CurrentControlSet\Control\Session Manager\SubSystem and increasing the 3rd parameter (zzz) of SharedSection=xxx,yyy,zzz as described in MSKB 126962 (for yyy).

The third SharedSection value (512) is the size of the desktop heap for each desktop that is associated with a "non-interactive" window station.

If this fixes your problem, heap depletion, caused for instance by leaking resources, is likely to be the source of your problem. The size of the "non-interactive" desktop heap is by default much smaller than the standard heap.

DarrellNorton
  • 3,901
  • 2
  • 21
  • 21
yonojoy
  • 5,486
  • 1
  • 31
  • 60
  • I didn't try this since the memory leak in the code was found, but I like the explanation for why it can't run as a service but can still run while double clicking the application so I'll mark this as the answer. – Trisha Jul 01 '15 at 18:53
0

The call to RegisterClass is resulting in a memory error. See Why RegisterClass fails with ERROR_NOT_ENOUGH_MEMORY?. Is your app calling RegisterClass or RegisterWindowsMessage many times?

Community
  • 1
  • 1
David Ching
  • 1,903
  • 17
  • 21
  • I tried using the atom table monitor (https://github.com/JordiCorbilla/atom-table-monitor). It looks like it says there are ~800-900 total atoms being used. Does this mean the problem is not from atoms running out? – Trisha Apr 15 '15 at 18:53