Reputation: 525
I am trying to display the consumed stack space during a recursive program execution, like inorder traversal of a tree. Is there a way to print the available stack space in Java, I know that available heap memory could be displayed using Runtime.getRuntime().freeMemory().
Upvotes: 0
Views: 670
Reputation: 308249
Java doesn't give you access to this kind of information, for various reasons:
The closest you can get is to get a current stack trace (for example using Thread.getStackTrace()
) and check the returned arrays size to know how many stack frames are in use (i.e. how deep the stack already is).
But even that operation is likely to be somewhat costly.
Upvotes: 1