diff --git a/code/main.py b/code/main.py
index 1288280f56fe0c5d59f600496c90a9eba2acd20e..39fba55aee35ef0eb5af67ead6218ebcac2083f7 100644
--- a/code/main.py
+++ b/code/main.py
@@ -394,6 +394,7 @@ class GraphViewer:
     def display_graph(self, graph_file):
         if self.canvas:
             self.canvas.get_tk_widget().destroy()
+            plt.close(self.fig)  # Close the previous figure to release memory
 
         img = plt.imread(os.path.join("resulting_figures", graph_file))
         self.fig, self.ax = plt.subplots()