# TensorFlow : explicit close session?

**URL:** <https://discourse.julialang.org/t/tensorflow-explicit-close-session/5952>\
**Category:** Machine Learning\
**Tags:** tensorflow\
**Created:** [September 18, 2017, 7:33pm UTC](https://discourse.julialang.org/t/tensorflow-explicit-close-session/5952 "2017-09-18T19:33:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_Beckingham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colin_beckingham/32/4878_2.png) [@Colin\_Beckingham](https://discourse.julialang.org/u/Colin_Beckingham)\
**Post date:** [September 18, 2017, 7:33pm UTC](https://discourse.julialang.org/t/tensorflow-explicit-close-session/5952/1 "2017-09-18T19:33:32Z")

</div>

The Python API calls for an explicit close of the session according to example code I have seen. The docs for Julia seem to indicate there is no close() on a session. Would this be a Python/Julia difference to add to the docs? Is there an implicit close, and can one close an existing session by opening another with the same name?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [September 19, 2017, 1:27am UTC](https://discourse.julialang.org/t/tensorflow-explicit-close-session/5952/2 "2017-09-19T01:27:46Z")

</div>

As of right now, all the stuff for closing a session (and most other closable things) is in the finalizer for the object in question.  
When they are garbage collected, they will be closed.

Mostly this means that if that no longer have any references, and the garbage collector is triggered, which normaly automatic but can be done by calling `gc()` they will be closed and wrapped up properly.  
Details on [finalizers when calling gc manually here](https://stackoverflow.com/questions/44469084/does-calling-gc-manually-result-in-all-finalizers-being-executed-immediat)

One should close the session by making all references to it go out of scope.  
This could be done by doing it inside a scoping block.  
(Eg all the tests are in let blocks. which are particularly for this)  
Or by assigning over the variable holding the session.

This has been [argued to be a bad thing](https://github.com/JuliaLang/julia/issues/11207), and may be [going away soon](https://github.com/JuliaLang/julia/issues/16307#issuecomment-329564078)

Adding a differences from python section to the docs is not something I put as high priority, but if someone made a PR I’ld review it.

---

<div class="post-metadata">

**Author:** ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)\
**Post date:** [September 19, 2017, 1:54am UTC](https://discourse.julialang.org/t/tensorflow-explicit-close-session/5952/3 "2017-09-19T01:54:50Z")

</div>

I’d be fine with defining `Base.close(::Session)`. In both Julia and Python, the Session resources are freed when GC runs on the session object so it’s not usually important to manually invoke, as oxinabox says.
