How to get free memory size on the device?

During the execution of my program the user can change the size of data to be allocated on the device. So program crashes on large sizes of data. Access violation(Windows) or Segmentation fault(Linux) errors are appeared during execution following line:

clCreateBuffer(cxGPUContext, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, new_size * sizeof(float), buffer , &ciErrNum);

I need to know the maximum size of data that can be allocated.
Is it any way to know the amount of free memory on the device?

Running out of device memory should not produce the error messages you are getting. You are sending in CL_MEM_COPY_HOST_PTR to clCreateBuffer. Are you sure that new_size*sizeof(float) bytes can be read from the ‘buffer’ pointer your are passing as well?

Regarding the amount of currently free memory, I don’t know any way to do that.

There is a function clGetDeviceInfo (, which can be called with flags CL_DEVICE_GLOBAL_MEM_SIZE and CL_DEVICE_LOCAL_MEM_SIZE.
This gives you the max amount of memory available and then you can keep track of what you’ve allocated so far. Or at least you could estimate what the max size of data you can handle and either split larger data into chunks or not allow users to go over the limit.
Also setting env variable CL_LOG_ERRORS to “stdout” gives more meaningful error messages/ warning.

Thanks a lot for your help. My problem has been fixed.