Calling gc() in global scope from a function


#1

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


#2

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


#3

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


#4

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.


#5

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.


#6

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


#7

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.


#8

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