NvAPI_GPU_GetMemoryInfo / NvAPI_GPU_GetMemoryInfoEx leak D3DKMT adapter handles on every call

Summary

Every call to NvAPI_GPU_GetMemoryInfo or NvAPI_GPU_GetMemoryInfoEx enumerates all adapters with D3DKMTEnumAdapters3, which opens one adapter handle per adapter, but only the handle of the queried GPU is closed afterwards. The other handles are never released. On a system with N adapters, each call leaks N-1 D3DKMT handles.

Our application that polls GPU memory once per frame (for example, the bgfx D3D11 renderer does this through NvAPI_GPU_GetMemoryInfo) uses up the per-process D3DKMT handle space after several hours. From then on, every DXGI/D3D call that needs a new adapter handle fails with E_OUTOFMEMORY: CreateDXGIFactory1, D3D11CreateDevice, IDXGIFactory::CreateSwapChain, and also Present and command list submission inside existing devices. The device itself is not removed (GetDeviceRemovedReason returns S_OK), and system memory is not exhausted.

IDXGIAdapter3::QueryVideoMemoryInfo, used as a control, does not leak.

We are aware that NvAPI_GPU_GetMemoryInfo has been deprecated since R520. However, NvAPI_GPU_GetMemoryInfoEx, the replacement recommended in the current SDK (R615, nvapi_lite_common.h), leaks in exactly the same way. The repro uses the NV_GPU_MEMORY_INFO_EX_V1 layout from the SDK header.

Environment

  • Windows 10 21H2 (10.0.19044.2604), x64
  • GeForce RTX 5070, driver 610.88 (32.0.16.1088)
  • The same E_OUTOFMEMORY failures after long uptime were seen on user machines with GeForce RTX 2060 / RTX 2060 SUPER, drivers 616.92 (32.0.16.1692) and 617.14 (32.0.16.1714)
  • The test machine reports 5 adapters to D3DKMTEnumAdapters3: four “NVIDIA GeForce RTX 5070” entries (there is one physical GPU; a virtual display driver is installed, but we have not determined why there are four entries) and Microsoft Basic Render Driver. Systems with a single adapter would presumably not show the leak.

Steps to reproduce

  1. Build the program at the end of this post (x64, VS 2022 Developer Command Prompt): cl /EHsc nvapi_leak_min.cpp
  2. Run it. It calls NvAPI_GPU_GetMemoryInfoEx 10,000 times and measures how far the D3DKMT handle index advances (it opens and closes one adapter handle with D3DKMTOpenAdapterFromLuid before and after the loop).

Actual result

adapters: 5
NvAPI_GPU_GetMemoryInfoEx: 4.00 handles leaked per call

NvAPI_GPU_GetMemoryInfo behaves the same (+4.00 per call), while IDXGIAdapter3::QueryVideoMemoryInfo stays at 0. When the calls continue, after about 4.2 million calls (handle index close to 2^24) CreateDXGIFactory1 returns 0x8007000E (E_OUTOFMEMORY) and keeps failing for the rest of the process lifetime.

Expected result

All handles opened inside the call are closed before it returns, so the handle count stays constant.

Kernel thunks per call

Traced with breakpoints on the win32u!NtGdiDdDDI* / win32u!NtDxgk* stubs, counting only the calls made between two markers around a loop of NvAPI_GPU_GetMemoryInfo (10 and 20 iterations; the counts scale exactly):

Thunk Calls per NvAPI_GPU_GetMemoryInfo
NtDxgkEnumAdapters3 (D3DKMTEnumAdapters3) 2 (first with NumAdapters=0 to get the count, then with a buffer)
NtGdiDdDDIQueryAdapterInfo 10
NtGdiDdDDIEscape 5
NtGdiDdDDICloseAdapter 1

The second D3DKMTEnumAdapters3 returns 5 adapters, each with a newly opened hAdapter. Only the handle of the queried GPU (first entry, LUID 0x3:0xDA1D55A6) is closed:

EA3 out num=5   handles: 400011c0 (LUID ...da1d55a6) 40001200 40001240 40001280 400012c0

CLOSE h=400011c0

