kunzmi / managedCuda

ManagedCUDA aims an easy integration of NVidia's CUDA in .net applications written in C#, Visual Basic or any other .net language.
Other
440 stars 79 forks source link

Cuda Device Memory Leak #40

Closed cudaLang closed 7 years ago

cudaLang commented 7 years ago

Sample creates a memory leak on the GPU even when disposing the object.

var d_zeros = new CudaDeviceVariable < float > (1000); float [] h_zeros = new float[1000]; d_zeros = h_zeros; /// will create memory leak d_zeros.CopyToDevice(h_zeros). // will NOT create memory leak

d_zeros.Dispose();

robik75 commented 7 years ago

d_zeros = h_zeros; /// will create memory leak

That isn't necessarily true - it depends on what you consider to be a memory leak.

This implicit conversion will create a new CudaDeviceVariable and assign it to the d_zeros variable. The old CudaDeviceVariable object will not be referenced anymore and the garbage collector will be free to collect it and run the finalizer at some later point in time - that finalizer will free the GPU memory.

CudaDeviceVariable implements the IDisposable interface but you do not use it in your example... that is your fault, not a problem with managedcuda.

kunzmi commented 7 years ago

As with any class that implements the IDiposable interface, it is the programmers responsibility to make sure to call Dispose() when the object isn't needed anymore. If you lose the reference to your object by overwriting it with other references, this is then a not properly handled IDisposable-object...

When implementing the implicit converters I was aware of exactly this risk, but the comfort that one gains is definitively worse it, isn't it?

@RoBiK75 : the finalizer won't actually succeed to free the memory, as garbage collector runs on its own thread without a CudaContext assigned. The finalizer is not supposed to throw an exception, why this won't be seen until you actually run out of memory. But this problem exists for many IDisposable classes, that, if not handled correctly, unmanaged resources won't be freed.

BTW, if you run managedCuda in the debug version, it puts out warnings on the debug-console if Dispose() wasn't called by the user on some object...