aspose file tools*
The moose likes Java in General and the fly likes StackOverflowError in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "StackOverflowError in java" Watch "StackOverflowError in java" New topic
Author

StackOverflowError in java

Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
hi,

i am running simple factorial program on my eclipse (fedora 14). I have 3gb ram

i am getting stackoverflow error when input is 100000.Max input i want to give my code is 1000000000 i.e 10 ^ 9

how to avoid this error

my code is given below



Thanks
user101
Problems And Solutions - Algorithms
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

Your factorial program is recursive.

Each time you do a recursive call, the call takes up some memory from the stack. The size of the stack is limited, it doesn't grow automatically, so it doesn't really matter that your PC has 3 GB RAM.

If you would enter 1,000,000,000 as the input, you'll make a billion nested method calls. I don't know exactly how many bytes a stack frame for a method call costs, but it's probably a few tens to a few hundred bytes. So if you do a billion nested calls, this will cost tens to hundreds of gigabytes of memory! That will never fit.

You should rewrite your method so that it is not recursive if you want this. And even then I doubt if it is going to work, because the sequence of factorials grows very fast. A number such as 1000000000! is so big that it might be hard to fit it in memory by itself.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
hi,

i can use recursive and iterative approach both like recursive method inside for loop and i think that may solve problem.

but this type of questions are asked in competitions so i wanted to know if we can use recursive function alone.

Can i increase the stack size of jvm and make it run for 10 ^ 9 ..?

thanks for reply
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

Yes, you can change the stack size with the -Xss option:

java -Xss2M org.mypackage.MyProgram

This will set the stack size to 2 MB. However, as I already said, for 1 billion nested method calls, you'd need to set it to 10 GB or 100 GB. That's not going to work - your computer doesn't have that much memory. Besides that, the operating system might have a limit for the stack size of threads, which is most likely far less than a gigabyte.
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
ok its not completely impossible..


so therotically if i have memory more than 10 GB on in TB i can run such recursive function.


now question is if i cant run my current program for large input, what modification i need to do in my current code to make it work.?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

You should make it iterative instead of recursive: just use a loop instead of have the method call itself.
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Thank you

All your replies were really helpful.


now will modify same code where i will include iteration,recursive and memoization techniques together.

Thanks
 
 
subject: StackOverflowError in java