This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes Can we specify call stack depth in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we specify call stack depth in Java?" Watch "Can we specify call stack depth in Java?" New topic
Author

Can we specify call stack depth in Java?

Girish K Gupta
Greenhorn

Joined: Mar 14, 2007
Posts: 19

Is it possible to set the maximum number of call stack depth in Java?

I found that the non standard JVM argument (-Xss) is used to specify the maximum memory of a particular stack BUT not the number of frames in the stack.

Can anyone throw some light or provide any pointers?


Girish K Gupta
SCJP 1.5 - 87%, SCWCD 1.5 - 92%
India
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1466
    
    5

Girish K Gupta wrote:Is it possible to set the maximum number of call stack depth in Java?

I found that the non standard JVM argument (-Xss) is used to specify the maximum memory of a particular stack BUT not the number of frames in the stack.

Can anyone throw some light or provide any pointers?

Welcome to CodeRanch!

I'm not sure whether this is generically possible, but if you are concerned with recursion, then getStackTraceDepth method of Throwable class might be helpful. It won't control anything, but it would return the depth, based on which, you can modify the behavior of the method.

I hope this helps.

Thanks.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Girish K Gupta wrote:Is it possible to set the maximum number of call stack depth in Java?

Even if it was, I'm wondering why you would want to do it? In eleven years of writing Java, I've never had to worry about it (in fact, I doubt if I've even approached it) except when I've got a bug.

It's set high enough that running out of stack room usually represents a serious error in your code - or your design - and you don't solve that by setting it higher.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

I don't think there is such a thing as a maximum stack depth. There is a maximum amount of memory that can be used for the stack. This can be tweaked using -Xss followed by the size; for example, -Xss64M will have a stack size of 64MB. Each time a method is called, its variables are put on the stack until this 64MB is full.

As an example, run the following using both java Test and java -Xss64M Test, and see the difference:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Girish K Gupta
Greenhorn

Joined: Mar 14, 2007
Posts: 19

Thank you all for your replies

@Anayonkar, I could not find any method, getStackTraceDepth, in Throwable class. Checked in Java6 specifications. Please let me know if I am missing anything.

@Winston, I completely understand your view point but I have worked on a customized JVM for embedded devices where a maximum of 128 call depth was allowed. For standard Java this is not a constraint at all. I asked this question more from understanding point of view.

@Rob, I tried your test application and learnt what I wanted. Thanks again !
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1466
    
    5

@Anayonkar, I could not find any method, getStackTraceDepth, in Throwable class. Checked in Java6 specifications. Please let me know if I am missing anything.

My mistake. The method is native, so documentation doesn't contain it. You'll have to call getStackTrace and then get length of returned array.

I hope it helps.
Girish K Gupta
Greenhorn

Joined: Mar 14, 2007
Posts: 19

Hi Anayonkar

I tried it but the length of arrays returned getStackTrace() method is always 1024 for my system while value of depth variable is around 12000 in different executions.

Following is the Test application code I am using:



Thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1466
    
    5

Girish K Gupta wrote: I tried it but the length of arrays returned getStackTrace() method is always 1024 for my system while value of depth variable is around 12000 in different executions.

This is where those JVM arguments come into picture (where you can change JVM's memory size etc.)
However, in my opinion, even 1024 is much more. As some of the replies mention, increasing the JVM size is not the solution.

The first question I would ask myself - what kind of operation requires a method calling itself 12000(or 1024) times?
The second question - Can it be done by using a loop instead of recursion?

In general, recursion is very helpful - provided its done correctly. Once it gets out of control, then debugging is much difficult.

I hope this helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we specify call stack depth in Java?
 
Similar Threads
Question on hibernate IN Operator
Recursive functions
increase jvm size in linux
Tomcat Thread Pool configured as 3,000
multiple recursion and stacks