memory leak in driver?

Hi All,

I was fighting for a long time with memory leaks control in my application. I figured out that cuda API does not free the resources as I would expect.

The following code

#include <stdlib.h>

#include <stdio.h>

#include <sys/sysinfo.h>

#include <cuda_runtime_api.h>

#include <cutil_inline.h>

void PrintFreeMemory()


	struct sysinfo myinfo;

	unsigned long total_bytes;


	total_bytes = myinfo.mem_unit * myinfo.freeram;

	printf(" usable main memory is %lu B, %lu MB\n",

	       total_bytes, total_bytes/1024/1024);


int main( int argc, char** argv )


	printf("Starting point :\t\t");



	printf("After cudaSetDevice :\t\t");


	cudaEvent_t startEvent;

	cutilSafeCall( cudaEventCreate( &startEvent ) );

	printf("After creating an event :\t");


	cutilSafeCall( cudaEventDestroy( startEvent) );

	printf("After destroying an event :\t");



	printf("After cudaThreadExit :\t\t");



gives the result like:

Starting point :		 usable main memory is 908730368 B, 866 MB

After cudaSetDevice :		 usable main memory is 903778304 B, 861 MB

After creating an event :	 usable main memory is 888094720 B, 846 MB

After destroying an event :	 usable main memory is 888094720 B, 846 MB

After cudaThreadExit :		 usable main memory is 903524352 B, 861 MB

Can somebody please explain me what I do wrong? I use Ubuntu 10.4, driver 195.36.24 and CUDA 3.0.



I don’t think you can conclude that is a memory leak. If you run your application 10,000 times on an otherwise completely idle system and it started thrashing or the kernel OOM killer started to kick in, then you might conclude something out of your control was leaking memory. But otherwise I don’t think so.

You should also realize that most implementations of malloc() and free() do not always release freed memory back to the operating system immediately. This can be an optimization for speed since your process could reuse that freed space for a future malloc() call, or some of the memory space might not be returnable to the OS due to memory fragmentation. Either way, I agree with avidday that it is basically impossible to conclude a memory leak from one execution of the sample code you provide.