java.lang.OutOfMemoryError: unable to create new native 해결법

2008. 12. 22. 15:57Java

The reason for this is the stack size. On Linux, each thread gets its own stack with a default size of 512kB. On my machine, there is only 3 GB for the stacks, so in theory I can only create 6144 threads. But there is more that needs memory, therefore I can only create about 5700 threads. If I increase to 1MB of stack, I can create about 2700 threads and when I reduce it to 128kb, I can create much more.

You can have a look at the process with "pmap" to see the memory allocated by the threads. Generally speaking, if you need more than the mentioned 400 threads, try to reduce the stack size using "-Xss128k" which should give you about 4 times the threads. Most applications should work fine with this stack size. If not, you'll get a "StackOverflowError" and have to increase the stack. I found a lower limit mentioned of 96kb on Linux.
 
스레드당 512kb  만큼 스택 사이즈가 지정되는데
이게 스레드 개수만큼 늘어나면서 메모리가 풀 나 버리면 위와 같은 에러가 난다.

스택 사이즈를 줄이면 해당 사이즈 만큼 스레드를 더 돌릴수 있다.