Java;Memory Yury Bubnov zStore
Object o = new Object();
Object o = new Object(); java.lang.OutOfMemoryError:  Java  heap  space
Object o = new Object(); java.lang.OutOfMemoryError:  Java  heap  space
Heap
Heap How many?
Heap How many? Size?
class Structure{ byte b; short i; Object o; }
1 2 4/8 class Structure{ byte b; short i; Object o; }
class Structure{ byte b; short i; Object o; } mark klass 2 1 1 4
class Structure{ byte b; short i; Object o; } 2 8 mark klass 2 1 1 4
class Structure{ byte b; short i; Object o; } mark klass 2 1 5 8
class Structure{ byte b; short i; Object o; } mark klass 2 1 5 8 4 8
Memory address ...01100100100111011001011000
Memory address ...01100100100111011001011000
Memory address ...01100100100111011001011000 32 bit pointer
Memory address ...01100100100111011001011000 32 bit pointer .... to 35 bit
Memory address ...01100100100111011001011000 32 bit pointer .... to 35 bit 2^35≈32GB (25-28GB Heap)
Memory address ...01100100100111011001011000 32 bit pointer .... to 35 bit 2^35≈32GB (25-28GB Heap) IBM J9: Java 6 SR1, by default since Java7 SR4 Hotspot: since Java 6u14, by default since u23
class Structure{ byte b; short i; Object o; } mark klass 1 2 1 4 4
class Structure{ byte b; short i; Object o; } 3 8 mark klass 1 2 1 4 4
klass flags monitor 4 1 3 2 2 =24b klass flags size* monitor 8 1 3 2 2 klass flags monitor 1 3 4 2 2 =40b =24b i386 x64 x64C IBM J9 Java6
What’s the object size? class Structure{ byte b;	short i; Object o;}
What’s the object size? HS 32b 16b HS 64b before u14 32b HS 64b after u14 (u27) 24b J9 32b 24b J9 64b 40b J9 64b compressed 24b class Structure{ byte b;	short i; Object o;}
Mind the gap
Mind the gap byte[][][] b = new byte[10][100][1000];
Mind the gap byte[][][] b = new byte[1000][100][10]; byte[][][] b = new byte[10][100][1000];
Mind the gap byte[][][] b = new byte[1000][100][10]; byte[][][] b = new byte[10][100][1000]; 1.032.344= 1.000.000+ 8.080(r)+20.220(h)+4.044(a)
Mind the gap byte[][][] b = new byte[1000][100][10]; byte[][][] b = new byte[10][100][1000]; 4.832.024= 1.000.000+ 808.000(r)+2.020.020(h)+1.004.004(a) 1.032.344= 1.000.000+ 8.080(r)+20.220(h)+4.044(a)
Mind the gap byte[][][] b = new byte[1000][100][10]; byte[][][] b = new byte[10][100][1000]; 4.832.024= 1.000.000+ 808.000(r)+2.020.020(h)+1.004.004(a) 1.032.344= 1.000.000+ 8.080(r)+20.220(h)+4.044(a) x100
Mind the gap byte[][][] b = new byte[1000][100][10]; byte[][][] b = new byte[10][100][1000]; 4.832.024= 1.000.000+ 808.000(r)+2.020.020(h)+1.004.004(a) 1.032.344= 1.000.000+ 8.080(r)+20.220(h)+4.044(a) x100 x100
Mind the gap byte[][][] b = new byte[1000][100][10]; byte[][][] b = new byte[10][100][1000]; 4.832.024= 1.000.000+ 808.000(r)+2.020.020(h)+1.004.004(a) 1.032.344= 1.000.000+ 8.080(r)+20.220(h)+4.044(a) x100 x100 x250
header 4 1 3 2 6 =24bx64C IBM J9 Java7 for ebay Structure: header 8 =16bx64C byte[8]: *Could be as little as 4 bytes (JavaOne 2011)
Garbage Collection
Garbage Collection 1. Starts if allocation fails or System.gc()* called or condition met
Garbage Collection 1. Starts if allocation fails or System.gc()* called or condition met 2. Stop-of-the-world operation
Garbage Collection 1. Starts if allocation fails or System.gc()* called or condition met 2. Stop-of-the-world operation 3. Mark, Sweep and Compact*
Mark
Mark
Mark PermGen Stack JNI Stack Stack
Mark PermGen Stack JNI X X X X Stack Stack
Mark A B C DE
A B 0 0 0 C D E 0 0 0 0 0 0 0 0 0 0 Mark A B C DE
1 A B 0 0 C D E 0 0 0 0 0 0 0 0 0 0 A Mark A B C DE
1 A B 0 C D E 0 0 0 0 0 0 0 0 0 Mark A B C DE B C 1 1
1 A B 0 C D E 0 0 0 0 0 0 0 0 Mark A B C DE CE 1 1 1
1 A B 0 C D E 0 0 0 0 0 0 0 0 Mark A B C DE E 1 1 1
1 A B 0 C D E 0 0 0 0 0 0 0 0 Mark A B C DE 1 1 1
1 A B C D E 0 0 0 Mark A B C DE 1 1 11 1 1 1 1 1
1 A B C D E 0 0 0 Mark A B C DE 1 1 11 1 1 1 1 1
References
References 1. Strong - always marked ! 2. Soft - not marked if too old or need memory ! 3. Weak - not marked if no strong reference ! 4. Phantom - correct implementation of finalize() method
Mark
MarkSerial
Mark
MarkParallel
Mark PermGen JNI Stack Stack Stack
Mark PermGen JNI Stack Stack Stack
Mark PermGen JNI Stack Stack Stack
Mark PermGen JNI Stack Stack Stack
Mark PermGen JNI Stack Stack Stack
Mark PermGen JNI Stack Stack Stack
Mark PermGen JNI Stack Stack Stack
Mark PermGen JNI Concurrent Stack Stack Stack
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ... Serial
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ... ... 1 0 0 1 1 1 0 0 0 1 1 ... ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ... ... 1 0 0 1 1 1 0 0 0 1 1 ... ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ... Parallel ... 1 0 0 1 1 1 0 0 0 1 1 ... ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ...
Sweep ... 1 0 0 1 1 1 0 0 0 1 1 ... Concurrent
1 1 0 0 1 1 1 0 0 0 1 1 ... Compact 1 1 1 1 1 1 1 0 0 0 0 0 ...
optthuput (default in Java6) parallel mark and compact. contiguous heap ! optavgpause concurrent mark and sweep. contiguous heap ! gencon (default in Java7) copying GC for young area concurrent mark for old area ! balanced (new for Java7) heap split to small regions (G1) Types of Garbage Collection
Heap
Heap (J9) Young/Eden Old/Tenured
Heap (J9) Allocation Old/Tenured Survivor
Heap (J9)
Heap (J9)
Heap (J9)
Heap (J9)
Heap (J9)
Balanced GC
Balanced GC
Balanced GC
Balanced GC 0 Free Free 1 3 Free 0 0 2 0 Free 2 10 11 5 0 2 Free 3 1 1 Free 15 Free 0
Balanced GC
Balanced GC 1. Partial garbage collection (PGC)
Balanced GC 1. Partial garbage collection (PGC) 2. Global mark phase (GMP)
Balanced GC 1. Partial garbage collection (PGC) 2. Global mark phase (GMP) 3. Global garbage collection (GGC)
Partial Garbage Collection 1. All Eden regions 2. Regions from CMP 3. Statistics
Partial Garbage Collection 0 Free 0 1 3 2 Free 1 Free 2 4 Free 1. All Eden regions 2. Regions from CMP 3. Statistics
Arraylets
PermGen
PermGen Bootstrap System
PermGen Bootstrap System Ext
PermGen Bootstrap System Ext Web App
PermGen Bootstrap System Ext Web App Web App
PermGen Bootstrap System Ext Web App Web App Web App
PermGen Bootstrap System Ext Web App Web App Web App Web App
PermGen Bootstrap System Ext Web App Web App Web App Web App Web App
PermGen Bootstrap System Ext Web App Web App Web App Web App Web App java.lang.OutOfMemoryError:  PermGen  space
PermGen Bootstrap System Ext Web App Web App Web App Web App
PermGen Bootstrap System Ext Web App Web App Web App Web App Heap
PermGen Bootstrap System Ext Web App Web App Web App Web App Stack Heap
Stack
Stack
Stack
Stack
Stack
Stack
Stack java.lang.StackOverflowError
Memory
Memory Allocation Old/Tenured Survivor
Memory Allocation Old/Tenured Survivor PermGen
Memory Allocation Old/Tenured Survivor PermGen Stack Stack Stack
Memory Allocation Old/Tenured Survivor PermGen GC Stack Stack Stack
Memory Allocation Old/Tenured Survivor PermGen JNI GC Stack Stack Stack
Memory Allocation Old/Tenured Survivor PermGen JNI GC Net Stack Stack Stack
Memory Allocation Old/Tenured Survivor PermGen JNI GC Net NIO Stack Stack Stack
Memory Allocation Old/Tenured Survivor PermGen JNI GC Net NIO JIT Stack Stack Stack
Memory Allocation Old/Tenured Survivor PermGen JNI GC Net NIO JIT CL Stack Stack Stack
Memory Allocation Old/Tenured Survivor PermGen JNI GC Net NIO JIT CL Stack Stack Stack C++
Paging
Paging 1. Linux/Windows use Virtual memory and “paging” to reference physical ! 2. Pages mapped to physical only when referenced ! 3. OS manages pages
Real size of Java Process?
Real size of Java Process? 1. Architecture: 32 vs 64
Real size of Java Process? 1. Architecture: 32 vs 64 2. JVM vendor: Oracle vs IBM
Real size of Java Process? 1. Architecture: 32 vs 64 2. JVM vendor: Oracle vs IBM 3. JVM version incl. update
Real size of Java Process? 1. Architecture: 32 vs 64 2. JVM vendor: Oracle vs IBM 3. JVM version incl. update 4. JVM settings
Real size of Java Process? 1. Architecture: 32 vs 64 2. JVM vendor: Oracle vs IBM 3. JVM version incl. update 5. OS (above applies) 4. JVM settings
Real size of Java Process? 1. Architecture: 32 vs 64 2. JVM vendor: Oracle vs IBM 3. JVM version incl. update 5. OS (above applies) + Run under real load for long time 4. JVM settings
?
Heap Young/Eden Old/Tenured
Heap (HS) Young/Eden Old/Tenured Survivor 1 Survivor 2
Heap (HS)
Heap (HS)
Heap (HS) TLAB TLH
Heap (HS) TLAB TLH
Heap (HS) TLAB TLH
Heap (HS)
Heap (HS)
Heap (HS)
Heap (HS) X X X
Heap (HS)
Heap (HS)
Heap (HS)
Heap (HS) X XX XX X
Heap (HS)
Heap (HS)
Heap (HS)
Heap (HS) X XX XX X
Heap (HS)
Heap (J9) Allocation Old/Tenured Survivor
Garbage Collection Serial Parallel Concurrent Which one to choose?

Java memory presentation IBM 7