1

Possible Duplicate:
Can't get past 2542 Threads in Java on 4GB iMac OSX 10.6.3 Snow Leopard (32bit)

I'm running a thread intensive application onto my Mac 64 bit with 8 Gbyte of memory. I have tried to vary the option of the VM arguments (into Eclipse and in cmd line) but for 2500 thread I always got "Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread".

When it crashes I saw that heap memory is only 100 Mbyte...

What's wrong?

Community
  • 1
  • 1
robob
  • 1,739
  • 4
  • 26
  • 44
  • What is the value -Xmx -Xss in the Run/Debug Settings of your application in eclipse? Please also check the mode in which you are running the JVM i.e server or client – Anupam Saini May 26 '11 at 06:00
  • Perhaps there is a better way to do what you are trying to do. How many cores do you have? Do you really need 2500 threads? – Peter Lawrey May 26 '11 at 06:15
  • Yes it is a simulation of a network onto the same JVM. But I found the solution. Run two different process. See my comment to the answer. – robob May 26 '11 at 06:33

2 Answers2

9

There is a limit on the maximum number of threads which is independent of how much memory you actually have. See this question.

Community
  • 1
  • 1
hammar
  • 138,522
  • 17
  • 304
  • 385
  • Yes the magic number is 2560. It's a limit of Mac. I have to increase the maximum thread number. Thank you – robob May 26 '11 at 06:28
  • http://support.apple.com/kb/HT3854. I could execute two different session to overcome the 20% of 12500 (2500) threds for process. Thanks a lot again for the hint – robob May 26 '11 at 06:31
4

Most implementations of the JVM run into limits after creating several thousand threads. While there might be ways to tune around this (like reducing the stack size with -Xss), there aren't many good reasons you'd use thousands of threads unless you had thousands of processors.

Instead you should consider using fewer threads for better efficiency. Something to close the number of processors that you have will be ideal if it's CPU intensive. If they're blocked on something non-CPU then more threads than processors could help.

You can use a ExectorService to manage a thread pool. E.g.:

int processors = Runtime.getRuntime().availableProcessors();
ExecutorService service = Executors.newFixedThreadPool(processors);
WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
  • +1 for thread pools, when used correctly they are very easy to implement when you want to cheaply expand an application's performance and scalability. – NightWolf Feb 16 '12 at 06:11