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><code>gc.get_referrers</code> takes one argument: the object whose referers it should find.</p>

<p>I cannot think of any circumstance in which <code>gc.get_referrers</code> would return no results, because in order to send an object to <code>gc.get_referrers</code>, there has to be a reference to the object.</p>

<p>In other words, if there was no reference to the object, it would not be possible to send it to <code>gc.get_referrers</code>.</p>

<p>At the very least, there will be a reference from the <code>globals()</code> or from the current <a href="https://docs.python.org/2.0/ref/execframes.html" rel="nofollow">execution frame</a> (which contains the local variables):</p>

<blockquote>
  <p>A code block is executed in an execution frame. An execution frame contains some administrative information (used for debugging), determines where and how execution continues after the code block's execution has completed, and (perhaps most importantly) defines two namespaces, the local and the global namespace, that affect execution of the code block.</p>
</blockquote>

<p>See an extended version of the example from the question:</p>

<pre><code>class Foo(object):
    pass

def f():
    bar = [Foo() for i in range(0, 10)]

    for x in range(0, len(bar)):
        # at this point there is one reference to bar[x]: it is bar
        print len(gc.get_referrers(bar[x]))  # prints 1

        baz = bar[x]

        # at this point there are two references to baz:
        #  - bar refernces it, because it is in the list
        #  - this "execution frame" references it, because it is in variable "baz"
        print len(gc.get_referrers(bar[x]))  # prints 2

        del bar[x]

        # at this point, only the execution frame (variable baz) references the object
        print len(gc.get_referrers(baz))  # prints 1

        print gc.get_referrers(baz) # prints a frame object 

        del baz

        # now there are no more references to it, but there is no way to call get_referrers

f()
</code></pre>

<h2>How to test it properly?</h2>

<p>There is a better trick to detect whether there are referers or not: <code>weakref</code>.</p>

<p><code>weakref</code> module provides a way to create <em>weak</em> references to an object which <em>do not count</em>. What it means is that even if there is a weak reference to an object, it will still be deleted when there are no other references to it. It also does not count in the <code>gc.get_referrers</code>.</p>

<p>So:</p>

<pre><code>&gt;&gt;&gt; x = Foo()
&gt;&gt;&gt; weak_x = weakref.ref(x)
&gt;&gt;&gt;
&gt;&gt;&gt; gc.get_referrers(x) == [globals()]  # only one reference from global variables
True

&gt;&gt;&gt; x
&lt;__main__.Foo object at 0x000000000272D2E8&gt;

&gt;&gt;&gt; weak_x
&lt;weakref at 0000000002726D18; to 'Foo' at 000000000272D2E8&gt;

&gt;&gt;&gt; del x
&gt;&gt;&gt; weak_x
&lt;weakref at 0000000002726D18; dead&gt;
</code></pre>

<p>The weak reference says that the object is dead, so it was indeed deleted.</p>

python object garbage-collection