Python object is being referenced by an object I cannot find
<p>I am trying to remove an object from memory in python and I am coming across an object that it is not being removed.  From my understanding if there is no references to the object the garbage collector will de-allocate the memory when it is run.  However after I have removed all of the references if I run </p>

<pre><code>bar = Foo()
print gc.get_referrers(bar)
del bar
baz = gc.collect()
print baz
</code></pre>

<p>I get a reply of </p>

<blockquote>
  <p>[&lt; frame object at 0x7f1eba291e50>]</p>
  
  <p>0</p>
</blockquote>

<p>So how come does it not delete the object?</p>

<p>I get the same reply for all of the instances of objects if i do </p>

<pre><code>bar = [foo() for i in range(0, 10)]
for x in range(0,len(bar))    
    baz = bar[x]
    del bar[x]
    print gc.get_referrers(baz)
</code></pre>

<p>How do I completely remove all referrers from an object/any idea what the frame object that is on all is? </p>

<p>I thought it would be the object frame(?) that contains a list of all objects in the program but I have not been able to confirm that/find a way to rid objects from being referenced by said mystical(to me) object fram.</p>

<p>Any help would be greatly appreciated</p>

<p>Edit: 
 Okay I rewrote the code to the simple form pulling out everything except the basics</p>

<pre><code>import random, gc
class Object():
  def __init__(self):
    self.n=None
    self.p=None
    self.isAlive=True
  def setNext(self,object):
    self.n=object
  def setPrev(self, object):
    self.p=object
  def getNext(self):
    return self.n
  def getPrev(self):
    return self.p
  def simulate(self):
      if random.random() &gt; .90:
        self.isAlive=False
  def remove(self):
    if self.p is not None and self.n is not None:
      self.n.setPrev(self.p)
      self.p.setNext(self.n)
    elif self.p is not None:
      self.p.setNext(None)
    elif self.n is not None:
      self.n.setPrev(None)
    del self

class Grid():
  def __init__(self):
    self.cells=[[Cell() for i in range(0,500)] for j in range(0,500)]
    for x in range(0,100):
      for y in range(0,100):
        for z in range(0,100):
            self.cells[x][y].addObject(Object())
  def simulate(self):
      for x in range(0,500):
          for y in range(0,500):
              self.cells[x][y].simulate()
      num=gc.collect()
      print "  " + str(num) +" deleted today."
class Cell():
  def __init__(self):
    self.objects = None
    self.objectsLast = None
  def addObject(self, object):
    if self.objects is None:
        self.objects = object
    else:
        self.objectsLast.setNext(object)
        object.setPrev(self.objectsLast)
    self.objectsLast = object
  def simulate(self):
    current = self.objects
    while current is not None:
      if current.isAlive:
        current.simulate()
        current = current.getNext()
      else:
        delete = current
        current = current.getNext()
        if delete.getPrev() is None:
          self.objects = current
        elif delete.getNext() is None:
          self.objectsLast = delete.getPrev()
        delete.remove()
def main():
    print "Building Map..."
    x = Grid()
    for y in range (1,101):
        print "Simulating day " + str(y) +"..."
        x.simulate()
if __name__ == "__main__":
    main()
</code></pre>

<p>Okay thanks to cjhanks and user2357112 I came up with this answer</p>

<p>The problem being that if you run the program the gc does not collect anything after each day even though there were things deleted</p>

<p>To test if it is deleted I instead run</p>

<pre><code>print len(gc.get_objects()) 
</code></pre>

<p>each time I go through a "day" doing this shows how many objects python is tracking. 
Now with that information and thanks to a comment I tired changing Grid to</p>

<pre><code>class Grid():
  def __init__(self):
    self.cells=[[Cell() for i in range(0,500)] for j in range(0,500)]
    self.add(100)
  def add(self, num):
      for x in range(0, 100):
          for y in range(0, 100):
              for z in range(0, num):
                  self.cells[x][y].addObject(Object())
  def simulate(self):
      for x in range(0,500):
          for y in range(0,500):
              self.cells[x][y].simulate()
      num=gc.collect()
      print "  " + str(num) +" deleted today."
      print len(gc.get_objects())
</code></pre>

<p>and then calling Grid.add(50) halfway through the process.  My memory allocation for the program did not increase (watching top in Bash) So my learning points:</p>

<ul>
<li>GC was running without my knowledge</li>
<li>Memory is allocated and never returned to system until the the program is done</li>
<li>Python will reuse the memory  </li>
</ul>

python object garbage-collection