{"record":{"id":"a3fd81bf0726cd8c","repo":"openjdk/jdk","slug":"java-lang-outofmemoryerror-n","errorCode":null,"errorMessage":"java.lang.OutOfMemoryError\\n","messagePattern":"java\\.lang\\.OutOfMemoryError\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/java.desktop/windows/native/libawt/windows/awt_new.cpp","lineNumber":77,"sourceCode":"    DASSERT(thread_seeded != TLS_OUT_OF_INDEXES);\n#endif\n\n    // use new handler for operator new and malloc\n    _set_new_mode(1);\n\n    // set the function which will be called when operator new or\n    // malloc runs out of memory\n    _set_new_handler((_PNH)NewHandler::handler);\n}\n\n// Called when malloc or operator new runs out of memory. We try to\n// compact the heap by initiating a Java GC. If the amount of free\n// memory available after this operation increases, then we return\n// (1) to indicate that malloc or operator new should retry the\n// allocation. Returning (0) indicates that the allocation should fail.\nint\nNewHandler::handler(size_t) {\n    fprintf(stderr, \"java.lang.OutOfMemoryError\\n\");\n    return FALSE;\n}\n\n// These three functions throw std::bad_alloc in an out of memory condition\n// instead of returning 0. safe_Realloc will return 0 if memblock is not\n// NULL and size is 0. safe_Malloc and safe_Calloc will never return 0.\nvoid *safe_Malloc(size_t size) {\n    void *ptr = malloc(size);\n    if (ptr == nullptr) {\n        throw std::bad_alloc();\n    }\n\n    return ptr;\n}\n\nvoid *safe_Calloc(size_t num, size_t size) {\n    void *ptr = calloc(num, size);\n    if (ptr == nullptr) {","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/openjdk/jdk/blob/88dfb74bbeefcf2b0aa11835183bcd949998fc8f/src/java.desktop/windows/native/libawt/windows/awt_new.cpp#L59-L95","documentation":"Windows AWT installs a C++ new-handler; when malloc/operator new exhausts native heap, NewHandler::handler prints 'java.lang.OutOfMemoryError' to stderr and returns FALSE so the allocation fails immediately (subsequent safe_Malloc/safe_Realloc/safe_Calloc then throw std::bad_alloc). The line means the process ran out of native (C heap) memory inside AWT code, not a Java heap OOM — despite the java-looking text.","triggerScenarios":"Heavy native allocation on Windows: very large images/bitmaps, many DWM/GDI objects, deep component trees, native buffers in 2D/3D pipelines. malloc cannot be satisfied, the handler runs (the comment says it tries to trigger a Java GC on some paths, but here it simply reports and fails), and std::bad_alloc propagates from safe_* allocators, often crashing or exiting the app.","commonSituations":"32-bit JVM hitting the ~2GB process address-space ceiling with big heaps (-Xmx too large leaves too little native space); memory leaks in native code (GDI/bitmaps not released); loading huge images; long-running desktop apps fragmenting the C heap.","solutions":["Switch to a 64-bit JVM — removes the address-space ceiling that most often triggers this","On 32-bit, lower -Xmx/-XX:MaxPermSize equivalents to leave room for native heap and stacks","Profile native memory (jcmd VM.native_memory with NativeMemoryTracking, Windows UMDH) to find C/GDI leaks","Reduce simultaneous large images/back buffers; call Image.flush()/dispose paths so GDI/bitmaps are released"],"exampleFix":"# before (32-bit JVM, huge heap)\njava -Xmx1600m -jar app.jar   # native heap starved -> 'java.lang.OutOfMemoryError' from NewHandler\n\n# after\njava -Xmx900m -jar app.jar     # or use a 64-bit JVM and keep -Xmx","handlingStrategy":"validation","validationCode":"// before launch: ensure native heap headroom (32-bit especially)\n// keep -Xmx well below the 32-bit process limit, or check bitness:\nif (System.getProperty(\"sun.arch.data.model\").equals(\"32\")\n        && Runtime.getRuntime().maxMemory() > 1_200_000_000L) {\n    System.err.println(\"32-bit JVM with very large -Xmx: native heap may be starved\");\n}","typeGuard":null,"tryCatchPattern":"try { bigOp(); } catch (OutOfMemoryError | std-bad-alloc-wrapper e) { /* reduce image/buffer sizes, flush caches, retry smaller */ }","preventionTips":["Prefer 64-bit JVMs for image-heavy desktop workloads","Release native resources deterministically: Image.flush(), dispose()","Track GDI handle counts on Windows during longevity tests"],"tags":["windows","out-of-memory","native-heap","awt","cpp"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}