# Unreasonable GPU memory usage with PyCall

**URL:** <https://discourse.julialang.org/t/unreasonable-gpu-memory-usage-with-pycall/13700>\
**Category:** General Usage\
**Created:** [August 19, 2018, 3:58am UTC](https://discourse.julialang.org/t/unreasonable-gpu-memory-usage-with-pycall/13700 "2018-08-19T03:58:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiucheng\_Li](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiucheng_li/32/9773_2.png) [@Xiucheng\_Li](https://discourse.julialang.org/u/Xiucheng_Li)\
**Post date:** [August 19, 2018, 3:58am UTC](https://discourse.julialang.org/t/unreasonable-gpu-memory-usage-with-pycall/13700/1 "2018-08-19T03:58:32Z")

</div>

The GPU memory usage grows with the iteration when calling PyTorch with PyCall, I have opened an issue at [github](https://github.com/JuliaPy/PyCall.jl/issues/529). It seems that PyTorch is unaware of the release of the local variable and I am wondering whether this is related to the Julia’s memory management mechanism. A further simplified code is shown as follows:

Julia

```julia
using PyCall

@pyimport torch

device = torch.device(ifelse(torch.cuda[:is_available](), "cuda", "cpu"))
println(device)

for epoch = 1:20
    for i = 1:100
        Xs = torch.rand(300, 800)[:to](device)
    end
    sleep(2)
    println("Epoch: $epoch")
end

```

Python

```python
import torch
import time

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)

for epoch in range(1, 21):
    for i in range(0, 100):
        Xs = torch.rand(300, 800).to(device)
    time.sleep(2)
    print("Epoch: ", epoch)

```

---

<div class="post-metadata">

**Author:** ![JosePereiraUA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiraua/32/37640_2.png) [@JosePereiraUA](https://discourse.julialang.org/u/JosePereiraUA)\
**Post date:** [November 15, 2020, 3:29pm UTC](https://discourse.julialang.org/t/unreasonable-gpu-memory-usage-with-pycall/13700/2 "2020-11-15T15:29:26Z")

</div>

Having the same issue. Is there a fix?

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [November 15, 2020, 4:58pm UTC](https://discourse.julialang.org/t/unreasonable-gpu-memory-usage-with-pycall/13700/3 "2020-11-15T16:58:50Z")

</div>

The [linked issue thread](https://github.com/JuliaPy/PyCall.jl/issues/529) seems to indicate `GC.gc(false)` works well enough.

---

<div class="post-metadata">

**Author:** ![JosePereiraUA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiraua/32/37640_2.png) [@JosePereiraUA](https://discourse.julialang.org/u/JosePereiraUA)\
**Post date:** [November 16, 2020, 12:12pm UTC](https://discourse.julialang.org/t/unreasonable-gpu-memory-usage-with-pycall/13700/4 "2020-11-16T12:12:54Z")

</div>

Can confirm this. Eventually used `GC.gc(false)` to more or less fix the issue.
