# Using GPU via PyCall causes non-reusable memory allocation

**URL:** <https://discourse.julialang.org/t/using-gpu-via-pycall-causes-non-reusable-memory-allocation/55140>\
**Category:** GPU\
**Tags:** question, pycall, gpu, pytorch, garbage-collection\
**Created:** [February 12, 2021, 11:47am UTC](https://discourse.julialang.org/t/using-gpu-via-pycall-causes-non-reusable-memory-allocation/55140 "2021-02-12T11:47:34Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 16, 2021, 4:07pm UTC](https://discourse.julialang.org/t/using-gpu-via-pycall-causes-non-reusable-memory-allocation/55140/4 "2021-02-16T16:07:04Z")

</div>

> [@JosePereiraUA](#):
>
> calling `GC.gc(false)` seems to help […] Is there anything I can do to re-use the memory allocated by Python?

It sounds like Julia’s garbage collection just isn’t running frequently enough for you, probably because Julia doesn’t know that memory is running low on the CUDA side?

You can explicitly tell Python you are done with an object `o` from PyCall by calling `pydecref(o)`. (This is safe if you are done with the object: it gets mutated to a NULL object to prevent it from being decref’ed again. Perhaps the function should have been called `pydecref!`…) Equivalently, you can just call `finalize(o)`.

See also [stop using finalizers for resource management? · Issue #11207 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/11207) and [`with` for deterministic destruction · Issue #7721 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/7721) for discussion of this general issue for resource management in Julia.

---

_[View the full topic](https://discourse.julialang.org/t/using-gpu-via-pycall-causes-non-reusable-memory-allocation/55140)._
