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;
}
markmarkmark klass
2 1 1 4
class Structure{
	 byte b;
	 short i;
	 Object o;
}
2
8
markmarkmark klass
2 1 1 4
class Structure{
	 byte b;
	 short i;
	 Object o;
}
markmarkmark
klassklassklass
2 1 5
888
class Structure{
	 byte b;
	 short i;
	 Object o;
}
markmarkmark
klassklassklass
2 1 5
888
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- not enabled by default
Hotspot: since Java 6u14, by default since u23
class Structure{
	 byte b;
	 short i;
	 Object o;
}
markmarkmarkmark
klass 1 2 1
4 444
class Structure{
	 byte b;
	 short i;
	 Object o;
}
3
8
markmarkmarkmark
klass 1 2 1
4 444
klassklass flagsflags
monitormonitor 44
1 3 2 2
=24b
klassklassklassklass
flagsflags size*size*
monitormonitormonitormonitor
8888
1 3 2 2
klass flagsflagsflags
monitor 1 33
4 22 2
=40b
=24b
i386
x64
x64C
IBM J9
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
Heap
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)
Young/Eden
Old/Tenured
Heap (J9)
Allocation
Old/Tenured
Survivor
Heap (J9)
Heap (J9)
Heap (J9)
Heap (J9)
Heap (J9)
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 ...
Garbage Collection
Serial Parallel Concurrent
Which one to choose?
Heap (J9)
Allocation
Old/Tenured
Survivor
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
?

Java memory presentation