JVM and Memory

jenkovjava-memory-model


Heap, Metaspace, Stack

Java Memory Model

Stack Memory

Stack Memory

Difference between Stack and Heap?
  • Stack contains methods, and its primitive values as well as object references.
  • Stack is LIFO.

  • Objects are stored in Heap, Stack contains references to them.
  • Heap is used for all the application whereas Stack is used by one Thread.
  • Objects in Heap are globally accessible whereas Stack can’t be accessed by other Threads.

  • When the memory is full, JVM throws for each case:
    • Stack: java.lang.StackOverFlowError
    • Heap: java.lang.OutOfMemoryError
Heap options: -Xms, -Xmx

Startup size and Maximum size of Heap memory.

Stack option: -Xss

Stack memory size.

String Pool

Whats a Java String Pool

Thread Dumps

  • jstack
  • kill -3
# jstack (jdk)
$ jstack -l  <pid> > <file-path>

# kill -3
$ kill -3 <pid>
  • JVisualVM
  • Java Mission Control (JMC)

How to take Thread dumps