TensorFlow
session management
computational graph
machine learning
deep learning

closing session in tensorflow doesn't reset graph

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In TensorFlow 1.x, a Session and a Graph are related but separate objects. Closing a session releases the runtime resources associated with execution, but it does not delete the graph definition itself, which is why repeated experiments in the same process can keep accumulating graph nodes unless you reset or isolate the graph explicitly.

Session And Graph Are Different Layers

A TensorFlow 1.x graph describes operations and tensors. A session executes that graph.

That means this code creates graph nodes first and runs them later:

python
1import tensorflow as tf
2
3g = tf.Graph()
4with g.as_default():
5    x = tf.constant(2)
6    y = tf.constant(3)
7    z = x + y
8
9with tf.compat.v1.Session(graph=g) as sess:
10    print(sess.run(z))

When the with block ends, the session closes, but g still exists as a Python object containing the graph structure.

Why Closing The Session Does Not Reset The Default Graph

In older TensorFlow code, many people used the global default graph implicitly. If you keep adding operations to that default graph and only close the session, the next run may attach even more operations to the same graph.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5a = tf.constant(1)
6b = tf.constant(2)
7
8with tf.compat.v1.Session() as sess:
9    print(sess.run(a + b))
10
11# The graph objects still exist here.
12c = tf.constant(3)

That behavior surprises people because closing the session feels like it should "clean everything up," but the graph lifecycle is separate.

How To Reset The Default Graph In TF1-Style Code

If you are using TensorFlow 1.x or TF1 compatibility mode, the classic reset call is:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5tf.compat.v1.reset_default_graph()

This clears the current default graph state for subsequent graph construction in the same thread.

Use it carefully. It is usually best at script boundaries, test boundaries, or notebook cells where you know you want a fresh graph.

Prefer Explicit Graph Objects

A cleaner pattern is to avoid relying on the global default graph at all.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5for _ in range(2):
6    graph = tf.Graph()
7    with graph.as_default():
8        x = tf.constant(10)
9        y = tf.constant(20)
10        total = x + y
11
12    with tf.compat.v1.Session(graph=graph) as sess:
13        print(sess.run(total))

This keeps each experiment isolated and avoids the accidental growth of one shared default graph.

TensorFlow 2.x Changes The Mental Model

TensorFlow 2.x uses eager execution by default, so most ordinary Keras or tensor code no longer revolves around manually managed sessions and global graphs. That is why many newer users do not run into this issue unless they are maintaining legacy code or using tf.compat.v1 APIs.

If you are writing new code, you should rarely need to think in terms of resetting a default graph.

Notebook Cells And Repeated Construction

This issue shows up most often in notebooks. A cell that defines the same ops repeatedly can accumulate duplicate nodes in the same default graph if the kernel stays alive.

That is why old TF1 notebook workflows often included tf.compat.v1.reset_default_graph() at the top of a cell.

Common Pitfalls

The most common mistake is assuming that a session owns the graph and therefore destroys it on close. Another is mixing implicit default-graph code with repeated notebook execution and then wondering why variable names or node counts keep growing. Developers also sometimes use reset_default_graph() too casually inside larger applications where graph ownership should be explicit instead. In TensorFlow 2.x code, the better fix is usually to avoid TF1 session-style patterns altogether.

Summary

  • Closing a TensorFlow 1.x session releases execution resources, not the graph definition.
  • The default graph persists until you reset it or replace it.
  • 'tf.compat.v1.reset_default_graph() is the classic way to clear TF1-style default graph state.'
  • Explicit tf.Graph() objects are often safer than relying on one global default graph.
  • In modern TensorFlow 2.x code, eager execution makes this issue much less central.

Course illustration
Course illustration

All Rights Reserved.