나는 메모리가 refcount는 제로에 도달하는 순간을 해제 자동으로되어 있다고 생각합니다. GC는 관련이 없습니다.
파이썬 GC는 선택 사항이며 참조주기가있는 도달 할 수없는 객체가있는 경우에만 사용됩니다. 실제로 프로그램이 참조주기를 생성하지 않는다고 확신하는 경우 gc.disable()
으로 전화 할 수 있습니다. 원래의 질문에 관해서는
:
- 당신이
del astrd
을, 당신은 로컬 네임 스페이스에서 개체에 대한 참조 (어떤 astrd 참조를)을 astrd의 바인딩을 제거합니다.
- refcount가 0 인 경우 개체에서 사용하는 메모리가 해제됩니다.
- 따라서
del
은 개체를 삭제하지 않으며 참조를 바인딩 해제합니다. 객체를 삭제하면 참조를 바인딩 해제하여 refcount가 0이되는 경우 발생하는 부작용입니다.
위의 내용은 CPython에만 해당됩니다. Jython과 IronPython은 JVM/CLR GC 메커니즘을 사용하며 refcounting을 전혀 사용하지 않습니다.
편리한 gc.get_objects
은 파이썬 인터프리터에서 추적 한 모든 객체 인스턴스의 목록을 반환합니다. 예 :
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.
m 예를 들어 코드를 첨부하여 내 질문을 편집하십시오. 을 말해 주시겠습니까? – user46646
코드에서 수행하려는 작업은 무엇입니까? – codeape
제 어제의 질문에 따라 수입품을 지우려고했습니다 두 번째 링크를 붙였습니다 – user46646