# How do I release memory after using AutoModel.from\_pretrained() to load model

**URL:** <https://discuss.huggingface.co/t/how-do-i-release-memory-after-using-automodel-from-pretrained-to-load-model/107705>\
**Category:** Beginners\
**Created:** [September 22, 2024, 8:11am UTC](https://discuss.huggingface.co/t/how-do-i-release-memory-after-using-automodel-from-pretrained-to-load-model/107705 "2024-09-22T08:11:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon59317333](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon59317333](https://discuss.huggingface.co/u/anon59317333)\
**Post date:** [September 22, 2024, 8:11am UTC](https://discuss.huggingface.co/t/how-do-i-release-memory-after-using-automodel-from-pretrained-to-load-model/107705/1 "2024-09-22T08:11:50Z")

</div>

after execute following code on apple’s silicon chip , even though the program ends memory is still occupied too much.  
sorry i’m a beginner of both English and Deep learning. Is it a bug or just I used a wrong way?

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/4/9/4915d5f242a0041870e8bb997512b9b6365d3b03.png)

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [September 22, 2024, 8:36am UTC](https://discuss.huggingface.co/t/how-do-i-release-memory-after-using-automodel-from-pretrained-to-load-model/107705/2 "2024-09-22T08:36:52Z")

</div>

I generally do it like this, but it’s probably not right and there are smarter ways to do it. I’m interested too.

```py
import torch
import gc
del llm_model
torch.cuda.empty_cache()
gc.collect()

```

---

<div class="post-metadata">

**Author:** ![anon59317333](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon59317333](https://discuss.huggingface.co/u/anon59317333)\
**Post date:** [September 22, 2024, 8:49am UTC](https://discuss.huggingface.co/t/how-do-i-release-memory-after-using-automodel-from-pretrained-to-load-model/107705/3 "2024-09-22T08:49:50Z")

</div>

thank you, actully `del llm_model` works, the problem is the memory monitor didn’t refresh 🥲

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [September 22, 2024, 9:42am UTC](https://discuss.huggingface.co/t/how-do-i-release-memory-after-using-automodel-from-pretrained-to-load-model/107705/4 "2024-09-22T09:42:44Z")

</div>

Hmmm, like this?  
In your case anyway, it would be better to remove it from VRAM first and then delete it.

But well, the torch tensor used in the model’s content is quite stubborn…  
If you don’t delete the tensors in the model, not the pipes or the model, one by one, they might be referenced from somewhere and won’t disappear.

```py
import torch
import gc
llm_model.to("cpu")
del llm_model
torch.cuda.empty_cache()
gc.collect()

```

It is more reliable to **del** together any relevant models and pipes.

---

<div class="post-metadata">

**Author:** ![anon59317333](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon59317333](https://discuss.huggingface.co/u/anon59317333)\
**Post date:** [September 24, 2024, 11:19am UTC](https://discuss.huggingface.co/t/how-do-i-release-memory-after-using-automodel-from-pretrained-to-load-model/107705/5 "2024-09-24T11:19:53Z")

</div>

thank you! it’s helpful

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [September 24, 2024, 11:20pm UTC](https://discuss.huggingface.co/t/how-do-i-release-memory-after-using-automodel-from-pretrained-to-load-model/107705/6 "2024-09-24T23:20:17Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
