title: Freeing up all used memory in python
body: <p><strong>Setup:</strong></p>
  
  <p>I am running a python code where:</p>
  
  <ol>
  <li>I open a file.</li>
  <li>For every line in file, I create an object</li>
  <li>Do some operations with the object</li>
  </ol>
  
  <p>Note that once I am done with the operations part, I no longer need the object. Every new line is independent. </p>
  
  <p><strong>Relevant Code as per request:</strong></p>
  
  <p>I have commented all the parts of my code, leaving below the following code:</p>
  
  <pre><code>import gc
  for l in range(num_lines):
      inp = f.readline()[:-1]
      collector = [int(i) for i in inp]
      M = BooleanFunction(collector)
      deg = M.algebraic_degree()
      del M
      gc.collect()
  </code></pre>
  
  <p><strong>The problem</strong>:</p>
  
  <p>The object once created, is consuming some amount of memory. After performing the operations, I am not able to free it. So while looping over the file, my memory keeps getting accumulated with new objects, and by around 793 lines into the file, my 16 GB of RAM is completely depleted.</p>
  
  <p><strong>What I have tried:</strong></p>
  
  <p>Using the garbage collector:</p>
  
  <p><code>import gc
   del Object
   gc.collect()
  </code></p>
  
  <p>However, the garbage collector will not free up the RAM (or) python is not giving up the memory to the system. Creating child-processes is an idea, but not what I am up for.</p>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>Is there any way I can free up all the memory currently occupied by the program to the OS? That means removing all variables (loop vars, global vars, etc). Something similar to what happens when you press CTRL+C to terminate the program, it returns all the memory to the OS.</li>
  <li>A way to specifically de-allocate an object (If I am not doing it right).</li>
  </ol>
  
  <p>Previous questions do not answer what if gc.collect() fails to do so and how do I completely give up the memory allocated.</p>