LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Kuko on August 02, 2011, 15:10:00

Title: OpenCL Events causing memory leaks
Post by: Kuko on August 02, 2011, 15:10:00
Hi, I have realized that any call to an clEnqueue* Command, like clEnqueueNDRangeKernel, with the last argument (PointerBuffer event) not set to null consumes a certain amount of System Memory, which is not released afterwards.
You can easily verify this by changing line 60 in the OpenCL Sum Example

clEnqueueNDRangeKernel(queue, kernel, 1, null, kernel1DGlobalWorkSize, null, null, null);


to

PointerBuffer ev = new PointerBuffer(1); 
for(int i=0; i<10000000;i++){
       clEnqueueNDRangeKernel(queue, kernel, 1, null, kernel1DGlobalWorkSize, null, null, ev);
}


for Example. Note: executing this will burn your main memory in seconds.
I have verified this on an Intel CPU, an Ati GPU and an Nvidia GPU with Linux and Windows. The memory used by GPU remains constant.

Is this a bug or is there a way to manually release the memory?
Title: Re: [BUG] OpenCL Events causing memory leaks
Post by: spasi on August 02, 2011, 17:27:06
CLEvent, like most CL objects, is "retainable". That means, it has a reference count that can be increased (clRetain<Type>) or decreased (clRelease<Type>). After a call to clEnqueueNDRangeKernel, the reference count is 1, so that's why you're seeing an increase in memory usage. This is how you get rid of the leak:

PointerBuffer ev = new PointerBuffer(1); 
for(int i=0; i<10000000;i++){
       clEnqueueNDRangeKernel(queue, kernel, 1, null, kernel1DGlobalWorkSize, null, null, ev);
       CLEvent e = queue.getCLEvent(ev.get(0));
       ... wait on event or whatever ...
       clReleaseEvent(e);
}
Title: Re: [BUG] OpenCL Events causing memory leaks
Post by: Kuko on August 03, 2011, 09:19:42
Thank you, spasi.