Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sys.getrefcount continuation

Tags:

python

del

link text

I got the concept of reference count

So when i do a "del astrd" ,reference count drops to zero and astrd gets collected by gc ?

This is the sample codes.These codes I developed after my yesterday's question:link text

one.py:

def abc():

print "Hello"
print "123"
print '345'

two.py:

import one
#reload(one)

#def defg():

one.abc()

three.py:

import os,sys,gc
from time import sleep

import two
#reload(two)
#two.defg()

sleep(20)


directory = os.listdir('.')

for filename in directory:

        if filename[-3:] == 'pyc':

                print '- ' + filename

                print sys.getrefcount(filename)

                file_name = os.path.splitext (filename)[0]

                del file_name   # remove the local reference

                del sys.modules[os.path.splitext (filename)[0]] # removes import

                gc.collect()    # garbage collect

                #del sys.modules[filename]

                #del filename

                #os.remove(filename)

What i did in three.py is correct or not ? Is there any unnecessary step ?If yes,why ?

Please help me out of this.

like image 506
user46646 Avatar asked Dec 29 '25 11:12

user46646


1 Answers

I believe that memory is automatically freed the moment the refcount reaches zero. The GC is not involved.

The python GC is optional, and is only used when there are unreachable objects that has reference cycles. In fact, you can call gc.disable() if you are sure your program does not create reference cycles.

As for the original question:

  • When you do del astrd, you remove the binding of astrd from the local namespace a reference to an object (whatever astrd references).
  • If this means that the refcount is zero, the memory used by the object is freed.
  • So del does not delete objects, it unbinds references. The deletion of objects is a side effect that occurs if unbinding a reference causes the refcount to reach zero.

Note that the above is only true for CPython. Jython and IronPython uses the JVM/CLR GC mechanism, and does not use refcounting at all, I believe.

The handy gc.get_objects returns a list of all object instances tracked by the python interpreter. Example:

import gc

class test(object):
    pass

def number_of_test_instances():
    return len([obj for obj in gc.get_objects() if isinstance(obj, test)])

for i in range(100):
    t = test()

print "Created and abandoned 100 instances, there are now", \
    number_of_test_instances(), \
    "instances known to the python interpreter."

# note that in normal operation, the GC would
# detect the unreachable objects and start
# collecting them right away
gc.disable()

for i in range(100):
    t = test()
    t.t = t

print "Created and abandoned 100 instances with circular ref, there are now", \
    number_of_test_instances(), \
    "instances known to the python interpreter."

gc.collect()
print "After manually doing gc.collect(), there are now", \
    number_of_test_instances(), \
    "instances known to the python interpreter."

Running this program gives:

Created and abandoned 100 instances, there are now 1 instances known to the python interpreter.
Created and abandoned 100 instances with circular ref, there are now 100 instances known to the python interpreter.
After manually doing gc.collect(), there are now 1 instances known to the python interpreter.
like image 186
codeape Avatar answered Dec 31 '25 03:12

codeape



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!