What is the difference between Java Non Heap Memory and Stack Memory? Are they Same if not what is the difference between them?

user2885295 picture user2885295 · Sep 16, 2014 · Viewed 32.3k times · Source

I am using Jconsole for monitoring a Java Application. The memory tab shows different Heap and Non Heap memories like

  1. Heap Memory Usage
  2. Non Heap Memory Usage
  3. Memory Pool "CMS Old Gen"
  4. Memory Pool "Par Eden Space"
  5. Memory Pool "Par Survivor Space"
  6. Memory Pool "Code Cache"
  7. Memory Pool "CMS Perm Gen"

What is the difference between these terms. Also please provide some information regarding - how to find anomalies in the application behavior by monitoring these parameters.

Answer

Hot Licks picture Hot Licks · Sep 16, 2014

There are essentially three categories of storage in all C-based languages (and most other languages):

  1. Heap
  2. Stack
  3. Static (with several variations)

Heap you're familiar with.

Stack you're also familiar with, but you just don't know it. When you have a method with "local" variables, those variables are allocated in a "invocation frame". The "invocation frame" is allocated when you call the method and deleted when you return from the method, and hence it's most efficiently implemented using a "stack" that grows with call and shrinks with return.

Static is stuff that you don't explicitly allocate and essentially exists from the time program execution begins.

The space required for stack is generally fairly small and is lumped in with "Non Heap Memory" in the categories above.