Python sys.getrefcount继续

Python sys.getrefcount继续,python,del,Python,Del,我得到了参考计数的概念 所以当我做一个del astrd时,引用计数下降到零,astrd被gc收集 这是示例代码。这些代码是我在昨天的问题之后开发的: 1.py: def abc: print "Hello" print "123" print '345' 2.py: import one #reload(one) #def defg(): one.abc() 3.py: import os,sys,gc from time import sleep import two #reloa

我得到了参考计数的概念

所以当我做一个del astrd时,引用计数下降到零,astrd被gc收集

这是示例代码。这些代码是我在昨天的问题之后开发的:

1.py:

def abc:

print "Hello"
print "123"
print '345'
2.py:

import one
#reload(one)

#def defg():

one.abc()
3.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中所做的是正确的还是错误的? 是否有任何不必要的步骤?如果有,为什么

请帮我解决这个问题。

它有机会在下一次GC收集运行时被收集

请参阅:

它有机会在下一次GC收集运行时被收集


请参阅:

我相信,一旦refcount达到零,内存就会自动释放。GC不参与其中

pythongc是可选的,仅当存在具有引用周期的不可访问对象时才使用。事实上,如果您确定您的程序没有创建引用循环,那么可以调用gc.disable

至于原来的问题:

执行del astrd时,可以从本地名称空间中删除astrd的绑定,即对astrd引用的对象的引用。 如果这意味着refcount为零,则对象使用的内存将被释放。 因此,del不会删除对象,而是解除引用的绑定。如果解除绑定引用会导致refcount达到零,则删除对象会产生副作用。 请注意,以上仅适用于CPython。Jython和IronPython使用JVM/CLR GC机制,我认为根本不使用refcounting

方便的gc.get_对象返回python解释器跟踪的所有对象实例的列表。例如:

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.
我相信当refcount达到零时,内存会自动释放。GC不参与其中

pythongc是可选的,仅当存在具有引用周期的不可访问对象时才使用。事实上,如果您确定您的程序没有创建引用循环,那么可以调用gc.disable

至于原来的问题:

执行del astrd时,可以从本地名称空间中删除astrd的绑定,即对astrd引用的对象的引用。 如果这意味着refcount为零,则对象使用的内存将被释放。 因此,del不会删除对象,而是解除引用的绑定。如果解除绑定引用会导致refcount达到零,则删除对象会产生副作用。 请注意,以上仅适用于CPython。Jython和IronPython使用JVM/CLR GC机制,我认为根本不使用refcounting

方便的gc.get_对象返回python解释器跟踪的所有对象实例的列表。例如:

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.
你能介绍一下你正在做什么吗? 除了清理不想公开的名称空间之外,很少有任何理由显式地在变量上使用del。我不知道为什么要调用del file_name或运行gc.collect。del sys.modules[filename]很好-这是del的不同用法

对于对象,当它们最终确定的确切时间无关紧要,例如file_name之类的字符串,您也可以让变量退出作用域-当您的函数完成时,它将被收集,并且在此之前不会造成任何伤害。为这些变量手动调用del只会把代码弄得乱七八糟

对于需要立即完成的对象,例如打开的文件或持有的锁,无论如何都不应该依赖垃圾收集器-不能保证立即收集此类对象。它恰好在标准的C python实现中实现,但在Jython或IronPython中没有,因此不能保证。相反,您应该通过调用close或使用新的with构造来显式清理这些对象

唯一的另一个原因可能是,您分配了大量内存,并且希望在引用它的变量自然超出范围之前发出信号,表明您已经完成了它


但是,您的示例似乎不适合这两种情况,因此我不确定您为什么要手动调用垃圾收集器。

您能提供一些您正在做的背景信息吗? 除了清理不想公开的名称空间之外,很少有任何理由显式地在变量上使用del。我不知道为什么要调用del file_name或运行gc.collect。del sys.modules[filename]很好-这是del的不同用法

对于对象,当它们最终确定的确切时间无关紧要,例如file_name之类的字符串,您也可以让变量退出作用域-当您的函数完成时,它将被收集,并且在此之前不会造成任何伤害。为这些变量手动调用del只会把代码弄得乱七八糟

对于需要立即完成的对象,例如打开的文件或持有的锁,无论如何都不应该依赖垃圾收集器-不能保证立即收集此类对象。信息技术 在标准的C python实现中正好如此,但在Jython或IronPython中却不是这样,因此不能保证。相反,您应该通过调用close或使用新的with构造来显式清理这些对象

唯一的另一个原因可能是,您分配了大量内存,并且希望在引用它的变量自然超出范围之前发出信号,表明您已经完成了它


你的例子似乎不适合这两种情况,所以我不知道你为什么要手动调用垃圾收集器。

m通过附加eg代码来编辑我的问题。你能告诉我wts hppng n我的回答正确吗?根据我昨天的问题,我试图删除我附加的导入第二个链接。你是否试图从*.py文件中执行可能更改的代码?如果是这种情况,请看一下execfile。我通过附加eg代码来编辑我的问题。你能告诉我wts hppng n我是否正确吗?根据我昨天的问题,我试图删除我附加的第二个链接的导入。你是否试图从*.py文件中执行可能更改的代码?如果是这种情况,请查看execfile。