Google Colab runtime and PyTorch CUDA memory: should cleanup be automatic after cell execution?
0 reputation · 13 Feb 2020, 11:21 UTC
Context
I'm designing a training workflow that runs on a Google Colab GPU runtime (Tesla T4 class) using PyTorch. The two components involved are the Colab hosted runtime, which owns the GPU session lifecycle, and PyTorch's CUDA caching allocator, which manages device memory within the process. The interoperability question is where responsibility for releasing GPU memory lies once a cell finishes executing.
Observed behavior and constraints
Colab's documentation does not appear to define a policy for automatic GPU garbage collection between cell executions. In practice, deleting tensors and calling torch.cuda.empty_cache() reduces what nvidia-smi reports, but it is unclear whether repeated allocation/deallocation cycles across many cells can leave fragmented memory that eventually triggers out-of-memory errors without a runtime restart. Restarting the runtime is a blunt fix because it also clears installed packages and mounted state.
Questions
- Does the Colab runtime perform any GPU memory reclamation of its own after a cell completes, or is cleanup entirely delegated to the framework's caching allocator?
- Is
empty_cache()plus tensor deletion sufficient to return to baseline usage across long sessions, or is fragmentation a real risk that only a restart resolves? - What is a reliable way to verify (e.g., via
nvidia-smisnapshots) whether memory is fully reclaimed without restarting?