threads and memory

Bryan Olson fakeaddress at nowhere.org
Mon Feb 6 14:32:44 EST 2006


Lee Leahu wrote:
> I am trying to write a simple threaded application which will
 > simulate 1000 connections to a remote service in order to
 > stress test" that the remote service can handle that many
 > connections.

That shouldn't be a problem on a modern OS, but there are
still quite a few not-up-to-speed systems in the field that
choke on just a few hundred threads. There are also many
systems with modern threading that choke because they are
poorly configured by default.

> However, I have encountered the following error after I
 > have started my 381st thread:

[...]
> I have 1.5GB of total ram in my computer, as evident by the
 > command 'free':
[...]
> NOTE the line that reads 'ENOMEM (Cannot allocate memory).

The error message may be misleading. The limiting factor on
the number of threads is usually address space, not physical
memory. You can get ENOMEM when the system cannon allocate
stack space for a thread. The space it needs is not RAM,
nor even RAM+swap; it's pure virtual address space.

Look up how to set the stack-space for threads on your
system. A reasonable size on a 32-bit system is 2MB to
4MB. Then you should be able to create threads numbering
in the low thousands. (On 64-bit systems, the issue just
goes away.)


-- 
--Bryan



More information about the Python-list mailing list