Skip to content

[bug] fix long-standing memory leakage, due to unused event, fix #127#128

Merged
fangq merged 1 commit into
masterfrom
memleakfix
Jun 14, 2026
Merged

[bug] fix long-standing memory leakage, due to unused event, fix #127#128
fangq merged 1 commit into
masterfrom
memleakfix

Conversation

@fangq

@fangq fangq commented Jun 13, 2026

Copy link
Copy Markdown
Owner

with the help of Claude code, the long-standing memory leakage on NVIDIA GPUs in #127 has been finally fixed.

it turns out the issue is related to NVIDIA memory management related to cl_event object - when an event is passed to the queue, even the event is not used, clReleaseCommandQueue won't actually release the resources.

the fix is to remove the unused waitevent object.

after the fix, the benchmark show no memory leakage.

@fangq fangq merged commit d5adcf8 into master Jun 14, 2026
39 checks passed
@fangq

fangq commented Jun 14, 2026

Copy link
Copy Markdown
Owner Author

here is the diagnosis from Claude

● I've found the root cause. Let me confirm the diagnosis is complete before fixing. The key facts:

  • Default build uses -DUSE_OS_TIMER (src/Makefile:9), so the kernel launch at line 698 (the #else branch) writes a cl_event into waittoread[devid].
  • kernelevent only exists in the non-OS-timer build (mmc_tictoc.c:49), so the clReleaseEvent(kernelevent) at line 1542 is compiled out in your build.
  • The events written to waittoread (line 698 kernel launch, line 742 reporter read, line 776 debug read) are never released, and the clWaitForEvents that would consume them is commented out (line 725). Synchronization actually
    happens via clFinish.

This is the leak. Here's why it's NVIDIA-specific:

In OpenCL, clReleaseMemObject, clReleaseCommandQueue, and clReleaseContext only decrement reference counts — they don't free anything while a reference remains. Every outstanding cl_event holds an implicit reference to its
command queue and context. NVIDIA's runtime honors this strictly: as long as those leaked events live, the context's refcount never reaches zero, so the GPU allocations behind it are never freed. AMD/Intel/CPU runtimes reap
completed events (and their references) much more eagerly, so you don't see the leak there.

Let me look at the exact lines to fix.

Read 1 file (ctrl+o to expand)

● The diagnosis is complete. The events in waittoread are never consumed — the clWaitForEvents is commented out (line 725) and synchronization is done via clFinish (lines 727, 865) and blocking reads (CL_TRUE). So the safest,
cleanest fix is to stop creating these orphan events by passing NULL for the event argument. That removes the references pinning the NVIDIA context alive.

@fangq fangq deleted the memleakfix branch June 14, 2026 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant