out of memory吧,jvm资源耗尽就会报出Exception了

解决方案 »

  1.   

    Exception in thread "main" java.lang.StackOverflowError
       at sun.io.CharToByteConverter.convertAny(CharToByteConverter.java:139)
       at sun.nio.cs.StreamEncoder$ConverterSE.implWrite(StreamEncoder.java:208)
       at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:134)
       at java.io.OutputStreamWriter.write(OutputStreamWriter.java:191)
       at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
       at java.io.PrintStream.newLine(PrintStream.java:323)
       at java.io.PrintStream.println(PrintStream.java:516)
       at Container.say(Test1.java:42)
       at Container.say(Test1.java:45)
       at Container.say(Test1.java:45)
       at Container.say(Test1.java:45)
       at Container.say(Test1.java:45)
       at Container.say(Test1.java:45)
       at Container.say(Test1.java:45)
    .
    .
    .
    .
    .
    .
    .
    ..
      

  2.   

    可否想办法让这段程序执行提尽可能的多?
    我试了java -Xmx 没有用的。
      

  3.   

    哈,看到了无数的
       at Container.say(Test1.java:45)
       at Container.say(Test1.java:45)
       at Container.say(Test1.java:45)
       at Container.say(Test1.java:45)
    满满一屏
    对堆栈要求太大了
      

  4.   

    ========================
    以下是同样问题在forum.java.sun.com上帖上后得到的回复。
    共同学习。
    ========================
    Exception in thread "main" java.lang.StackOverflowError
    Author: tomxujack  Dec 19, 2002 9:04 PM      
     
    When I run the java application below,it always throw a exception as:
    how can I run it over completely or more?------------- exception -----------------------------------------
    Exception in thread "main" java.lang.StackOverflowError
    at sun.io.CharToByteConverter.convertAny(CharToByteConverter.java:139)
    at sun.nio.cs.StreamEncoder$ConverterSE.implWrite(StreamEncoder.java:208)
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:134)
    at java.io.OutputStreamWriter.write(OutputStreamWriter.java:191)
    at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
    at java.io.PrintStream.newLine(PrintStream.java:323)
    at java.io.PrintStream.println(PrintStream.java:516)
    at Container.say(Test1.java:42)
    at Container.say(Test1.java:45)
    at Container.say(Test1.java:45)
    at Container.say(Test1.java:45)
    at Container.say(Test1.java:45)
    at Container.say(Test1.java:45)
    at Container.say(Test1.java:45)
    ----------------------------------------------------------
    .
    .
    .--------------------- source code -----------------------
    public class Test1{/*public void say(int i){
    System.out.println(i);
    if((++i)<350000)
    say((++i));
    }
    public static void main(String s[]){
    Test1 t = new Test1();
    t.say(0);
    }
    */
    public static void main(String s[]){Container m = new Container(0);
    Container ma = m;for(int i=1;i<1000000;i++){
    Container c = new Container(i);
    if(m != null){
    m.setSon(c);
    }
    m = c;
    }
    ma.say();
    }}class Container{
    int id;
    Container son;
    public Container(int id){
    this.id = id;
    }
    public void say(){System.out.println(id);if(son != null){
    son.say();
    }
    }
    public void setSon(Container son){
    this.son = son;
    }
    }
     
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: jarreth 
    In Reply To: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 9:06 PM   Reply 1 of 10   
     
     
    why do you need 1000000 components?  
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: tomxujack 
    In Reply To: Re: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 9:16 PM   Reply 2 of 10   
     
     
    I only want to know how many times recursion can do.I think it relative with JVM heap setting.
    but I have set > java -Xmx256000000 .............
    no use.And on different computer,different OS.result is different.  
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: paulcw 
    In Reply To: Re: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 9:33 PM   Reply 3 of 10   
     
     Wait a minute. You've written a program that deliberately tries to overflow the stack. And you get a stack overflow error. Of course.> And on different computer,different OS.result is different.Of course.
    What's the problem exactly?Are you asking how you can make an infinitely large stack?Can't be done. Computers are discrete finite math engines.
     
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: JN_ 
    In Reply To: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 9:46 PM   Reply 4 of 10   
     
     
    > When I run the java application below,it always throw
    > a exception as:
    > how can I run it over completely or more?Stop designing it to intentionally crash.  
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: tomxujack 
    In Reply To: Re: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 9:51 PM   Reply 5 of 10   
     
     
    In a application.I must use recursion to deal with a large object.
    I want to know,how many times JVM allow the recursion do?
    1000 times?
    10000 times?
    100000 times?
    How can I make sure it?  
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: jarreth 
    In Reply To: Re: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 9:59 PM   Reply 6 of 10   
     
     
    print out an incremented number on every recursion entrywhen it blows up, the last number printed is the number of times.  
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: JN_ 
    In Reply To: Re: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 9:59 PM   Reply 7 of 10   
     
     
    > In a application.I must use recursion to deal with a
    > large object.
    > I want to know,how many times JVM allow the recursion
    > do?
    > 1000 times?
    > 10000 times?
    > 100000 times?
    > How can I make sure it?You can't.  
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: tomxujack 
    In Reply To: Re: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 10:08 PM   Reply 8 of 10   
     
     
    But the application is going to run in many different OS,such as window,solair,linux,hareware is quit different.
    I know should control the recursion times.
    But how cano I do?
    Help!  
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: JN_ 
    In Reply To: Re: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 10:11 PM   Reply 9 of 10   
     
     
    Find a way that doesn't require recursion.  
     Re: Exception in thread "main" java.lang.StackOverflowError 
    Author: paulcw 
    In Reply To: Re: Exception in thread "main" java.lang.StackOverflowError  Dec 19, 2002 11:02 PM   Reply 10 of 10   
     
     If stack overflow errors are a likely possibility in your design, then change your design.Really. Trying to hover just below the maximum is just asking for trouble.And anyway, I don't think this question can be answered. The number of stack frames you can get away with depends on the size of the stack frame, which I believe changes per-method. So even if you ran a test that gave you an answer, in production the actual method you use will have different results.And I wouldn't be surprised if the stack frame was also dependant on any native methods called (although I don't know if that actually is an issue or not). So you may not be able to get an answer across OS's.