1

In Java, objects are stored in the heap but method local variables are in the stack. But when I open jconsole, I do not see any memory allocated in the stack. I just see heap and permgen (Java 6). Is the stack just a logical separation in the heap and not physical?

trincot
  • 317,000
  • 35
  • 244
  • 286
emilly
  • 10,060
  • 33
  • 97
  • 172
  • 1
    See [Is frame in JVM heap allocated or stack allocated?](http://stackoverflow.com/questions/26741925/is-frame-in-jvm-heap-allocated-or-stack-allocated/26779782#26779782) and [How do I get stack memory stats programatically?](http://stackoverflow.com/questions/26835087/how-do-i-get-stack-memory-stats-programatically/26852490#26852490) – apangin Feb 16 '17 at 07:10
  • BTW I suggest you migrate from Java 6 to 8 and `jconsole` to `jmc` as soon as possible. – Peter Lawrey Feb 16 '17 at 09:03
  • The fact that `jconsole` doesn’t show this information doesn’t allow any conclusion about the organization of the stack. – Holger Feb 16 '17 at 15:26
  • Stack and Heap are just memory that happen to be used as stacks and heaps. They are allocated in the same way from the operating system. – user3344003 Feb 16 '17 at 17:25

2 Answers2

1

No. Stack is a different area of memory dedicated to holding information related to method calls, such as argument values and where program flow should return after the method returns.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
1

The JVM uses the native stack of the process. This minimise overhead, and allows the stack to be virtual (it can have a maximum size much larger than what is actually used)

As a result most stacks are rarely used much ( << 10% of maximum ) and graphing them might be more confusing than useful.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • `The JVM uses the native stack of the process.` I believe it means memory allocated for process by OS. Then it must be separate from heap mentioned through -Xms and -Xmx parameters. ? – emilly Feb 16 '17 at 13:36
  • @emilly only virtual memory/address space is allocated. When this happens only the pages touched turn into real memory.i.e if you set the maximum to 10 MB, but you use only 21 KB, then 6 * 4 KB is actually used (24 KB) You can set the maximum size for a stack with `-Xss` though this is rarely needed. – Peter Lawrey Feb 16 '17 at 14:12
  • sorry. say I have total ram of 4 GB. I have allocated 1GB max memory for heap. Now stack for some reason needs 512M memory then it will be allocated out of 3GB ( 4-1) ? – emilly Feb 16 '17 at 14:59
  • @emilly each thread has it's own stack. I would leave it with the default setting unless you know it's a problem. – Peter Lawrey Feb 16 '17 at 16:20
  • I am not facing any issue right now and not going to change any default setting . But want wan to understand whether memory for stack region is allocated out of heap or it allocated from rest of the RAM ? – emilly Feb 17 '17 at 05:11
  • @emilly the stack, direct memory, shared libraries, GUI components, PermGen/metastases, memory mapped files use native memory rather than the heap. – Peter Lawrey Feb 17 '17 at 06:26