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
- Build the program at the end of this post (x64, VS 2022 Developer Command Prompt):
cl /EHsc nvapi_leak_min.cpp - Run it. It calls
NvAPI_GPU_GetMemoryInfoEx10,000 times and measures how far the D3DKMT handle index advances (it opens and closes one adapter handle withD3DKMTOpenAdapterFromLuidbefore 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;
}