Python内存释放 [英] Python memory deallocate

查看:64
本文介绍了Python内存释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的应用程序存在很大的内存问题。


首先,一个例子:


如果我写:


a =范围(500 * 1024)


我看到python进程分配大约80Mb的内存。

我可以做什么来DEALLOCATE这个记忆,或者很好的一部分?


我真正的问题是我的程序可以处理更多照片,我<使用PIL包打开


当我启动它时,我的程序会分配大约200Mb的内存。


如果我想要中止实际工作并且重新启动而不重启python

进程,内存使用量将增加大约380-400 Mb。


我想找到一些东西来分配未使用的内存。


我已经尝试过使用gc python模块,但不能正常工作(它取消分配

约20-30 Mb)

我已经尝试过使用Destroy,del命令,但记忆没有显示出来。


Thx


我非常绝望

解决方案




''del a''应该删除''a'',作为对

''范围'创建的元组的引用'功能。

如果这也是最后一个参考,它现在可以被垃圾收集。


当然,问题是如果你真的需要分配这么大的内存量b $ b。如果你只是需要迭代一些数字,那么使用''xrange''而不是''range'更好'
:''xrange''不会创建

预先提供完整的数字列表,但会创建一个迭代器,

逐个产生所需的数字。如此大的范围,这个

将显着降低内存消耗。


干杯,


--Tim


O

我看到


ma ************** @ gmail.com 写道:


我的应用程序存在很大的内存问题。

首先,一个例子:

a = range(500 * 1024)

我看到python进程分配大约80Mb的内存。
我能做些什么? DEALLOCATE这个记忆,或者这个很好的部分?

我真正的问题是我的程序可以处理更多的照片,我用PIL打开包装。
当我启动时,我的程序分配大约200Mb内存。

如果我想要中止实际工作并重新启动而不重启python
进程,内存使用量将增加大约380-400 Mb。

我已经尝试过使用gc python模块,但是不能正常工作(它取消分配大约20-30 Mb)<我已经尝试过Destroy,del命令了,但是记忆没有显示出来。

Thx

我非常绝望



无需。仅仅因为这个过程看起来那么大并不意味着蟒蛇消耗了大量的内存。只是现代操作系统

将无法正确显示真正消耗的内存量,因为它们只需要分配的虚拟内存就不会太急于删除a b $ b过程已经被授予了b $ b。我想这是一个优化方案。例如,如果我这样做

a = range(500 * 1024 * 50)



python进程的VmSize增长到500MB。如果我发出一个
del a


它缩小到300MB。重新发布

a =范围(500 * 1024 * 50)


将使其增长 - 但仅限于400MB左右。然后我做了

del a
a = [chr(i%255)for i in xrange(500 * 1024 * 50)]



这次我使用了字符串来绕过一些内部缓存python可能会对数字进行
。但仍然 - 整体VmSize是400MB


底线:不要被tsakmanagers内存大小显示混淆。当消费稳定增长时,你只需要关注
- 这表明

a内存泄漏。


Diez


Hi,
I''ve a big memory problem with my application.

First, an example:

If I write:

a = range(500*1024)

I see that python process allocate approximately 80Mb of memory.
What can i do for DEALLOCATE this memory, or good part of this?

My really problem is that my program work with more photos, which I
open with PIL package.
When I start it, my program allocate approximately 200Mb memory.

If I want abort actual work and restart without restarting python
process, the memory usage will go up approximately 380-400 Mb.

I would like find something that DEallocate the memory not used.

I''ve tried with gc python module, but don''t work fine (it deallocate
approximately 20-30 Mb)
I''ve tried with Destroy, del command, but the memory don''t show down.

Thx

I''m very desperate

解决方案

Hi,

''del a'' should remove ''a'', as a reference to the tuple created by the
''range'' function.
If that is also the last reference, it can now be garbage-collected.

Of course, the question is if you really need to allocate such a big
amount of memory. If you just need to iterate over some numbers, it''s
better to use ''xrange'' instead of ''range'': ''xrange'' will not create the
whole list of numbers in advance, but will create an iterator,
producing the desired numbers one by one. With such large ranges, this
will reduce memory consumption significantly.

Cheers,

--Tim


O
I see


ma**************@gmail.com wrote:

Hi,
I''ve a big memory problem with my application.

First, an example:

If I write:

a = range(500*1024)

I see that python process allocate approximately 80Mb of memory.
What can i do for DEALLOCATE this memory, or good part of this?

My really problem is that my program work with more photos, which I
open with PIL package.
When I start it, my program allocate approximately 200Mb memory.

If I want abort actual work and restart without restarting python
process, the memory usage will go up approximately 380-400 Mb.

I would like find something that DEallocate the memory not used.

I''ve tried with gc python module, but don''t work fine (it deallocate
approximately 20-30 Mb)
I''ve tried with Destroy, del command, but the memory don''t show down.

Thx

I''m very desperate



No need to. Just because the process seems to be that large doesn''t mean
that there is so much memory consumed by python. It''s just that modern OSs
will not correctly display the amount of memory really consumed, as they
aren''t too eager to remove once allocated virtual memory a process has been
granted. I guess it''s an optimization scheme. For example if I do

a = range(500*1024 * 50)

the VmSize of the python process grows to 500MB. If I issue an
del a
it shrinks to 300MB. The reissuing
a = range(500*1024 * 50)
will make it grow - but only to about 400MB. Then I do
del a
a = [chr(i % 255) for i in xrange(500*1024 * 50)]


This time I used strings, to circumvene some internal caching python might
do on numbers. Yet still - the overall VmSize is 400MB

Bottomline: don''t get confused by tsakmanagers memory size display. You only
have to be concerned when the consumption steadily grows - which indicates
a memory leak.

Diez


这篇关于Python内存释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