﻿# Preserve completion when mixing CUDA allocation and free APIs

Keep allocation provenance with cleanup: cudaFree on cudaMallocAsync memory requires accesses to be complete before the call.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/guide-perf-mixed-allocation-free-apis/
Language: en

A destructor calls cudaFree and assumes it waits for every user of its buffer. That assumption changes when the buffer originated from cudaMallocAsync. CUDA documents interoperability between allocation families, but the free contract depends on the allocation's origin. Preserve that provenance before applying familiar cleanup behavior to an asynchronously allocated object.

## Record allocation provenance

Identify which API created the buffer, its pool and device, and who owns its release. A wrapper exposing only a raw pointer can erase information its destructor needs. The same address value can later belong to another allocation, so a historical pointer log is not a reliable ownership record. Keep allocation identity and release policy together.

## Read the mixed-family rule

The programming guide permits memory from cudaMallocAsync to be released by cudaFree, but states that this path performs no further synchronization. The application must already have completed all relevant accesses. A general expectation that cudaFree is an implicit global wait is therefore insufficient for this allocation. Check the documented origin-specific rule instead of inferring it from the release function's name.

## Prove completion before cleanup

Track all consumers, including kernels or copies submitted to other streams. At a justified shutdown or object boundary, establish completion through the documented stream or event mechanisms before the synchronous free path. Merely ordering allocation in its original stream does not establish the last use elsewhere. Error cleanup and cancellation need the same proof as the normal success path.

## Review the reverse combination

The guide also permits an allocation from cudaMalloc to be released with cudaFreeAsync. In that case, the release is stream ordered and must follow all uses. The two interoperability directions should not be summarized as interchangeable cleanup. A library migration must specify which allocation family it accepts and which completion guarantee its caller must provide.

## Test the ownership interface

Use a bounded delayed consumer and repeated create-use-destroy cycles with checked results and supported memory diagnostics. Verify that the wrapper preserves provenance and the intended completion boundary instead of accidentally relying on a slow host path. Acceptance validates the mixed-family lifetime contract. It does not prove that synchronous destruction is the best performance design or that a successful free return establishes correct earlier access ordering.

## Primary references

[CUDA Stream-Ordered Allocator Guide](https://docs.nvidia.com/cuda/cuda-programming-guide/04-special-topics/stream-ordered-memory-allocation.html).

[CUDA Stream-Ordered Allocator API](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/group__CUDART__MEMORY__POOLS.html).

[CUDA Runtime Memory Management](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/group__CUDART__MEMORY.html).

Related reading: [Stream-ordered GPU allocation: the lifetime follows execution](/journal/learn-stream-ordered-allocation-lifetime/); [CUDA event dependencies: connecting producer and consumer streams](/journal/learn-cross-stream-event-dependency/).
