# How to avoid memory out from python list

**URL:** <https://discuss.pytorch.org/t/how-to-avoid-memory-out-from-python-list/66369>\
**Category:** Uncategorized\
**Created:** [January 11, 2020, 9:03am UTC](https://discuss.pytorch.org/t/how-to-avoid-memory-out-from-python-list/66369 "2020-01-11T09:03:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oasjd7](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/oasjd7/32/5053_2.png) [@oasjd7](https://discuss.pytorch.org/u/oasjd7)\
**Post date:** [January 11, 2020, 9:03am UTC](https://discuss.pytorch.org/t/how-to-avoid-memory-out-from-python-list/66369/1 "2020-01-11T09:03:31Z")

</div>

Hi, all.  
I want to append features to a list.  
but out of memory occurs.

Here is my codes.

```auto
for epoch in range(total_epochs):
   list_a = []
   for step, data in range(data_loader):
        ...
         if some_condition:
              list_a.append(features.cpu()) # (both cuda() and cpu() occurs memory out)
   data_loader_a = torchdata.DataLoader(list_a, ...)
   for step, data in range(data_loader_a):
       ... train process ...

```

How can I solve this problem?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [January 12, 2020, 1:40am UTC](https://discuss.pytorch.org/t/how-to-avoid-memory-out-from-python-list/66369/2 "2020-01-12T01:40:28Z")

</div>

I guess `features` is still attached to the computation graph, thus all intermediate tensors (which would be needed for a `backward` call), are still on the device.  
If you don’t need to backpropagate using the features stored in `list_a`, you could just detach the tensors before appending to the list via:

```python
list_a.append(features.detach().cpu())

```
