“Win32Exception没有足够的存储空间来处理此命令”作为Windows服务运行时出错

有时我得到错误

Win32Exception没有足够的存储空间来处理此命令

申请运行一个月左右后。

这对应于系统错误

ERROR_NOT_ENOUGH_MEMORY (8) 

通常,它作为Windows服务运行,并且更改其登录的用户仍然会发生错误。 但是,如果我通过双击它来运行应用程序,它可以正常工作。 它可以再次作为Windows服务运行的唯一方法是重新启动服务器。

该错误出现在应用程序写入的日志中。 完整的错误是:

 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) 

我已经看到其他答案说它与服务器上的内存泄漏有关,但是如果双击时应用程序仍然可以运行,那是否仍然意味着可能存在内存泄漏?

此外,这是在Windows Server 2012上运行。

是的,问题可能与服务器上的内存泄漏有关。 也许桌面堆耗尽了。 “非交互式”窗口会话的桌面堆与实际(交互式)桌面会话的桌面堆不同。 这解释了为什么仍然可以双击应用程序。

尝试找到注册表项\ System \ CurrentControlSet \ Control \ Session Manager \ SubSystem并增加SharedSection=xxx,yyy,zzz的第三个参数(zzz) SharedSection=xxx,yyy,zzz如MSKB 126962 (yyy)中所述。

第三个SharedSection值(512)是与“非交互式”窗口站关联的每个桌面的桌面堆的大小。

如果这可以解决您的问题,例如由资源泄漏导致的堆耗尽可能是您的问题的根源。 默认情况下,“非交互式”桌面堆的大小远小于标准堆。

RegisterClass的调用导致内存错误。 请参阅为什么RegisterClass因ERROR_NOT_ENOUGH_MEMORY而失败? 。 您的应用多次调用RegisterClassRegisterWindowsMessage