Java Memory

Any object on the heap which cannot be reached for a reference from the stack is eligible for garbage.

Stack:

Heap:

Garbage collection:

  • Mark and sweep, generational garbage collection.
  • G1 generational garbage collection (in this case the heap is split into regions).

HEAP contains four different place to store objects:

  • Young/Eden: small (size is configurable), every new object get moved here. If it get full, GC will run.
  • Young/S0: smaller than Eden(size is configurable), after survive one GC the object get into here
  • Young/S1: smaller than S0(size is configurable), after survive another five (?) GC the object get into here
  • Old (size is configurable): after survive another five (?) GC the object get into here

There are two type of collect process: minor, full;

In jvisualvm with the visualGC plugin the garbage generations get visible.

Metaspace:

static variables are stored in the metaspace. (not in the stack)

static Objects are stored in the HEAP, the references are in the metaspace.

Before JAVA7= PermGen did something similar, not relevant anymore. StringPool was sored this place before JAVA7.

 

String intern:

String one = "67";
int two = 67;
two = i.toString(); 
if (one.equals(two)) {};      //will be true
if (one == two) {};           //wont be equal (after a while JIT compiler could noticed the equality)
two = i.toString().intern(); 
if (one == two) {};           //will be equal

 

Show free memory:

Runtime.getRuntime().freeMemory();

 

JvisualVM:

For using with openjdk you need to download from visualvm.github. (oracle jdk contains it). For proper working the hfperfdata folder must be writeable.

jvisualvm

 

Memory analyzer (MAT):

It can analyze heap dump downloaded by jvisualvm-