{"record":{"id":"3562ed301b40420b","repo":"dotnet/wpf","slug":"error-not-enough-memory-8-error-outofmemory-14","errorCode":"ERROR_NOT_ENOUGH_MEMORY (8) / ERROR_OUTOFMEMORY (14)","errorMessage":"OutOfMemoryException","messagePattern":"OutOfMemoryException","errorType":"exception","errorClass":"OutOfMemoryException","httpStatus":null,"severity":"critical","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs","lineNumber":1691,"sourceCode":"                case 1400:  // 1400 ERROR_INVALID_WINDOW_HANDLE     Invalid window handle.\n                case 1401:  // 1401 ERROR_INVALID_MENU_HANDLE       Invalid menu handle.\n                case 1402:  // 1402 ERROR_INVALID_CURSOR_HANDLE     Invalid cursor handle.\n                case 1403:  // 1403 ERROR_INVALID_ACCEL_HANDLE      Invalid accelerator table handle.\n                case 1404:  // 1404 ERROR_INVALID_HOOK_HANDLE       Invalid hook handle.\n                case 1405:  // 1405 ERROR_INVALID_DWP_HANDLE        Invalid handle to a multiple-window position structure.\n                case 1406:  // 1406 ERROR_TLW_WITH_WSCHILD          Cannot create a top-level child window.\n                case 1407:  // 1407 ERROR_CANNOT_FIND_WND_CLASS     Cannot find window class.\n                case 1408:  // 1408 ERROR_WINDOW_OF_OTHER_THREAD    Invalid window; it belongs to other thread.\n                    throw new ElementNotAvailableException();\n\n                // We're getting this in AMD64 when calling RealGetWindowClass; adding this code\n                // to allow the DRTs to pass while we continue investigation.\n                case 87:    //   87 ERROR_INVALID_PARAMETER\n                    throw new ElementNotAvailableException();\n\n                case 8:     //    8 ERROR_NOT_ENOUGH_MEMORY         Not enough storage is available to process this command.\n                case 14:    //   14 ERROR_OUTOFMEMORY               Not enough storage is available to complete this operation.\n                    throw new OutOfMemoryException();\n\n                case 998:   //  998 ERROR_NOACCESS                  Invalid access to memory location.\n                case 5:     //    5 ERROR_ACCESS_DENIED\n                    throw new InvalidOperationException();\n\n                case 121:   //  121 ERROR_SEM_TIMEOUT               The semaphore timeout period has expired.\n                case 258:   //  258 WAIT_TIMEOUT                    The wait operation timed out.\n                case 1053:  // 1053 ERROR_SERVICE_REQUEST_TIMEOUT   The service did not respond to the start or control request in a timely fashion.\n                case 1460:  // 1460 ERROR_TIMEOUT                   This operation returned because the timeout period expired.\n                    throw new TimeoutException();\n\n                default:\n                    // Not sure how to map the reset of the error codes so throw generic Win32Exception.\n                    throw new Win32Exception(errorCode);\n            }\n        }\n\n        internal static bool UnhookWinEvent(IntPtr winEventHook)","sourceCodeStart":1673,"sourceCodeEnd":1709,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs#L1673-L1709","documentation":"The Win32 error mapper throws OutOfMemoryException when GetLastError is 8 (ERROR_NOT_ENOUGH_MEMORY) or 14 (ERROR_OUTOFMEMORY). The underlying Win32 UI call failed because the system or the target process could not allocate memory, and the mapper converts that native condition into the .NET OutOfMemoryException.","triggerScenarios":"Win32 accessibility/window calls made by proxies (SendMessage, GetWindowLong-style calls, etc.) failing with native error 8 or 14 — system-wide or per-process memory exhaustion.","commonSituations":"Automating very large apps or desktops under memory pressure; target process hitting its address-space limit (32-bit processes); leaky automation loops that never release elements.","solutions":["Free memory / reduce memory pressure on both the client and the target process, then retry","Look for leaks in long-running automation loops (cached elements, providers, handles never released)","Restart the target application if its process is exhausted (common for 32-bit targets)","Catch OutOfMemoryException from UIA calls and retry after a delay rather than crashing the harness"],"exampleFix":"// before\nvar result = NativeCall(hwnd); // native error 8/14 -> OutOfMemoryException\n// after\ntry\n{\n    var result = NativeCall(hwnd);\n}\ncatch (OutOfMemoryException)\n{\n    GC.Collect();\n    Thread.Sleep(500); // allow target process to recover\n    result = NativeCall(hwnd); // retry\n}","handlingStrategy":"retry","validationCode":"// Check available memory before heavy automation runs:\nvar gc = GC.GetGCMemoryInfo();\nif (gc.MemoryLoadBytes > threshold) { FreeResources(); }","typeGuard":null,"tryCatchPattern":"for (int i = 0; i < 3; i++) { try { return NativeCall(hwnd); } catch (OutOfMemoryException) { Thread.Sleep(1000); GC.Collect(); } } throw;","preventionTips":["Dispose UIA elements/providers promptly in long-running loops","Monitor memory of both client and target processes during automation","Prefer 64-bit target processes to avoid 32-bit address-space exhaustion","Schedule large automation runs away from peak system memory load"],"tags":["ui-automation","out-of-memory","win32-error","resource-exhaustion"],"backgroundTag":"resource-not-found","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}