sys.getrefcount延续 [英] sys.getrefcount continuation
问题描述
我有了引用计数的概念
因此,当我执行 del astrd时,引用计数降至零,而gc收集了astrd?
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():
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)
我在three.py中所做的是正确的吗?
是否有任何不必要的步骤?如果是,为什么?
What i did in three.py is correct or not ? Is there any unnecessary step ?If yes,why ?
请帮助我。
推荐答案
我相信,当引用计数达到零时,内存会自动释放。
I believe that memory is automatically freed the moment the refcount reaches zero. The GC is not involved.
python GC是可选的,仅在存在具有引用循环的不可访问对象时使用。实际上,如果您确定程序未创建参考循环,则可以调用 gc.disable()
。
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.
对于原始问题:
- 当您执行
del astrd
时,您将其删除 - 如果这意味着refcount为零,则释放该对象使用的内存。
- li>
- 因此
del
不会删除对象,它会取消绑定引用。如果取消绑定引用导致引用计数达到零,则会删除对象。
- 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.
请注意,以上仅是对于CPython是true。我相信Jython和IronPython使用JVM / CLR GC机制,并且根本不使用引用计数。
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.
方便的 gc.get_objects
返回python解释器跟踪的所有对象实例的列表。示例:
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."
运行该程序可以得到:
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.
这篇关于sys.getrefcount延续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!