python不会释放元组的内存,是吗?

发布于 2024-12-15 11:06:59 字数 457 浏览 2 评论 0原文

根据我读到的这篇文章,看起来不可变类型可能会变得不朽。

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 技术交流群。

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

发布评论

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

评论(3

寒冷纷飞旳雪 2024-12-22 11:06:59

本文没有指定不可变类型 - 它指定:

内存使用过多的另一个可能原因是 Python 对某些对象类型(包括整数和浮点数)使用所谓的“空闲列表”。

我不知道你能从GAE进程中获得什么信息,但你可以在你自己的系统上尝试这个实验。

首先,启动 python 解释器并找到进程。然后运行这个命令:

>>> many_tuples = [() for x in range(5000000)] #replace with xrange for 2.x

然后,看一下内存使用情况。您刚刚创建了一个包含 500 万个元组的列表。现在输入:

>>> del many_tuples

在我的系统(Python 3.2、Win 7)上,我的内存使用量猛增约 20k,然后在我删除变量后又下降了相同的量。如果您可以获得有关进程的信息(CPU、内存使用情况),您可以尝试这样做 - 也许连续几次,这应该会给您带来一些更高的内存使用情况。

This article doesn't specify immutable types - it specifies:

Another possible cause for excessive memory usage is that Python uses so-called “free lists” for certain object types, including integers and floats.

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:

>>> many_tuples = [() for x in range(5000000)] #replace with xrange for 2.x

Then, take a look at the memory usage. You've just created a list of 5 million tuples. Now type:

>>> del many_tuples

On my system (Python 3.2, Win 7), my memory usage shot up about 20k, and then dropped by the same amount once I deled 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.

惟欲睡 2024-12-22 11:06:59

我在您链接的文章中没有看到元组是 Python 维护自己的空闲列表的类型之一。他们很可能是这样,但根据这篇文章,具体的罪魁祸首是int s、floatdictlist。尽管那篇文章是 2005 年的文章,从那时起事情可能已经发生了变化......

在 Python 2.6 或更高版本中,除了 intfloat 之外的所有内容的空闲列表都可以被清除with with gc.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 ints, floats, dicts, and lists. 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 ints and floats can be cleared with with gc.collect(2), which I guess doesn't help you on GAE, but I thought I'd mention it.

星星的軌跡 2024-12-22 11:06:59

不可变类型不会变得不朽。它占用的内存仍然归Python所有,但可供其他对象使用。

循环依赖可能会导致内存泄漏:

class Parent(object):
    def __init__(self):
        self.offspring = Child(self)
    def __del__(self):
        # doesn't matter what goes here, gc will not be able to auto collect
        # freed Parents and Childs

class Child(object):
    def __init__(self, parent):
        self.parent = parent

John_Doe = Parent()

此时,您有一个 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:

class Parent(object):
    def __init__(self):
        self.offspring = Child(self)
    def __del__(self):
        # doesn't matter what goes here, gc will not be able to auto collect
        # freed Parents and Childs

class Child(object):
    def __init__(self, parent):
        self.parent = parent

John_Doe = Parent()

At this point you have a Child with a link to its Parent, and a Parent with a link to its Child, and Python may have trouble releasing them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文