EA3 out num=5 handles: 40001300 (LUID ...da1d55a6) 40001340 40001380 400013c0 40001400

CLOSE h=40001300

The calls originate in nvapi64_impl.dll (loaded from the driver store by nvapi64.dll). The same growth (+4 per call) is seen with NvAPI_GPU_GetMemoryInfoEx.

Impact and workaround

In our D3D11 application, the per-frame NvAPI_GPU_GetMemoryInfo call in bgfx caused crashes after about 14–25 hours of uptime (sometimes after several days, depending on frame rate and adapter count), always with E_OUTOFMEMORY from DXGI/D3D11 while the device was not removed. We replaced the call with IDXGIAdapter3::QueryVideoMemoryInfo, which fixed the crashes. Other applications that poll these NvAPI functions periodically are likely affected on systems with more than one adapter.

Repro source (nvapi_leak_min.cpp)

// cl /EHsc nvapi_leak_min.cpp   -- prints how many D3DKMT handles each NvAPI_GPU_GetMemoryInfoEx call leaks
#include <windows.h>
#include <d3dkmthk.h>
#include <cstdio>
#pragma comment(lib, "gdi32.lib")

struct NV_GPU_MEMORY_INFO_EX_V1 { UINT32 version; UINT64 fields[9]; };
typedef void* (__cdecl* QueryInterface)(UINT32);

// D3DKMT handles get increasing indices; open+close one handle to read the next index.
static UINT NextHandleIndex(LUID luid)
{
    D3DKMT_OPENADAPTERFROMLUID open = {};
    open.AdapterLuid = luid;
    D3DKMTOpenAdapterFromLuid(&open);
    D3DKMT_CLOSEADAPTER close = { open.hAdapter };
    D3DKMTCloseAdapter(&close);
    return (open.hAdapter >> 6) & 0xFFFFFF;
}

int main()
{
    QueryInterface qi = (QueryInterface)GetProcAddress(LoadLibraryW(L"nvapi64.dll"), "nvapi_QueryInterface");
    ((int (__cdecl*)())qi(0x0150E828))();                                                   // NvAPI_Initialize
    void* gpus[64] = {};
    UINT32 count = 0;
    ((int (__cdecl*)(void**, UINT32*))qi(0xE5AC921F))(gpus, &count);                        // NvAPI_EnumPhysicalGPUs
    auto getMemoryInfoEx = (int (__cdecl*)(void*, NV_GPU_MEMORY_INFO_EX_V1*))qi(0xC0599498); // NvAPI_GPU_GetMemoryInfoEx

    D3DKMT_ENUMADAPTERS2 enumAdapters = {};
    D3DKMTEnumAdapters2(&enumAdapters);
    D3DKMT_ADAPTERINFO adapters[64];
    enumAdapters.pAdapters = adapters;
    D3DKMTEnumAdapters2(&enumAdapters);
    printf("adapters: %u\n", enumAdapters.NumAdapters);

    const int calls = 10000;
    UINT before = NextHandleIndex(adapters[0].AdapterLuid);
    for (int i = 0; i < calls; ++i)
    {
        NV_GPU_MEMORY_INFO_EX_V1 info = {};
        info.version = sizeof(info) | (1 << 16);
        getMemoryInfoEx(gpus[0], &info);
    }
    UINT after = NextHandleIndex(adapters[0].AdapterLuid);
    printf("NvAPI_GPU_GetMemoryInfoEx: %.2f handles leaked per call\n", (after - before) / double(calls));
    return 0;
}

I can confirm this issue. I encountered it while investigating frame-time stalls in the MGS4 PC port and reported my findings directly to an NVIDIA engineer on October 8.

For context, I’m an independent researcher, not affiliated with Konami or NVIDIA.

I’ve reproduced the leak on two separate systems:

  • Windows 10 Pro, build 19045.6466

    • RTX 4090, Driver 616.56 and 621.01
    • AMD Ryzen 9 7950X3D
    • Six enumerated adapters, approximately five leaked D3DKMT handles per NVAPI query
  • Windows 11 Pro, build 26200.9457

    • RTX 3070 Ti Laptop, Driver 617.42
    • Intel Core i9-12900H
    • Five enumerated adapters, approximately four leaked handles per NVAPI query

