• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stack overflow error... what the limit?

 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I'm working on project and decided to use recursion instead of loops to try gain more points. The only problem is that the number of iterations required could fairly high, which would cause a StackOverflowException if I use recursion, whereas if I use loops this won't be a problem. I might still use the recursive method but without knowing what the maximum number of iterations can be it's a little concerning. Does anyone know exactly how many recursive calls Java can tolerate (or does anywhere atleast know where to find it... i checked here and didn't find anything useful). Can anyone help please.

Thanks in advance.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably varies from machine to machine. I have had problems with >5000 recursion depth with 1GB RAM and a Pentium 5, but that probably would vary with the amount of RAM available.
 
Olivier Legat
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank-you Ritchie That's all I needed to know.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Probably varies from machine to machine. I have had problems with >5000 recursion depth with 1GB RAM and a Pentium 5, but that probably would vary with the amount of RAM available.


i think what exactly you do in each recursive step makes a difference as well. If you create 1000 objects for each iteration, you won't get as far as if you are only creating one or two...
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point, Fred, thank you.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You can also look into increasing the size of the stack for each thread using the -Xss. However, be careful with this -- with a combo of many threads and a large stack size, you can run out of memory really quick.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic