为什么这不会像预期的那样产生垃圾?

Why doesn't this create garbage like expected?

我试图想出一个简洁的示例,用于对没有变量名引用的对象进行垃圾回收,但是这段代码似乎不起作用。我想了解为什么要更好地了解 Python 的内部工作原理。好像暴露了我的误会。

some_name = [['some_nested_lst_object_with_an_str_object']]
id(some_name)
'''
you don't normally need to do this.
This is done for the reference example.
Accessing garbage collector:
'''
import gc
print(gc.collect())
'''
If I assign something new to ''*some_name*'',
the reference to the previous object will be lost:
'''
some_name
print(gc.collect())
some_name = [[['something_new']]]
some_name
print(gc.collect())

Python 通常使用引用计数来释放对象。 只有在循环引用的情况下,才需要进行垃圾回收:

some_name = [123]
print(gc.collect())
some_name = [] # previous some_name-object is freed directly
some_name.append(some_name) # cyclic reference
print(gc.collect()) # 0
some_name = None
print(gc.collect()) # 1