# How can l clear the old cache in GPU, when training different groups of data continuously?

**URL:** <https://discuss.pytorch.org/t/how-can-l-clear-the-old-cache-in-gpu-when-training-different-groups-of-data-continuously/94650>\
**Category:** Memory Format\
**Created:** [August 31, 2020, 6:36am UTC](https://discuss.pytorch.org/t/how-can-l-clear-the-old-cache-in-gpu-when-training-different-groups-of-data-continuously/94650 "2020-08-31T06:36:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcexeexe](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/a9a28c/32.png) [@tcexeexe](https://discuss.pytorch.org/u/tcexeexe)\
**Post date:** [August 31, 2020, 6:36am UTC](https://discuss.pytorch.org/t/how-can-l-clear-the-old-cache-in-gpu-when-training-different-groups-of-data-continuously/94650/1 "2020-08-31T06:36:15Z")

</div>

I need to train the image classifiers in different groups. When one group is trained, the script will automatically load and train the next group by “for” statements.  
But an error occurred saying there was not enough memory in GPU when it start to train the next group. How can I clear the GPU memory used by the last group training before the script start train the next group?  
l have try to use torch.cuda.empty\_cache() after each group training finished but it doesn’t work.

```auto
    time.sleep(5)
    del model
    del loss
    gc.collect()
    torch.cuda.empty_cache()#清空GPU缓存
    # print(torch.cuda.memory_stats(0))
    time.sleep(10)
    torch.cuda.empty_cache()#清空GPU缓存
    # print(torch.cuda.memory_stats(0))

```

Pytorch’s version is 1.6.0

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/8/b/8b94ad2e444c53dd5cb1ad62fe8334543856d612.png)

---

<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:** [September 1, 2020, 4:54am UTC](https://discuss.pytorch.org/t/how-can-l-clear-the-old-cache-in-gpu-when-training-different-groups-of-data-continuously/94650/2 "2020-09-01T04:54:40Z")

</div>

If you delete all references to the model and other tensors, the memory can be freed or reused. [Here](https://discuss.pytorch.org/t/clearing-the-gpu-is-a-headache/84762/4) is a small example.  
Make sure you are not storing the model output, loss etc. without detaching it, as this would keep the computation graph with all intermediate tensors alive.
