# Memory leaking when using GPU acceleration of filtering and resampling

**URL:** <https://mne.discourse.group/t/memory-leaking-when-using-gpu-acceleration-of-filtering-and-resampling/3537>\
**Category:** Support & Discussions\
**Created:** [August 13, 2021, 7:23am UTC](https://mne.discourse.group/t/memory-leaking-when-using-gpu-acceleration-of-filtering-and-resampling/3537 "2021-08-13T07:23:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BarryLiu97](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/barryliu97/32/33_2.png) [@BarryLiu97](https://mne.discourse.group/u/BarryLiu97)\
**Post date:** [August 13, 2021, 7:23am UTC](https://mne.discourse.group/t/memory-leaking-when-using-gpu-acceleration-of-filtering-and-resampling/3537/1 "2021-08-13T07:23:38Z")

</div>

```python
- MNE-Python version: 0.23.0
- operating system: Ubuntu

```

Hi, I’m using CUDA to accelerate the filtering. However, I found that the memory wouldn’t release even if I used

```python
import gc
import mne

fpath = ***
raw = mne.io.read_raw_edf(fpath, preload=True)
mne.utils.set_config('MNE_USE_CUDA', 'true')
raw.notch_filter(50, n_jobs='cuda')

del raw
gc.collect()

```

If I don’t set `n_jobs = 'cuda'`, things looked fine. Is this a problem of `cupy`?  
And the usage of memory became extreamly large when filtering and it wouldn’t release until I killed the program.

---

<div class="post-metadata">

**Author:** ![BarryLiu97](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/barryliu97/32/33_2.png) [@BarryLiu97](https://mne.discourse.group/u/BarryLiu97)\
**Post date:** [August 13, 2021, 12:50pm UTC](https://mne.discourse.group/t/memory-leaking-when-using-gpu-acceleration-of-filtering-and-resampling/3537/2 "2021-08-13T12:50:49Z")

</div>

This works well in Windows10.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [August 17, 2021, 9:15pm UTC](https://mne.discourse.group/t/memory-leaking-when-using-gpu-acceleration-of-filtering-and-resampling/3537/3 "2021-08-17T21:15:22Z")

</div>

I’m tagging @larsoner here, who might have some ideas as to what’s going on.

---

<div class="post-metadata">

**Author:** ![larsoner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/larsoner/32/3_2.png) [@larsoner](https://mne.discourse.group/u/larsoner)\
**Post date:** [August 18, 2021, 3:49pm UTC](https://mne.discourse.group/t/memory-leaking-when-using-gpu-acceleration-of-filtering-and-resampling/3537/4 "2021-08-18T15:49:22Z")

</div>

We don’t do any special gymnastics with `cupy`, so I strongly suspect this is a bug at their end. @BarryLiu97 can you try allocating a bunch of cupy arrays in a loop like:

```python
for _ in range(1000):
    cupy.array(np.random.RandomState(0).randn(100, 1000))

```

and see if it causes memory usage to go up?

---

<div class="post-metadata">

**Author:** ![BarryLiu97](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/barryliu97/32/33_2.png) [@BarryLiu97](https://mne.discourse.group/u/BarryLiu97)\
**Post date:** [August 18, 2021, 4:05pm UTC](https://mne.discourse.group/t/memory-leaking-when-using-gpu-acceleration-of-filtering-and-resampling/3537/5 "2021-08-18T16:05:15Z")

</div>

In windows, it didn’t cause memory usage, while in ubuntu, the initial memory is  
 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/4bf560f9ef31b0b9f609c594f8c728182fb99f27.png)  
after running the code in the console, it became  
 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/2599243d95fcab2ecb917a843a5474129c79b342.png)  
it wouldn’t go down until I kill the program.

---

<div class="post-metadata">

**Author:** ![BarryLiu97](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/barryliu97/32/33_2.png) [@BarryLiu97](https://mne.discourse.group/u/BarryLiu97)\
**Post date:** [August 18, 2021, 4:14pm UTC](https://mne.discourse.group/t/memory-leaking-when-using-gpu-acceleration-of-filtering-and-resampling/3537/6 "2021-08-18T16:14:41Z")

</div>

I suppose that’s a Cupy problem. I see a thread here [Memory leak in python for loop even if I delete all variables at the end of each iteration - Stack Overflow](https://stackoverflow.com/questions/63061699/memory-leak-in-python-for-loop-even-if-i-delete-all-variables-at-the-end-of-each). Although it didn’t work for me.
