JVM

Memory Parts

  1. Stack (local variables, method calls, arguments, reference variables, intermediate computations, etc)
  2. PermGen (Class and method objects; internal representation of the Java classes)
    1. Method Area: is part of perm gen. It stores per-class structures( runtime constants and static fields), code for methods and constructors.
  3. Heap (object instances, instance variables, etc)
    1. Eden (Young Generation) - objects are initially created here. The minor GC (more frequent with smaller pauses) happens here.
    2. Survivor Space (Young Generation) - objects survived GC of Eden space are moved here.
    3. Tenured Generation (Old Generation) - objects that existed in survivor space for some time are moved here such as singletons

Facts about Heap

  • Is shared among all threads.
  • Class instances (ojbect) and arrays are stored in heap
  • Is created on virtual machine start-up.
  • Heap is GCed
  • The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary.
  • The memory for the heap does not need to be contiguous.
  • Min/max/initial size of the heap and frequency of GC can be set using parameters
  • Heap grows down (from low memory addresses to higher memory addresses) - stack is reverse.
  • Whenever we use "new" we will have a dynamic variable and this goes into the heap.
mem.png

When the objects are created, they reside inside the Eden. When the Garbage collector(GC) runs, if the object is dead (means they are no active references) it is flushed out otherwise they are moved toS1(Survivor Space 1) or S2. This is called a GC cycle. Internal GM algorithm decides the frequency of the GC cycle. Eden + S1 + S2 section of Heap memory is called as Young generation. Objects that survives a fixed number of GC cycles are moved in to Old Gen space. Most number of java objects die as infant and never reach Old Gen. This typically includes local variables which are flushed after the methods are executed.

Facts about Method Area

  • Is shared among all threads.
  • Some JVM implementations may choose not to either garbage collect or compact it.
  • Stores class info, constant strings
  • JVM will generate an internal class representation of the bytecode and stores it here.

Facts about Stack

  • Stack values only exist within the scope of the method they are created in. Once it returns, they are discarded. Objects are created on the heap and only references (which in turn are primitives) are passed around on the stack.
  • Method parameters are stored in stack
  • stack grows up (from high memory addresses to low memory addresses)
  • Method local variables are stored in stack
  • When a method returns its stack memory is claimed back (popped off stack)
  • Local object references are stored in stack

Real-time VM

RTVMs have sections other than heap that are not garbage collected such as immortal memory. For more info see: http://java.sun.com/developer/technicalArticles/Programming/rt_pt1/

How much memory

This is a good formula to calculate how much memory the application might need:

(-Xmx) + (-XX:MaxPermSize) + numberofthreads * (-Xss) + Other mem
where Xmx is max heap and Xss is stack size.

The other mem part depends on how much native code is used like NIO, socket buffers, JNI etc.

Assume we have -Xmx1024m -XX:MaxPermSize=256m -Xss512k and 100 threads: 1024m + 256m + 100*512k + x. This is roughly 1.5 * max heap to be the minimum amont of RAM we need.

HotSpot

HotSpot is the Oracle's implementation of the JVM specification so it's a JVM implementation. It actually features two virtual machines, one called Client and the other Server. The Client version is tuned for quick loading. It makes use of interpretation. The Server version loads more slowly, putting more effort into producing highly optimized JIT compilations.

More Info

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License