python不会释放元组的内存,是吗?
根据我读到的这篇文章,看起来不可变类型可能会变得不朽。
http:// /effbot.org/pyfaq/why-doesnt-python-release-the-memory-when-i-delete-a-large-object.htm
我目前正在解决我的 Web 服务的内存使用问题哪个在谷歌应用程序引擎上运行。 “使用太多元组”是否可能是导致此问题的潜在原因?
感谢您的回复: 我正在谷歌应用程序引擎后端实例上运行我的代码,它有一个内存使用上限(128mb)。它说我使用的内存超出了允许的范围并停止了我的实例。正如评论所提到的,这可能是“内存使用量仍然很大”而不是“内存泄漏”。
According to this article I read, it looks like immutable type may become immortal.
http://effbot.org/pyfaq/why-doesnt-python-release-the-memory-when-i-delete-a-large-object.htm
I am currently working on solve a memory usage problem for my web service which running on google app engine. Does "use too much tuple" could be a potential reason to cause this problem?
Thanks for the reply:
I am running my code on google app engine backend instance and it has a memory usage upper bounds (128mb). It said I use more memory than allowed and stopped my instance. As comments mentioned, It could be "memory usage remain large" instead of "memory leaks".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
本文没有指定不可变类型 - 它指定:
我不知道你能从GAE进程中获得什么信息,但你可以在你自己的系统上尝试这个实验。
首先,启动 python 解释器并找到进程。然后运行这个命令:
然后,看一下内存使用情况。您刚刚创建了一个包含 500 万个元组的列表。现在输入:
在我的系统(Python 3.2、Win 7)上,我的内存使用量猛增约 20k,然后在我删除变量后又下降了相同的量。如果您可以获得有关进程的信息(CPU、内存使用情况),您可以尝试这样做 - 也许连续几次,这应该会给您带来一些更高的内存使用情况。
This article doesn't specify immutable types - it specifies:
I don't know what information you can get on a GAE process, but you can try this experiment on your own system.
First, start up a python interpreter and find the process. Then run this command:
Then, take a look at the memory usage. You've just created a list of 5 million tuples. Now type:
On my system (Python 3.2, Win 7), my memory usage shot up about 20k, and then dropped by the same amount once I
del
ed the variable. If you can get information about your processes (CPU, memory usage), you can try doing that - maybe several times in a row which should give you a few spikes of higher memory usage.我在您链接的文章中没有看到元组是 Python 维护自己的空闲列表的类型之一。他们很可能是这样,但根据这篇文章,具体的罪魁祸首是
int
s、float
、dict
和list
。尽管那篇文章是 2005 年的文章,从那时起事情可能已经发生了变化......在 Python 2.6 或更高版本中,除了
int
和float
之外的所有内容的空闲列表都可以被清除with withgc.collect(2)
,我想这对你在 GAE 上没有帮助,但我想我应该提一下。I don't see in the article you linked that tuples are one of the types for which Python maintains its own free lists. They could well be, but according to this article the specific culprits are
int
s,float
s,dict
s, andlist
s. Although that article is from 2005 and things could have changed since then...In Python 2.6 or later, the free lists for everything but
int
s andfloat
s can be cleared with withgc.collect(2)
, which I guess doesn't help you on GAE, but I thought I'd mention it.不可变类型不会变得不朽。它占用的内存仍然归Python所有,但可供其他对象使用。
循环依赖可能会导致内存泄漏:
此时,您有一个
Child
,其链接到其Parent
,还有一个Parent
,其链接到其Child
,Python 可能无法释放它们。The immutable type does not become immortal. The memory it occupied is still owned by Python, but it is available for use for other objects.
Circular dependencies is a possibility for your memory leak:
At this point you have a
Child
with a link to itsParent
, and aParent
with a link to itsChild
, and Python may have trouble releasing them.