Some questions about the application and release of space in the heap in cuda

In my cuda program, different threads will produce operation results of different lengths. So I call malloc in kernel function A to apply for space to store these results. These results are accessed through pointers in the kernel function B, and the space for storing the results is released after the access is completed.
The problem now is that no matter whether I release the space obtained by malloc, I will encounter the problem of full heap space at the same moment (malloc fails). If I double the heap space, the program can execute for a longer time.
Question 1: I suspect that I failed to free these spaces. Is there any way for me to accurately detect whether the heap space is full?
Question 2: Besides closing the program, is there any way to clear all the data in the heap?
Question 3: Is there a way to allow the space allocated by malloc or new to occupy global memory instead of heap space?

That is sometimes referred to as a leak. The compute-sanitizer tool has a device heap leak check facility that may be of interest, although I don’t think its a direct answer to any of your questions. You can detect that the heap space is full by testing the returned pointer from new or malloc for NULL.

No, for the runtime API. In the driver API, context destruction should do it, I believe.


1 Like

Thanks for your answer, I finally found that the cause of the problem may be that some space in the global space was accidentally released when calling free(), rather than the space pointed to by the address stored in the global space. After this incident, some weird memory access or malloc errors appeared.
Thank you again for your help.

CPU stack corruption! It’s a nasty problem. It masquerades as nearly anything.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.