How to completely release GpuMat memory

Hello
I am using opencv 4.1.0 with cuda 10.0 on VS2015 and windows10.
I used some gpumat and opencv cuda funcions in my function. I want to release my GpuMat memory completely because maybe my function will be called simultaneously in different threads and i don’t want to cause a gpu memory explosion. So i tried gpumat.release() function, but it seemed that the memory is not released completely.
Here is a simple test.

	size_t free, total;
	cudaMemGetInfo(&free, &total);
	auto s0 = total - free;
	auto s1 = total - free;
	std::cout <<"used memory:"<< total - free << std::endl;
	
	//first gpumat
	cv::cuda::GpuMat m1 = cv::cuda::GpuMat(cv::Size(2000, 1000), CV_32FC1, cv::Scalar(0));
	cudaMemGetInfo(&free, &total);
	s1 = total - free;
	std::cout << "used memroy:" << total - free <<" Difference: "<< (int)s1 - (int)s0 << std::endl;
	s0 = total - free;
	//second gpumat
	cv::cuda::GpuMat m2 = cv::cuda::GpuMat(cv::Size(2000, 1000), CV_32FC1, cv::Scalar(0));
	cudaMemGetInfo(&free, &total);
	s1 = total - free;
	std::cout << "used memroy:" << total - free << " Difference: " << (int)s1 - (int)s0 << std::endl;
	s0 = total - free;
	//third gpumat
	cv::cuda::GpuMat m3 = cv::cuda::GpuMat(cv::Size(2000, 1000), CV_32FC1, cv::Scalar(0));
	cudaMemGetInfo(&free, &total);
	s1 = total - free;
	std::cout << "used memroy:" << total - free << " Difference: " << (int)s1 - (int)s0 << std::endl;
	s0 = total - free;
	//release
	m1.release();
	m2.release();
	m3.release();
	cudaMemGetInfo(&free, &total);
	s1 = total - free;
	std::cout << "used memroy:" << total - free << " Difference: " << (int)s1 - (int)s0 << std::endl;

Here is the output of the code above:

used memory:375531109
used memroy:470754917 Difference: 95223808
used memroy:478946917 Difference: 8192000
used memroy:487138917 Difference: 8192000
used memroy:462562917 Difference: -24576000

As you can see, after i release all three gpumat, used memory is still a lot bigger than it was at the first time.
Also, when first gputmat was constructed, the memory difference is much bigger than the second and third one.
So my question is: how can i make “used memory” back to its original size?

Hello, I think the memory will be automatically freed when GpuMat’s life cycle ends

Hi, on your first call the cuda context is initialized which uses 83MB (95223808 - 8192000 = 87,031,808). This is not released when you release the last piece of device memory you have allocated. It stays initialized so that future calls don’t incure the overhead of allocating the context.