I also tested the RTX 4090 with all additional display adapters disabled except Microsoft Basic Render Driver. In that configuration, only one handle leaks per query, confirming that additional enumerated adapters amplify the problem rather than cause it.

One additional consequence I’ve measured is progressively worsening latency. As the leaked handles accumulate, dxgkrnl periodically expands its per-process handle table, resulting in increasingly expensive stalls.

I captured a 52 ms stall with the following kernel callstack:


ntoskrnl!KeFlushProcessWriteBuffers

ntoskrnl!KeFlushTb

ntoskrnl!MiFlushEntireTbDueToAttributeChange

ntoskrnl!MiChangePageAttributeBatch

ntoskrnl!MiGetPageChain

ntoskrnl!MiResolvePrivateZeroFault

ntoskrnl!KiPageFault

dxgkrnl!memcpy+0x1D3

dxgkrnl!HMGRTABLE::ExpandTable+0x92

dxgkrnl!HMGRTABLE::AllocHandle+0x7B

dxgkrnl!DXGPROCESS::AllocHandleSafe+0x46

dxgkrnl!DXGADAPTER::CreateHandle+0x4D

dxgkrnl!EnumAdaptersCallback+0x103

dxgkrnl!DXGGLOBAL::IterateAdaptersWithCallback+0x190

dxgkrnl!DxgkEnumAdaptersInternal+0xD6

dxgkrnl!DxgkEnumAdapters3Impl+0x2C6

dxgkrnl!NtDxgkEnumAdapters3+0x15

win32u!NtDxgkEnumAdapters3+0x14

nvapi64_impl+0x4B68E

nvapi64_impl+0x3B1EE6

nvapi64_impl+0x3B35EE

nvapi64_impl+0x38F3FB

nvapi64_impl+0x38F1D3

nvapi64_impl+0x19B945

nvapi64_impl+0x19B38F <-- Inside NvAPI_GPU_GetMemoryInfo

With five leaked handles per query, the periodic expansion stalls grow by approximately 2.16 ms per 100,000 calls.

As a control, repeatedly calling D3DKMTEnumAdapters3 directly and explicitly closing every returned handle keeps the table stable over millions of iterations.

I’ve also reproduced the behavior with both NvAPI_GPU_GetMemoryInfo and NvAPI_GPU_GetMemoryInfoEx.

I have a standalone reproducer and detailed logs from both machines, including latency measurements, if they’re useful.

API comparison and handle leakage

Test Calls Leaked handles/call DXGK handle table (start → end) Calls ≥1 ms Worst call Median stall growth / 100k calls
NvAPI_GPU_GetMemoryInfoEx 853,374 5.00 21 → 4,266,904 3,678 20.92 ms +2.16 ms
NvAPI_GPU_GetMemoryInfo (deprecated) 853,169 5.00 21 → 4,265,879 3,676 22.83 ms +2.12 ms
D3DKMTEnumAdapters3 + close all handles (control) 3,124,962 0.00 10,065 → 10,047 7 1.41 ms None observed

Progressive latency degradation

Cumulative calls GetMemoryInfoEx stall median GetMemoryInfo stall median
150,000 2.22 ms 2.27 ms
300,000 5.64 ms 5.64 ms
450,000 8.51 ms 8.71 ms
600,000 11.98 ms 11.87 ms
750,000 15.25 ms 15.28 ms
850,000 17.24 ms 17.28 ms

These measurements were taken on an RTX 4090 with NVIDIA driver 616.56 (Windows 10, build 19045.6466), where D3DKMTEnumAdapters3(Filter=0x3) enumerates six adapter entries. Both NVAPI functions leak approximately five handles per invocation.

The ordinary calls remain fast (~8 µs median), but periodic DXGK handle-table expansions become progressively more expensive as leaked handles accumulate. System paged-pool usage also increased by approximately 57 MB for GetMemoryInfoEx and 64 MB for GetMemoryInfo, with the resources released upon process termination.

As a control, explicitly closing all handles returned by D3DKMTEnumAdapters3 kept the handle table stable over more than three million calls, without the progressive latency degradation.