cudaFree

Release GPU memory from cudaMalloc

Syntax

cpp
1
cudaError_t cudaFree(void *devPtr);

Parameters

devPtr
A pointer returned by cudaMalloc or cudaMallocManaged, or nullptr.

Returns

cudaSuccess
The memory was released (or devPtr was nullptr).
cudaErrorInvalidValue
"invalid argument": the pointer was already freed or did not come from a device allocator.
cudaErrorCudartUnloading
"driver shutting down": called after main returned, for example from a global object's destructor.
errors from earlier launches
Like most runtime calls, it can also return an error left by an earlier asynchronous kernel, such as cudaErrorIllegalAddress.

How it works

It waits for the GPU

On our T4, cudaFree called while a 0.3 s kernel was running returned only after the kernel finished. Freeing inside a hot loop therefore serialises the CPU and the GPU.

Match each allocator with its free

cudaMalloc and cudaMallocManaged pair with cudaFree, cudaMallocHost with cudaFreeHost, cudaMallocAsync with cudaFreeAsync, and malloc with free.

Free before main returns

Global or static objects are destroyed after the CUDA runtime starts shutting down, and cudaFree then fails with "driver shutting down". Release GPU memory in main or in objects that go out of scope before it returns.

Example

Press Run to execute this on a real NVIDIA GPU in the playground.

Example: a leak and its fix, measured with cudaMemGetInfo
main.cu
123456789101112131415161718192021222324252627282930313233343536373839404142434445
#include <cstdio>
#include <cstdlib>
#include <cuda_runtime.h>

#define CUDA_CHECK(call)                                                  \
    do {                                                                  \
        cudaError_t err_ = (call);                                        \
        if (err_ != cudaSuccess) {                                        \
            fprintf(stderr, "%s:%d: %s (%s)\n", __FILE__, __LINE__,       \
                    cudaGetErrorString(err_), cudaGetErrorName(err_));    \
            exit(1);                                                      \
        }                                                                 \
    } while (0)

double usedMiB() {
    size_t freeB, totalB;
    CUDA_CHECK(cudaMemGetInfo(&freeB, &totalB));
    return (totalB - freeB) / 1048576.0;
}

int main() {
    const size_t bytes = 64 << 20;   // 64 MiB per iteration
    double start = usedMiB();

    for (int i = 0; i < 10; i++) {   // leak: never freed
        float *d;
        CUDA_CHECK(cudaMalloc(&d, bytes));
    }
    printf("after leaking loop: +%.0f MiB\n", usedMiB() - start);

    double mid = usedMiB();
    for (int i = 0; i < 10; i++) {   // fixed: free every buffer
        float *d;
        CUDA_CHECK(cudaMalloc(&d, bytes));
        CUDA_CHECK(cudaFree(d));
    }
    printf("after fixed loop:   +%.0f MiB\n", usedMiB() - mid);

    float *d;
    CUDA_CHECK(cudaMalloc(&d, bytes));
    CUDA_CHECK(cudaFree(d));
    cudaError_t err = cudaFree(d);   // double free
    printf("second cudaFree: %s\n", cudaGetErrorString(err));
    return 0;
}
Output on an NVIDIA T4
after leaking loop: +640 MiB
after fixed loop:   +0 MiB
second cudaFree: invalid argument

Common mistakes

Double free

Set the pointer to nullptr after freeing it. A second cudaFree(nullptr) is then harmless.

Freeing host memory with cudaFree

Pointers from malloc, new or cudaMallocHost return "invalid argument". Use the free function that matches the allocator.

Ignoring the return value

Because cudaFree waits for earlier kernels, it is often where an illegal memory access from one of them is first reported. Check it like any other call.

FAQ

Does memory get freed when my program exits?

Yes. The driver releases all of a process's GPU memory when it exits. Leaks matter in long-running programs such as servers, training loops and notebooks.

Is it safe to call cudaFree(nullptr)?

Yes. It does nothing and returns cudaSuccess, just like free(NULL).