PyTorch torch.cuda.memory_allocated function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.cuda.memory_allocatedis a function in PyTorch used to get the allocated GPU memory. It returns the size of memory allocated on the current CUDA device (in bytes).

Function Definition

torch.cuda.memory_allocated(device=None)

Usage Example

Example

import torch

# Check allocated GPU memory
if torch.cuda.is_available():
    print(f"Initial memory: {torch.cuda.memory_allocated()} bytes")

    # Create tensor
    x = torch.randn(1000, 1000).cuda()
    print(f"After allocation: {torch.cuda.memory_allocated()} bytes")

    # Delete tensor
    del x
    print(f"After deletion: {torch.cuda.memory_allocated()} bytes")
else:
    print("CUDA not available")

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions