How to simulate an Out-Of-Memory (OOM) condition in a Win32 desktop application

Harshithraj1871 1,751 Reputation points
2026-01-07T07:26:00.6733333+00:00

Hi,

I’m working on a Win32 desktop application and trying to simulate an out-of-memory (OOM) condition within my application.

To make the test more realistic, I disabled paging by setting “No paging file” in the Windows virtual memory advanced settings.

In my application, I continuously allocate memory using VirtualAlloc with MEM_RESERVE | MEM_COMMIT, storing all returned pointers so the memory is not released. The allocation strategy is as follows:

  • First, allocate 1 GB chunks in a loop until allocation fails.
  • Then allocate 10 MB chunks in a loop until allocation fails.
  • Finally, allocate 50 KB chunks in a loop until allocation fails.

At this point, VirtualAlloc fails consistently, indicating that user-mode memory is exhausted.

However, after reaching this state, I am still able to:

  • Create a Win32 window
  • Display a Win32 MessageBox

Even with paging disabled and no further user-mode memory available, these UI elements continue to work. I understand that Win32 windows and message boxes rely on kernel-mode memory (USER/GDI subsystems), which appears to remain available.

My questions are:

Is it possible to simulate an OOM condition by exhausting kernel memory (e.g., USER/GDI or non-paged pool) from a user-mode Win32 application?

If so, what is the safest and recommended way to do this without crashing or destabilizing Windows?

If not, what is the correct or industry-standard approach to testing OOM handling in Win32 desktop applications?

Thanks in advance for any insights.

Windows development | Windows API - Win32
0 comments No comments
{count} votes

Answer accepted by question author
  1. Harry Vo (WICLOUD CORPORATION) 4,745 Reputation points Microsoft External Staff Moderator
    2026-01-07T08:33:03.7+00:00

    Hi @Harshithraj1871 ,

    Is it possible to simulate an OOM condition by exhausting kernel memory (e.g., USER/GDI or non-paged pool) from a user-mode Win32 application?

    It's possible but I strongly advise against this approach. Exhausting kernel resources like the non-paged pool can destabilize or crash the entire system, not just your application. Windows has built-in protections, but pushing these limits is risky.

    If not, what is the correct or industry-standard approach to testing OOM handling in Win32 desktop applications?

    Consider these industry-standard practices:

    • Use Windows Application Verifier with Low Resource Simulation - this tool can simulate memory allocation failures without actually exhausting memory
    • Implement fault injection in your code using preprocessor directives or a test mode that makes allocations fail after certain thresholds
    • Use memory limit tools like Job Objects (SetProcessMemoryLimit) to constrain your process to a specific memory budget
    • Virtual machine testing - run your app in a VM with very limited RAM (e.g., 512MB) to naturally trigger memory pressure

    You've correctly identified that USER/GDI subsystems use kernel memory. This is actually by design - Windows tries to keep critical UI functions alive even under memory pressure so users can at least see error dialogs and potentially save work.

    It's better to focus on testing your application's own allocation failures rather than trying to exhaust all system memory. Most real-world OOM scenarios in production will fail your app's allocations while the system remains functional.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Viorel 126.1K Reputation points
    2026-01-07T08:21:56.9366667+00:00

    Maybe these tools can be useful: Testlimit, NotMyFault, CpuStress:

    1 person found this answer helpful.
    0 comments No comments

  2. Q&A Assist
    2026-01-07T07:27:13.18+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Simulating an Out-Of-Memory (OOM) condition in a Win32 desktop application can be complex, especially when considering the distinction between user-mode and kernel-mode memory. As you've observed, exhausting user-mode memory with VirtualAlloc can still allow certain UI operations because they rely on kernel-mode memory, which may not be exhausted in the same way.

    To address your questions:

    1. Simulating OOM by exhausting kernel memory: It is generally not feasible to directly exhaust kernel memory (like USER/GDI or non-paged pool) from a user-mode application in a controlled manner. The kernel manages these resources, and they are typically allocated based on system needs and limits. Attempting to exhaust kernel memory can lead to system instability or crashes, which is not recommended.
    2. Safest and recommended way to test OOM handling: The industry-standard approach for testing OOM conditions usually involves:
      • Using tools: Consider using specialized testing tools or frameworks that can simulate OOM conditions without directly manipulating system resources. These tools can help you test how your application handles memory allocation failures gracefully.
      • Mocking memory allocation: Implementing a mock memory allocator in your application that simulates allocation failures can provide a controlled environment for testing OOM handling without risking system stability.
      • Monitoring and logging: Ensure that your application has robust logging and monitoring in place to capture how it behaves under low-memory conditions, allowing you to analyze its response without needing to exhaust kernel resources.

    In summary, while you can simulate user-mode OOM conditions effectively, directly exhausting kernel memory is not advisable. Instead, leverage testing tools and mock implementations to safely evaluate your application's memory handling capabilities.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.