This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Stack Memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Stack Memory" Watch "Stack Memory" New topic

Stack Memory

Mohtashim Shaikh
Ranch Hand

Joined: Jul 18, 2010
Posts: 43
I need to know what is the stack size i.e. -Xss my java program is running with.
Is there a way to find that out from inside the java program code and outside of it.

KayCee Clark

Joined: Jun 22, 2011
Posts: 13

I have not had experience looking at this but here is what I found.

I tried JConsole ( in your bin directory ). That gives you a lot of information about various pools, but no specific mention of stack.

I found something that indicates setting the stack size higher may be a problem because each thread gets that amount of space and you can actually run out of memory faster.

I found this HotSpot parameter documentation that mentions some default sizes.

-XX:ThreadStackSize=512 Thread Stack Size (in Kbytes). (0 means use default stack size) [Sparc: 512; Solaris x86: 320 (was 256 prior in 5.0 and earlier); Sparc 64 bit: 1024; Linux amd64: 1024 (was 0 in 5.0 and earlier); all others 0.]

on this page

There is another parameter for Solaris only on that page as well.

Have fun !

KayCee Clark

Joined: Jun 22, 2011
Posts: 13

A little bit more, but I don't (yet?) see any direct way to just look at stack size programatically.

A good page on tuning, including memory, from Oracle.


I found this page where the author provides his code

and his reasoning behind the test and calculations.

Also, some code here, but again, nothing specific for


Mohtashim Shaikh
Ranch Hand

Joined: Jul 18, 2010
Posts: 43

Thank you for your reply. However, what i am looking for is to read whatever the current set -Xss (stack sie) is for a particular JVM i.e PID be it default or set eplicitly from inside a unix shell script or a java program inorder to setup a monitoring alert script.

A third-party tool may also come handy only if we can use its API inorder to read the stack size inside the unix shell script or a java program.

Kindly help !!!

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38107
Moving discussion as too difficult for "beginning".
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

There is no (easy) way of getting the stack size for threads in a Java process.

There is for example no system property that you could get with System.getProperty(...), and even the JVM TI (an API for debugging and profiling tools to get information from and control a running JVM) doesn't seem to have any way to get the stack size.

Why do you need this?

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

Joined: Jul 18, 2010
Posts: 43
The pmap command for solaris tells us the current stack usage for a JVM. What is th use of the current stack size that it displays when there should be no way to find the upper limit of stack which that JVM is allowed to run with.

This deprives us for writing a script that alerts as the stack size reaches more than 90% of allocated stack size. Thats why i need it.
I agree. Here's the link:
subject: Stack Memory
Similar Threads
What is stored on Stack and Heap
Simple Java Program throwing cannot create process =193 error
how to clear the stack
Doubt in stack