# How to free the GPU memory once computations are done using the device\_arrays

**URL:** <https://numba.discourse.group/t/how-to-free-the-gpu-memory-once-computations-are-done-using-the-device-arrays/1940>\
**Category:** Community Support\
**Created:** [May 15, 2023, 1:25pm UTC](https://numba.discourse.group/t/how-to-free-the-gpu-memory-once-computations-are-done-using-the-device-arrays/1940 "2023-05-15T13:25:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akshay](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/akshay/32/834_2.png) [@Akshay](https://numba.discourse.group/u/Akshay)\
**Post date:** [May 15, 2023, 1:25pm UTC](https://numba.discourse.group/t/how-to-free-the-gpu-memory-once-computations-are-done-using-the-device-arrays/1940/1 "2023-05-15T13:25:18Z")

</div>

```auto
import numpy as np
from numba import cuda
n=999999
arr = np.arange(n)
device_arr = cuda.device_array(arr)
#do some computations
#now how can we free the memory used by device_arr?

```

I have tried deleting the device\_arr variable after the computation but it is of no use.

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [May 18, 2023, 12:45pm UTC](https://numba.discourse.group/t/how-to-free-the-gpu-memory-once-computations-are-done-using-the-device-arrays/1940/2 "2023-05-18T12:45:58Z")

</div>

You could call

```python
cuda.current_context().memory_manager.deallocations.clear()

```

after removing all references to the memory.

Why do you want to free the memory eagerly? Numba will eventually clear up allocations, but it defers it in order to batch cleanup for better performance: [Memory management — Numba 0.58.0dev0+116.gbd5b553b0.dirty documentation](https://numba.readthedocs.io/en/latest/cuda/memory.html#deallocation-behavior)

---

<div class="post-metadata">

**Author:** ![Akshay](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/akshay/32/834_2.png) [@Akshay](https://numba.discourse.group/u/Akshay)\
**Post date:** [May 18, 2023, 3:25pm UTC](https://numba.discourse.group/t/how-to-free-the-gpu-memory-once-computations-are-done-using-the-device-arrays/1940/3 "2023-05-18T15:25:02Z")

</div>

Thank you @gmarkall for responding.

Assuming that I only have 16GB memory on my GPU device and there are two different datasets of 8GB each. Whenever I try to process them one by one on device I run into memory allocation error. I observed that numba keeps the data on device forever till that program is running. I want to replace the dataset on device with my second dataset and perform computations. How can I ideally achieve this?

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [May 18, 2023, 4:39pm UTC](https://numba.discourse.group/t/how-to-free-the-gpu-memory-once-computations-are-done-using-the-device-arrays/1940/4 "2023-05-18T16:39:45Z")

</div>

If you have no reference to the first dataset remaining then when you allocate the second one it should force Numba to deallocate the first one. Can you provide an executable sample of code that reproduces the issue? If there’s another reason the deallocation is prevented, I might be able to help identify it.
