Memory Leak in OpenCL NVIDIA Driver From clCreateSubBuffer

System:
Linux 3.2.0-4-amd64 #1 SMP Debian 3.2.57-3 x86_64 GNU/Linux
NVIDIA Driver for Linux X64 version 340.32
Tesla K20 and K40

Issue:
There is a memory leak in the host system (not on the gpu) when calling clCreateSubBuffer, even if the memory object is released. Enough calls exhausts memory and crashes the program.

Steps to reproduce:
Here’s a github repo that will reproduce the leak: https://github.com/pjreddie/opencl_memory_leak

The leak appears from this specific section:

while(1){
    cl_buffer_region r;
    r.origin = 0*sizeof(float);
    r.size = 100*sizeof(float);
    cl_mem sub = clCreateSubBuffer(mem, CL_MEM_READ_WRITE, CL_BUFFER_CREATE_TYPE_REGION, &r, &info.error);
    check_error(info);
    info.error = clReleaseMemObject(sub);
    check_error(info);
}

I’ve tested this on a machine running the AMD OpenCL drivers and they do not have this leak.

+1, this is very frustrating.

Problem well described here (including code for reproducing):

https://bloerg.net/2013/01/15/opencl-resource-management.html
“However on NVIDIA systems, a final clReleaseMemObject() will not free the memory segment in GPU memory, if not every other OpenCL object has been freed too.”

I fixed my memory leak by adding single missed clReleaseProgram (all others objects were released by appropriate clRelease - including context and buffer, but without this single clReleaseProgram my app was still vRAM leaking)

For me problem exists on Windows (378 driver) and Linux (375 driver).