Calling gc() in global scope from a function

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

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

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

1 Like

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.

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.

1 Like

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

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.

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