# Calling gc() in global scope from a function

**URL:** <https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483>\
**Category:** General Usage\
**Created:** [June 27, 2017, 5:39am UTC](https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483 "2017-06-27T05:39:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 27, 2017, 5:39am UTC](https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483/1 "2017-06-27T05:39:41Z")

</div>

I am trying to call gc() to clean the global scope but from inside a function. Is such a thing possible?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 27, 2017, 5:44am UTC](https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483/2 "2017-06-27T05:44:18Z")

</div>

Ended up wrapping the function in another function and calling gc() after calling the first function!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 27, 2017, 6:37am UTC](https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483/3 "2017-06-27T06:37:44Z")

</div>

FWIW, there’s almost no valid use case of explicitly calling `gc()`.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 27, 2017, 10:03pm UTC](https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483/4 "2017-06-27T22:03:22Z")

</div>

When using VTK from PyCall, after rendering a render window, the window stays open even after the function using it has ended. Only when gc() is called that the windows closes. I could make the rendering offscreen but just to wrap up loose ends I am explicitly running gc() again.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 27, 2017, 10:43pm UTC](https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483/5 "2017-06-27T22:43:37Z")

</div>

That is indeed a incorrect usage of `gc()`. Resources with significant side-effect should not be managed by the finalizer only. The correct way is to explicitly call the function that does the cleanup when you are done.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 27, 2017, 11:06pm UTC](https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483/6 "2017-06-27T23:06:08Z")

</div>

Seems like the right function I was looking for is Finalize.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 28, 2017, 12:57am UTC](https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483/7 "2017-06-28T00:57:27Z")

</div>

`finalize` will indeed only run the finalizer. It is however slower in general so if you know what the finalizer is it is better to call just that. If whatever you are using does not expose that function, it is generally a good idea to fix it.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 28, 2017, 12:58am UTC](https://discourse.julialang.org/t/calling-gc-in-global-scope-from-a-function/4483/8 "2017-06-28T00:58:40Z")

</div>

yes I was talking about the renderwindow[:Finalize] function in VTK 🙂
