---
title: memory
description: Garbage collection Torch (CUDA) memory.
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.utilities.memory
---
# memory {#memory}

Functions

- [`garbage_collection_cuda`](#lightning.pytorch.utilities.memory.garbage_collection_cuda) — Garbage collection Torch (CUDA) memory.
- `is_cuda_out_of_memory` — rtype:bool
- `is_cudnn_snafu` — rtype:bool
- `is_oom_error` — rtype:bool
- `is_out_of_cpu_memory` — rtype:bool
- [`recursive_detach`](#lightning.pytorch.utilities.memory.recursive_detach) — Detach all tensors in in_dict.

Utilities related to memory.

### `lightning.pytorch.utilities.memory.garbage_collection_cuda()` {#lightning.pytorch.utilities.memory.garbage_collection_cuda}

Garbage collection Torch (CUDA) memory.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

### `lightning.pytorch.utilities.memory.recursive_detach(in_dict, to_cpu=False)` {#lightning.pytorch.utilities.memory.recursive_detach}

Detach all tensors in in\_dict.

May operate recursively if some of the values in in\_dict are dictionaries which contain instances of Tensor. Other types in in\_dict are not affected by this utility function.

Parameters:

*   **in\_dict** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Dictionary with tensors to detach
    
*   **to\_cpu** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Whether to move tensor to cpu
    

Returns:

Dictionary with detached tensors

Return type:

out\_dict

---

**Navigation**

- Parent: [Api](/docs/pytorch/2.6.6/optional-api/api.md)
