Any object on the heap which cannot be reached for a reference from the stack is eligible for garbage.
Stack:
Heap:
Garbage collection:
HEAP contains four different place to store objects:
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-