torch.empty_cache
function empty_cache(): voidReleases all unoccupied cached memory from the allocator.
The WebGPU memory allocator maintains a pool of allocated buffers for efficient reuse, avoiding costly GPU allocations. This function frees all pooled (cached) memory that is not currently in use by tensors. Call this to reclaim GPU memory between major computation phases, after inference or evaluation loops, or when switching between different models. Useful for:
- Freeing GPU memory after inference
- Preparing GPU for a large memory-intensive operation
- Reducing memory footprint between training phases
- Working with memory-constrained devices
- Reducing memory fragmentation
- Only frees pooled memory: Active tensor allocations are not freed
- No performance penalty: Safe to call frequently; doesn't affect computation
- Automatic pooling: Memory is automatically pooled again as you create tensors
- Use after inference: Call this after inference loops to reduce GPU memory usage
- Doesn't free tensor data: Only releases cached buffers not in use
- May not help much: If most memory is active (tensors), empty_cache() won't free much
- Not automatic: Unlike Python, this must be called manually (no garbage collection)
Returns
void
Examples
// Process first batch and clear cache before large operation
const result1 = model.forward(batch1);
result1.sum().backward();
// Free pooled memory before processing large second batch
torch.webgpu.empty_cache();
// Process second batch with more available memory
const result2 = model.forward(batch2);
result2.sum().backward();
// Clear cache after inference to free memory
const predictions = model.forward(testData);
torch.webgpu.empty_cache();
// Monitoring memory usage
console.log('Before clear:', torch.webgpu.memory_summary());
torch.webgpu.empty_cache();
console.log('After clear:', torch.webgpu.memory_summary());See Also
- PyTorch torch.cuda.empty_cache()
- memory_stats - Check memory usage before and after calling empty_cache()
- memory_summary - Get formatted memory information
- reset_peak_memory_stats - Reset peak memory counter for profiling