Will CUDA free the memory when my application exit?

In my application, I allocate the device memory once, store it in a global variable, and launch the kernel thousands of time. (I am using the runtime API, not the device API.)

I am curious if I don’t call cudaFree() to deallocate my device memory when my application exits, will the runtime library free the memory for me? Or will the memory leak and cannot be reclaimed forever?

Not required - it’ll be freed on application exit.

Not required - it’ll be freed on application exit.

I realize that it wreaks of bad programming to not free allocated memories but are there situations in which the omissions actually affect results/performance? I am talking specifically about cudaFrees that occur right before end of the program.

I realize that it wreaks of bad programming to not free allocated memories but are there situations in which the omissions actually affect results/performance? I am talking specifically about cudaFrees that occur right before end of the program.

If the [font=“Courier New”]cudaFree[/font]'s are called immediately prior to [font=“Courier New”]return( EXIT_SUCCESS )[/font] then they can’t affect any results - those have already been written out. I don’t see how there could be any measureable effect on performance either. I suppose that it might be slightly kinder to the driver to explicitly do the [font=“Courier New”]cudaFree[/font] calls yourself, but I don’t think there will be a noticeable difference.

But I would emphasise that not explicitly free’ing memory yourself is horrible practice. When some unsuspecting individual later tries to hack the program to be called in a loop, they suddenly have a huge memory leak dropped on them. And the ‘unsuspecting individual’ can be the same programmer six months later.

If the [font=“Courier New”]cudaFree[/font]'s are called immediately prior to [font=“Courier New”]return( EXIT_SUCCESS )[/font] then they can’t affect any results - those have already been written out. I don’t see how there could be any measureable effect on performance either. I suppose that it might be slightly kinder to the driver to explicitly do the [font=“Courier New”]cudaFree[/font] calls yourself, but I don’t think there will be a noticeable difference.

But I would emphasise that not explicitly free’ing memory yourself is horrible practice. When some unsuspecting individual later tries to hack the program to be called in a loop, they suddenly have a huge memory leak dropped on them. And the ‘unsuspecting individual’ can be the same programmer six months later.

I realize that we’re getting off topic but this interests me. When a program is called in a loop, wouldn’t the program terminate after each iteration and subsequently free memory even without the presence of cudaFree statements? It seems like in order to avoid this, the hacker has to get inside the source code and construct a loop inside the code. But if he has this capability, wouldn’t he also have the capability to just delete out some cudaFree statements?

I realize that we’re getting off topic but this interests me. When a program is called in a loop, wouldn’t the program terminate after each iteration and subsequently free memory even without the presence of cudaFree statements? It seems like in order to avoid this, the hacker has to get inside the source code and construct a loop inside the code. But if he has this capability, wouldn’t he also have the capability to just delete out some cudaFree statements?

If the program is called in a loop by a script, then the memory will certainly be released each time the program runs. I was thinking of the case where (crudely) the original ‘main’ function is renamed to something else, and then compiled into another program. Obviously, while doing this the programmer could add in the missing [font=“Courier New”]cudaFree[/font] statements. However, they might assume that no one would have been so nasty as to rely on the OS to clean up after a ‘normal’ exit.

If the program is called in a loop by a script, then the memory will certainly be released each time the program runs. I was thinking of the case where (crudely) the original ‘main’ function is renamed to something else, and then compiled into another program. Obviously, while doing this the programmer could add in the missing [font=“Courier New”]cudaFree[/font] statements. However, they might assume that no one would have been so nasty as to rely on the OS to clean up after a ‘normal’ exit.