Are resources released automatically?

Hi. If I create OpenCL resources/objects like context, device, buffers, etc. and do not call the release function afterwards are those resources now forever on my GPU (until I restart my PC)?

I just have this question because in a lot of tutorials new buffers are created with clCreateBuffer and they are wrote to and red from but I never see a clReleaseMemObject.

I believe the host (your CPU app) owns the context, and the context owns the resources. So when your app terminates, the context should be cleaned up and the resources it owns freed.

For a hint as to how this might be done, see the following link. Some OpenCL implementations expose an API to trigger “fast context cleanup” (outside of the normal ref counting cleanup), and program termination is mentioned as one use case for this. See:

Thx for the reply. But clTerminateContextKHR nor clTerminateContext nor CL_DEVICE_TERMINATE_CAPABILITY_KHR is in OpenCL 3.0 anymore, so it’s not relevant to new devices.

I just wonder what happens with the data that I wrote to a cl_mem object when my program terminates? Will the memory block I reserved with clCreateBuffer of the specified size be now forever in my GPU RAM? Also the program that I build, will it be forever in my GPU RAM?

What happens if you malloc memory but never free it (or any other equivalents)?

The answer for CPU memory is no different than the answer for GPU memory. Interacting with either requires OS permission. The OS knows what memory you can access, and it knows when your process has ended. It therefore will release those resources when your program terminates.

Ok, great. :slight_smile: If it works, I won’t complain.