3

我有一个基本的 python 程序,它可以创建大量线程(2000),处理一些东西,然后把它写出来。

我已将我的代码缩小到与此类似(使用 2k 线程): URL fetch thread example on: http ://www.ibm.com/developerworks/aix/library/au-threadingpython/

除了在我的课堂上,我实际上什么都不做(从队列中获取项目,然后调用任务完成)。在这个缩小版和我做事的版本中,内存使用是一样的。在 32 位 python 解释器中,我使用了大约 105 兆的虚拟内存。在 64 位中,我使用了超过 8 个演出。

我正在运行 rhel 6。我还添加了:threading.stack_size(32768) 以减小堆栈大小。我假设 python 正在获取一些默认的内存限制,我只是无法弄清楚这个限制是什么。

有任何想法吗?

谢谢!

4

2 回答 2

2

If you are on RHEL6 or your glibc is newer than 2.10 (you can check with rpm -q glibc). It's due to the missing of MALLOC_ARENA_MAX.

In RHEL 6, the malloc of glibc (>=2.10) has a new arena allocator which allows each thread to be able to allocate its own arena. And the max number of re-usable arena depends on the number of cores. On a 64-bit system, these arenas are 64M mappings and the default number of arenas for a 16-cores system could reach up to 128. You can easily get 128*64MB = 8GB. So it can result in huge amount of virtual memory (VMS) when using many threads (though the increase in RSS could be completely normal.)

This can be resolved by setting the env. variable MALLOC_ARENA_MAX to a small number like 1 or 4.

于 2013-05-27T14:58:29.067 回答
0

如果你真的需要 2k+ 线程,你将会有兴趣阅读 Global Interpreter Lock (GIL):http ://wiki.python.org/moin/GlobalInterpreterLock

于 2012-11-08T17:55:12.130 回答