Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

24 Threads - java.lang.OutOfMemoryError: unable to create new native thread

843829Oct 16 2007 — edited Feb 9 2009
Hi Guys

I have created a simple java test class that consistantly throws a java.lang.OutOfMemoryError: unable to create new native thread exception when trying to start the 24th user thread.

The following test was ran on a 4 i686 CPU server with 8GB memory, 2GB swap and using RHEL3 Update 5.
uname -a reports:
Linux <hostname> 2.4.21-32.0.1.ELsmp #1 SMP Tue May 17 17:52:23 EDT 2005 i686 i686 i386 GNU/Linux

This Test class starts a given number of threads.
public class Test {
    public static void main(final String[] args) throws Exception {
        // start the given number of threads
        for (int i = 1; i <= Integer.parseInt(args[0]); i++) {
            System.out.println("Starting Thread " + i);
            final Thread t = new Thread("T[" + i + "]") {
                public void run() {
                    try {
                        while (true) {
                            Thread.sleep(1000);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            t.setDaemon(true);
            t.start();
            Thread.sleep(5);
        }
        // wait
        Thread.sleep(1000000);
    }
}
Using jdk1.5.0_13 I ran the test with the following parameters

1) java -Xmx724m Test 500
2) java -Xmx725m Test 500

The 1st test successfully created all 500 threads.

The 2nd test fails with the following output:
Starting Thread 1
Starting Thread 2
...
Starting Thread 23
Starting Thread 24
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:574)
        at Test.main(Test.java:17)
If I use jdk1.6.0_03 then the above exception is thrown regardless of the -Xmx setting.

I get the same results even after removing all hard and soft ulimits and using an additional -Xss64k parameter.

It seems to me like an integer overflow bug. Googling for solutions just comes up with discussions about a suggested "#threads * Xss + Xmx <= MAX Xmx" inequality.

Until I find a solution to this, I cannot run a tomcat server with more than ~700MB memory or using JDK 1.6.

Is this a misconfiguration or a bug? Is the problem with the JDK, the operating system or the combination?

More importantly, how do I fix this?

Thanks

PS
+/usr/sbin/lsof+ returns the following list of open files.
/lib/ld-2.3.2.so
/lib/libc-2.3.2.so
/lib/libdl-2.3.2.so
/lib/libm-2.3.2.so
/lib/libnsl-2.3.2.so
/lib/libnss_files-2.3.2.so
/lib/libpthread-0.10.so
/usr/lib/locale/locale-archive
~/jdk1.5.0_13/bin/java
~/jdk1.5.0_13/jre/lib/charsets.jar
~/jdk1.5.0_13/jre/lib/ext/dnsns.jar
~/jdk1.5.0_13/jre/lib/ext/localedata.jar
~/jdk1.5.0_13/jre/lib/ext/sunjce_provider.jar
~/jdk1.5.0_13/jre/lib/ext/sunpkcs11.jar
~/jdk1.5.0_13/jre/lib/i386/libjava.so
~/jdk1.5.0_13/jre/lib/i386/libverify.so
~/jdk1.5.0_13/jre/lib/i386/libzip.so
~/jdk1.5.0_13/jre/lib/i386/native_threads/libhpi.so
~/jdk1.5.0_13/jre/lib/i386/server/libjvm.so
~/jdk1.5.0_13/jre/lib/jce.jar
~/jdk1.5.0_13/jre/lib/jsse.jar
~/jdk1.5.0_13/jre/lib/rt.jar
Edited by: garymorgan on Oct 16, 2007 3:34 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 9 2009
Added on Oct 16 2007
7 comments
1,734 views