# When calling PyTorch using PyCall or pythoncall, I run out of memory on GPU cards

**URL:** <https://discourse.julialang.org/t/when-calling-pytorch-using-pycall-or-pythoncall-i-run-out-of-memory-on-gpu-cards/102290>\
**Category:** Machine Learning\
**Created:** [July 31, 2023, 6:18am UTC](https://discourse.julialang.org/t/when-calling-pytorch-using-pycall-or-pythoncall-i-run-out-of-memory-on-gpu-cards/102290 "2023-07-31T06:18:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [July 31, 2023, 6:18am UTC](https://discourse.julialang.org/t/when-calling-pytorch-using-pycall-or-pythoncall-i-run-out-of-memory-on-gpu-cards/102290/1 "2023-07-31T06:18:57Z")

</div>

Hi All,

I am inferfacing to PyTorch using PythonCall (or PyCall) with the data moved between Julia and Python by `DLPack.jl.` I use PyTorch to compute gradients and the rest (e.g. optimization) is handled by Julia, since I am way more familiar with it. After few iterations, PyTorch crashes as he runs out of memory on GPU. I move data between Python and Julia allocated on cpu, not gpu, therefore Julia should not be responsible for memory management in GPU. Has anyone encountered similar problems?  
I can prepare an MWE, but that would take me some time and I wanted to ask first.

Thanks a lot in advance.  
Tomas

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [July 31, 2023, 8:23am UTC](https://discourse.julialang.org/t/when-calling-pytorch-using-pycall-or-pythoncall-i-run-out-of-memory-on-gpu-cards/102290/2 "2023-07-31T08:23:48Z")

</div>

I need to manually call `GC.gc()` to free the memory. I guess the problem is that Julia is holding objects which prevents `PyTorch` to free the memory.
