{"record":{"id":"9c72312c8b235c75","repo":"dotnet/wpf","slug":"e-outofmemory","errorCode":"E_OUTOFMEMORY","errorMessage":"OutOfMemoryException","messagePattern":"OutOfMemoryException","errorType":"exception","errorClass":"OutOfMemoryException","httpStatus":null,"severity":"critical","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs","lineNumber":424,"sourceCode":"\n        /// <summary>\n        /// The partition this media context is connected to went into\n        /// zombie state. This means either an unhandled batch processing,\n        /// rendering or presentation error and will require us to reconnect.\n        /// </summary>\n        private void NotifyPartitionIsZombie(int failureCode)\n        {\n            //\n            // We only get back these kinds of notification:-\n            // For all OOM cases, we get E_OUTOFMEMORY.\n            // For all OOVM cases, we get D3DERR_OUTOFVIDEOMEMORY and\n            // for all other errors we get WGXERR_UCE_RENDERTHREADFAILURE.\n            //\n\n            switch (failureCode)\n            {\n            case HRESULT.E_OUTOFMEMORY:\n                throw new System.OutOfMemoryException();\n            case HRESULT.D3DERR_OUTOFVIDEOMEMORY:\n                throw new System.OutOfMemoryException(SR.MediaContext_OutOfVideoMemory);\n            default:\n                throw new System.InvalidOperationException(SR.MediaContext_RenderThreadError);\n            }\n        }\n\n        /// <summary>\n        /// The back channel processed a malformed packet and so gives\n        /// the notification of invalid packet.\n        /// </summary>\n        private void HandleInvalidPacketNotification()\n        {\n            //\n            // -\n            // For now we ignore the packet and continue processing\n            // other packets. In future, we could also close this channel.\n            //","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaContext.cs#L406-L442","documentation":"MediaContext.NotifyPartitionIsZombie maps the render-thread failure HRESULT to a managed exception. When the failure code is E_OUTOFMEMORY it throws a bare OutOfMemoryException, indicating the render/UCE partition went zombie because the renderer ran out of memory.","triggerScenarios":"The render thread reports partition failure with HRESULT E_OUTOFMEMORY (via NotifySyncChannelMessage or NotifyChannelMessage) — usually after huge image allocations, excessive bitmap caching, or desktop heap / video-memory pressure.","commonSituations":"Loading many large images simultaneously, huge WriteableBitmap/RenderTargetBitmap allocations, memory leaks in visual-tree content, 32-bit process address-space exhaustion.","solutions":["Free visual resources: unload large images (set Source=null), call GC.Collect after releases, and reduce simultaneous large bitmaps.","Switch to a 64-bit process to remove the 2GB address-space limit.","Use DecodePixelWidth/Height to downscale images at decode time instead of full-resolution bitmaps.","Profile memory (dotMemory/PerfMon) for leaks around Visuals, Images and bitmaps; check for the real OutOfMemoryException inner cause."],"exampleFix":"// before\nimg.Source = new BitmapImage(new Uri(\"huge.jpg\")); // full-res decode\n// after\nvar bmp = new BitmapImage();\nbmp.DecodePixelWidth = 800; // decode scaled-down\nbmp.BeginInit();\nbmp.UriSource = new Uri(\"huge.jpg\");\nbmp.EndInit();\nimg.Source = bmp;","handlingStrategy":"try-catch","validationCode":"// Pre-check process memory before loading large imagery:\nusing var p = Process.GetCurrentProcess();\nif (p.PagedMemorySize64 > 1L * 1024 * 1024 * 1024) FreeUnusedImages();","typeGuard":null,"tryCatchPattern":"try { LoadLargeImages(); }\ncatch (OutOfMemoryException) { FreeUnusedImages(); GC.Collect(); GC.WaitForPendingFinalizers(); RetryWithDownscaling(); }","preventionTips":["Set DecodePixelWidth/DecodePixelHeight on all BitmapImages","Run as 64-bit to lift the 2GB limit","Release Image.Source for off-screen items (virtualization)","Monitor private bytes and handle leaks in production"],"tags":["wpf","out-of-memory","render-thread","zombie-partition"],"backgroundTag":"out-of-memory","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"}