2009-04-17 54 views
0

link textsys.getrefcount延續

我得到的引用計數

所以,當我做了「德爾astrd」,引用計數下降到零astrd得到由GC收集的概念?

這是我昨天我提出的問題後開發的示例代碼codes.These: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) 

我在three.py中所做的是正確的還是不正確? 有沒有不必要的步驟?如果是,爲什麼?

請幫我解決這個問題。

回答

6

我相信,內存會自動釋放引用計數達到零的時刻。 GC不參與。

python GC是可選的,只有在有引用週期的不可達對象時纔會使用。事實上,如果您確定您的程序沒有創建參考週期,您可以致電gc.disable()

至於原來的問題:

  • 當你做del astrd,從本地名稱空間中刪除astrd結合到一個對象的引用(不管astrd引用)。
  • 如果這意味着refcount爲零,則該對象使用的內存將被釋放。
  • 因此del不刪除對象,它解除綁定引用。刪除對象是一個副作用,如果解除綁定引用導致refcount達到零,則會發生副作用。

請注意,以上僅適用於CPython。 Jython和IronPython使用JVM/CLR GC機制,並且根本不使用refcounting。

方便的gc.get_objects返回由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. 
+0

m編輯我的問題,通過附加一個例如代碼 可以告訴我wts hppng我是否正確? – user46646 2009-04-17 10:56:20

0

可不可以給一些背景,以你在做什麼? 很少有任何理由明確地使用del變量,而不是清理不想公開的事物的命名空間。我不知道你爲什麼打電話del file_name或運行gc.collect()。 (del sys.modules[filename]是好的 - 這是一個不同的使用德爾)

對於確切的時間,他們得到最終確定無關緊要(例如字符串像file_name),你也可以讓變量退出範圍 - 當你的功能完成後,它會被收集起來,到那時它不會造成任何傷害。手動調用del這樣的變量只會混淆你的代碼。

對於需要立即完成的對象(例如打開的文件或保留的鎖),您不應該依賴垃圾回收器 - 它不保證立即收集這些對象。它恰好在標準的C python實現中這樣做,但不在Jython或IronPython中實現,因此無法保證。相反,您應該通過調用close或使用新的with構造來明確清理這些對象。

唯一的其他原因可能是您分配的內存量非常大,並且希望在引用它的變量自然超出範圍之前用信號完成。

然而,你的例子似乎不符合這些情況,所以我不確定你爲什麼手動調用垃圾收集器。