有一段服务器代码,特点是线程创建多,数据库查询多。 
在我本地机器上跑没有问题,在客户机器上跑就会出java.long.OutOfMemoryErr: unable to create new native thread。出现的地方很多,比如线程start,通过newInstance创建对象,创建数据库连接。 通过不停创建线程测试,在我本地可以创建4000多个线程,可是在客户机器上可以创建的线程数量不超过40。 
测试代码: 
package test;   
public class Test implements Runnable{   
     
  private static Object waitObj = new Object();   
  
  private int threadNo;   
  
  public Test(int aNo) {   
    super();   
    threadNo = aNo;   
  }   
  
  public static void main(String[] args)  {   
  
    int iNo = 1;   
    while(true){   
      Test t = new Test(iNo);   
      Thread th = new Thread(t);   
      th.start();   
      try {   
        Thread.sleep(10);   
      } catch (InterruptedException e) {   
        break;   
      }   
      iNo ++;   
    }   
    waitObj.notifyAll();   
  }   
  
  public void run() {   
    System.out.println("Thread " + threadNo + " created.");   
    synchronized(waitObj){   
      try {   
        waitObj.wait();   
      } catch (InterruptedException e) {   
        System.err.println("InterruptedException at Thread " + threadNo + ".");   
      }   
    }   
    System.out.println("Thread " + threadNo + " finished.");   
  }   
}  package test;
public class Test implements Runnable{
  
  private static Object waitObj = new Object();  private int threadNo;  public Test(int aNo) {
    super();
    threadNo = aNo;
  }  public static void main(String[] args)  {    int iNo = 1;
    while(true){
      Test t = new Test(iNo);
      Thread th = new Thread(t);
      th.start();
      try {
        Thread.sleep(10);
      } catch (InterruptedException e) {
        break;
      }
      iNo ++;
    }
    waitObj.notifyAll();
  }  public void run() {
    System.out.println("Thread " + threadNo + " created.");
    synchronized(waitObj){
      try {
        waitObj.wait();
      } catch (InterruptedException e) {
        System.err.println("InterruptedException at Thread " + threadNo + ".");
      }
    }
    System.out.println("Thread " + threadNo + " finished.");
  }
}本地机器和客户机器装的jdk版本是同样的。 看过http://jroller.com/rreyelts/date/20040909 
意思大概是说分配个虚拟机的内存越多,可以创建的线程数量就越少。 
通过java -Xmx1000M test.Test这样,在本地上可以看出来变化效果,但是在客户机器上看不出来有任何变化。 客户机器输出: 
java test.Test 
Thread 1 created. 
Thread 2 created. 
Thread 3 created. 
Thread 4 created. 
Thread 5 created. 
Thread 6 created. 
Thread 7 created. 
Thread 8 created. 
Thread 9 created. 
Thread 10 created. 
Thread 11 created. 
Thread 12 created. 
Thread 13 created. 
Thread 14 created. 
Thread 15 created. 
Thread 16 created. 
Thread 17 created. 
Thread 18 created. 
Thread 19 created. 
Thread 20 created. 
Thread 21 created. 
Thread 22 created. 
Thread 23 created. 
Thread 24 created. 
Thread 25 created. 
Thread 26 created. 
Thread 27 created. 
Thread 28 created. 
Thread 29 created. 
Thread 30 created. 
Thread 31 created. 
Thread 32 created. 
Thread 33 created. 
Thread 34 created. 
Thread 35 created. 
Thread 36 created. 
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(Unknown Source) 
        at test.Test.main(Test.java:29) 机器配置: 
  本地:intel pentium dual E2160 @ 1.80G 1.79G, 1.99G内存 
  客户机器:AMD xxxx 都快困扰死我了。 就15分了,以后又分一定补上。 
QQ:84403389 
Email:[email protected] 
等。谢谢。 

解决方案 »

  1.   

    java.lang.OutOfMemoryError内存溢出了。给jvm多分配点儿内存。
    其实应该用线程池,别一个劲儿的新建thread,很耗资源的。
      

  2.   

    实际代码中用的是线程池。
    在这里只是测试,在客户机器上最多也就能创建30多个线程了,再多了就不行。线程池也无济于事。
    试过-Xmx参数,没有效果。
      

  3.   

    内存不足问题,更改一下内存设定java -Xmx512m test.Test 
      

  4.   

    终于找出来了,安装了360的问题,卸载了就没事了。
    360好像用了太多的内存,而且用的内存大小很不稳定,时大时小,。机器是2G内存,有时候可以跑起来,但大多情况都说内存不够分配,用Xmx直接报内存分配错误的异常了。
    我机器也装360了,但是一切正常。不知道是360版本问题还是系统版本问题,都是XP。 
    结贴了。