Python垃圾是否会在更高的范围内收集变量,而变量将永远不会再使用?

发布于 2025-02-09 13:03:21 字数 351 浏览 1 评论 0原文

例如,在代码中:

a = [1, 2, 3, 4] # huge list

y = sum(a)

print( do_stuff(y) )

列表a的内存是否会在程序结束之前被释放? do_stuff功能调用是否必须使用a不断占用内存的所有操作,即使a永远不会再使用?

而且,如果a没有收集垃圾,一旦我完成了它,是否是手动设置a = none的解决方案?

For example, in the code:

a = [1, 2, 3, 4] # huge list

y = sum(a)

print( do_stuff(y) )

Will the memory for the list a ever get freed up before the program ends? Will the do_stuff function call have to do all its stuff with a constantly taking up memory, even though a's never going to be used again?

And if a doesn't get garbage collected, is the solution to manually set a=None once I'm done using it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2025-02-16 13:03:21

想象do_stuff做到了这一点:

def do_stuff(y):
    return globals()[input()]

并且用户输入a,以便毕竟在此使用列表。 Python不知道这不会发生,它必须阅读用户的思想。

考虑这样的微不足道的情况:

def do_stuff(y):
    return y

现在很明显,a不再使用,所以Python 可以弄清楚吗?好吧... 打印不再是关键字/语句。 Python必须弄清楚您没有使用a的东西覆盖print。即使您没有,它也需要知道其自己的打印不使用a

您必须自己删除它。我将使用del a。 (除非您想要 a仍然存在并具有值none)。

Imagine do_stuff did this:

def do_stuff(y):
    return globals()[input()]

And the user enters a, so that the list is used there after all. Python can't know that that won't happen, it would have to read the user's mind.

Consider a trivial case like this:

def do_stuff(y):
    return y

Now it's clear that a doesn't get used anymore, so Python could figure that out right? Well... print isn't a keyword/statement anymore. Python would have to figure out that you didn't overwrite print with something that does use a. And even if you didn't, it would need to know that its own print doesn't use a.

You'll have to delete it yourself. I'd use del a. (Unless you want a to still exist and have the value None).

-残月青衣踏尘吟 2025-02-16 13:03:21

a除非超出范围(即。

Python's垃圾收集器使用一个名为参考计数)的系统。简而言之,所有变量都有一个参考计数器,当创建对变量的引用时会增加,并且当变量设置为none时,则降低。

例子:

a = [999999] # 1 reference, the value [999999] is stored in memory
b = a # 2 references

def foo(x):
    print(x)

foo(a) # 3 references during the function call
# back to 2 references
a = None # 1 reference
b = None # 0 references, the value [999999] is deleted from memory

a will never be freed unless it goes out of scope (ie. it was in a function to begin with), or you manually set it to None.

Python's garbage collector uses a system called reference counting. In short, all variables have a reference counter that is incremented when a reference to the variable is created, and decremented when an variable is set to None or when it goes out of scope.

Example:

a = [999999] # 1 reference, the value [999999] is stored in memory
b = a # 2 references

def foo(x):
    print(x)

foo(a) # 3 references during the function call
# back to 2 references
a = None # 1 reference
b = None # 0 references, the value [999999] is deleted from memory
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文