title: Why doesn't a tensorflow model's memory clear from RAM?
body: <p>I want to create a population of different models and update the population every generation. This means I have to create a new list of models every generation. Every time the list of models is cleared, the memory appears to stay and will accumulate every generation until it consumes all the memory.</p>
  <p>This is my simplified code:</p>
  <pre><code>import tensorflow as tf
  from tensorflow.keras.models import Sequential
  from tensorflow.keras.layers import Dense, LSTM
  
  class population:
      def __init__(self):
          self.pop = []
  
      def createModel(self, inputShape):
          model = Sequential([
                  LSTM(64, input_shape=(inputShape), return_sequences=True),
                  LSTM(64, input_shape=(inputShape)),
                  Dense(32, activation=&quot;relu&quot;),
                  Dense(2, activation=&quot;softmax&quot;),
          ])
          return model
  
      def createList(self, numModels):
          model_list = []
          for _ in range(numModels):
              model = self.createModel((5,5))
              model_list.append(model)
          
          return model_list
  
      def updateList(self, iterations):
          for i in range(iterations):
              print(f&quot;Generation: {i+1}&quot;)
              self.pop.clear()
              self.pop = self.createList(50)
  
  pop = population()
  pop.updateList(10)
  </code></pre>