mvn test java.lang.OutOfMemoryError: unable to create new native thread
Asked Answered
N

3

5

When I run mvn test I receive the below exception. I have tried both raising and lowering my Xmx and Xss JVM settings and bumping all limits under ulimit. There are about 1300 tests and the last 200 always fail with this exception. Running those tests by themselves allows them to pass. The same tests pass on my ubuntu box. I receive this exception when running the tests on my mac. I am pretty sure its an environment issue, but I have tweaked every setting that I am aware of with absolutely no luck.

I am using mvn 2.1 and Java 6. My test framework is junit 4.8.

java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:658)
        at java.util.concurrent.ThreadPoolExecutor.addIfUnderMaximumPoolSize(ThreadPoolExecutor.java:727)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:657)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:92)
        at com.google.appengine.tools.development.ApiProxyLocalImpl$PrivilegedApiAction.run(ApiProxyLocalImpl.java:197)
        at com.google.appengine.tools.development.ApiProxyLocalImpl$PrivilegedApiAction.run(ApiProxyLocalImpl.java:184)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.google.appengine.tools.development.ApiProxyLocalImpl.doAsyncCall(ApiProxyLocalImpl.java:172)
        at com.google.appengine.tools.development.ApiProxyLocalImpl.makeAsyncCall(ApiProxyLocalImpl.java:138)
Nigrify answered 23/5, 2011 at 15:52 Comment(3)
You should get a thread dump before it fails. It may be forking a thread for each test which could get messy.Culex
Hmm, I dont. Do I need to provide any additional mvn args to get that? I am using the surefire plugin to run the tests. Also, I tried passing the argument to disable forking in tests and that did not help.Nigrify
See also this answer hereProthesis
C
5

I ran into this problem using the Maven Surefire plugin (v2.12). I had to configure surefire as follows:

<configuration>
  <forkMode>always</forkMode>
  ... other surefire config ...
</configuration>

If I omitted the "forkMode" element in my config, I would get the "unable to create new native thread" error because the java Surefire process would create thousands of threads otherwise, exceeding my OS limit (Mac OSX -- you can see this in the Activity Monitor).

As best I can tell, all the new threads are getting created because the default "forkMode" is "once" in Surefire, and whatever new threads are getting created don't die off until the "one" surefire process terminates.

One final note: adjusting my JVM memory settings seemed to have no effect (good or bad). Using default values worked normally, as did the following:

<argLine>-Xss512k -Xms512m -Xmx4096m -XX:MaxPermSize=2048m</argLine>
Conventionality answered 27/5, 2012 at 17:38 Comment(1)
This worked! I wasnt explicitly including the surefire plugin in my pom (looks like it is automatically included...), but defining it as a plugin, updating to the latest version and providing the 'always' forkMode has fixed the issue. Thanks! For future viewers of this, here is some more documentation on the surefire plugin: maven.apache.org/plugins/maven-surefire-plugin/usage.htmlNigrify
A
3

Related to sappenin's answer, you should use the forkCount and reuseForks configuration parameters (instead of deprecated forkMode) for newer versions of the surefire plugin. The resulting plugin configuration might look like the shown code.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <forkCount>1</forkCount>
                <reuseForks>false</reuseForks>
                <argLine>-Xms256m -Xmx1024m</argLine> 
            </configuration>
        </plugin>
    </plugins>
</build>

citation: http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

Amersham answered 7/1, 2015 at 17:55 Comment(0)
R
2

You (or someone acting on your behalf) is creating too many threads in your tests. Confirm this by running jstack on the forked test process while it runs, it'll almost certainly show a huge and increasing number of threads.

Try limiting the size of your thread pool or make sure they are allocated properly. If the mac supports something like ulimit you might be able to increase the max number of threads per process. You will fail even more miserably on Windows.

Rosemarierosemary answered 23/5, 2011 at 17:43 Comment(2)
My app is not creating threads anywhere since they cant be used on the App Engine. Maven might be, but when profiling it with jconsole, I only see ~25 threads during test execution. I have already set ulimit max processes to unlimited... Everything works on ubuntu. I just cant figure out what is different on my mac.Nigrify
A piece of code creating threads in a tight loop can fail with this kind of error in less than half a second, so that might be happening. Maybe run some kind of profiler and watch the threads there.Rosemarierosemary

© 2022 - 2024 — McMap. All rights reserved.