Red Hat Linux中的Java单进程线程限制低 [英] Low Java single process thread limit in Red Hat Linux

查看:60
本文介绍了Red Hat Linux中的Java单进程线程限制低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Java 1.6(1.6.0_02或1.6.0_04)运行Red Hat Linux(内核版本为2.4.21-37.ELsmp)的测试计算机上遇到问题.问题是,一旦在单个线程组中创建了一定数量的线程,操作系统将不愿意或无法再创建更多线程.

I'm experiencing an issue on a test machine running Red Hat Linux (kernel version is 2.4.21-37.ELsmp) using Java 1.6 (1.6.0_02 or 1.6.0_04). The problem is, once a certain number of threads are created in a single thread group, the operating system is unwilling or unable to create any more.

这似乎特定于Java创建线程,因为C线程限制程序能够创建大约1.5k线程.此外,使用Java 1.4 JVM不会发生这种情况……它可以创建超过1.4k线程,尽管显然它们在操作系统方面的处理方式有所不同.

This seems to be specific to Java creating threads, as the C thread-limit program was able to create about 1.5k threads. Additionally, this doesn't happen with a Java 1.4 JVM... it can create over 1.4k threads, though they are obviously being handled differently with respect to the OS.

在这种情况下,它截断的线程数仅为29个线程.这可以通过一个简单的Java程序进行测试,该Java程序仅创建线程直到出现错误,然后打印其创建的线程数.错误是

In this case, the number of threads it's cutting off at is a mere 29 threads. This is testable with a simple Java program that just creates threads until it gets an error and then prints the number of threads it created. The error is a

java.lang.OutOfMemoryError: unable to create new native thread

这似乎不受其他进程或用户正在使用的线程数或系统当时正在使用的内存总量之类的影响.像Xms,Xmx和Xss这样的JVM设置似乎也没有任何改变(这是预期的,考虑到问题似乎出在本机OS线程创建上).

This seems to be unaffected by things such as the number of threads in use by other processes or users or the total amount of memory the system is using at the time. JVM settings like Xms, Xmx, and Xss don't seem to change anything either (which is expected, considering the issue seems to be with native OS thread creation).

"ulimit -a"的输出如下:

The output of "ulimit -a" is as follows:


core file size        (blocks, -c) 0
data seg size         (kbytes, -d) unlimited
file size             (blocks, -f) unlimited
max locked memory     (kbytes, -l) 4
max memory size       (kbytes, -m) unlimited
open files                    (-n) 1024
pipe size          (512 bytes, -p) 8
stack size            (kbytes, -s) 10240
cpu time             (seconds, -t) unlimited
max user processes            (-u) 7168
virtual memory        (kbytes, -v) unlimited

似乎不是用户进程限制的问题.搜索有关可能出错的信息的信息并没有太多,但是这篇文章似乎表明至少有一些Red Hat内核将一个进程限制为分配给堆栈的300 MB内存,而每个堆栈的每个线程10 MB,这似乎是有问题的(尽管这看起来很奇怪,并且不太可能出现).好).

The user process limit does not seem to be the issue. Searching for information on what could be wrong has not turned up much, but this post seems to indicate that at least some Red Hat kernels limit a process to 300 MB of memory allocated for stack, and at 10 MB per thread for stack, it seems like the issue could be there (though it seems strange and unlikely as well).

我已经尝试使用"ulimit -s"更改堆栈大小以进行测试,但是除10240和JVM之外的任何其他值都不会以以下错误开头:

I've tried changing the stack size with "ulimit -s" to test this, but any value other than 10240 and the JVM does not start with an error of:

Error occurred during initialization of VM
Cannot create VM thread. Out of system resources.

我通常可以绕过Linux,但是我对系统配置的了解并不多,而且我还没有找到专门解决这种情况的任何东西.对于可能导致此问题的系统或JVM设置有任何想法.

I can generally get around Linux, but I really don't know much about system configuration, and I haven't been able to find anything specifically addressing this kind of situation. Any ideas on what system or JVM settings could be causing this would be appreciated.

编辑:运行

Edits: Running the thread-limit program mentioned by plinth, there was no failure until it tried to create the 1529th thread.

使用1.4 JVM时也不会发生此问题(在1.6.0_02和1.6.0_04 JVM中会发生,目前无法在1.5 JVM中进行测试).

The issue also did not occur using a 1.4 JVM (does occur with 1.6.0_02 and 1.6.0_04 JVMs, can't test with a 1.5 JVM at the moment).

我正在使用的线程测试代码如下:

The code for the thread test I'm using is as follows:

public class ThreadTest {

   public static void main(String[] pArgs) throws Exception {

      try {
         // keep spawning new threads forever
         while (true) {
            new TestThread().start();
         }
      }
      // when out of memory error is reached, print out the number of
      // successful threads spawned and exit
      catch ( OutOfMemoryError e ) {
         System.out.println(TestThread.CREATE_COUNT);
         System.exit(-1);
      }
   }

   static class TestThread extends Thread {
      private static int CREATE_COUNT = 0;
      public TestThread() {
         CREATE_COUNT++;
      }
      // make the thread wait for eternity after being spawned
      public void run() {
         try {
            sleep(Integer.MAX_VALUE);
         }
         // even if there is an interruption, dont do anything
         catch (InterruptedException e) {
         }
      }
   }
}

如果您使用1.4 JVM运行此程序,它将在无法创建更多线程并需要kill -9(至少对我有用)时挂起.

If you run this with a 1.4 JVM it will hang when it can't create any more threads and require a kill -9 (at least it did for me).

更多修改:

事实证明,出现问题的系统正在使用LinuxThreads线程模型,而另一个运行良好的系统正在使用NPTL模型.

It turns out that the system that is having the problem is using the LinuxThreads threading model while another system that works fine is using the NPTL model.

推荐答案

使用NPTL线程将内核更新为较新的版本(2.6.something).

Updating the kernel to a newer version (2.6.something) with NPTL threading fixed this.

这篇关于Red Hat Linux中的Java单进程线程限制低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