1.14M
Категория: ПрограммированиеПрограммирование

Java Basics: Memory Management

1.

Java basics:
Memory management

2.

Questions
Task*
How much memory is required after boolean
field is added into object?
When object is removed from HEAP?
When OOM is thrown?
JVM tuning

3.

Sizes
Memory types
Objects in heap
Heap configuration
GC goals
Serial
Parallel
Concurrent Mark Sweep (CMS)
Garbage First (G1)

4.

Sizes
• Primitives
• boolean
• byte
• char
• short
• int
• float
• long
• double
1 or 4
1
2
2
4
4
8
8
bytes
bytes
bytes
bytes
bytes
bytes
bytes
bytes

5.

Sizes (prod.)
• Reference
• Soft
• Weak
• Phantom
• Strong

6.

Sizes (prod.)
• Reference
32 or 64 bits = 4 or 8 bytes
-XX:+UseCompressedOops
4 bytes
-XX:ObjectAlignmentInBytes=alignment
power of 2
8 < alignment < 256
232 objects
* alignment = Max heap size

7.

Sizes (prod.)
• Object
• Header (8, 16, +4 bytes)
• Primitives
• Other object references
• Alignment
dependent on VM implementation
And other factors (e.g. Escape analysis)

8.

9.

Memory types in Java
Java Memory Management
(not JMM from JSR-133)
HEAP
HEAP
Young generation
Eden
space
Survivor1
Survivor2
NON-HEAP
NON-HEAP
Old
Tenured
Permanent
Metaspace
generation
Native
Thread
Stack,
etc.
Java 1.8
GC processing

10.

Objects in heap
Thread
Stack
Object link
Object link
int
long
Object link
…
HEAP
Object
int
double
Object link
…
Object
double
double
Object
float
Object link
Promptness
Object
int
Object link
…
Object
Object link
…
Object
Object link
…
…
Object
Object link
…

11.

Promptness
Thread
Stack
Object link
Object link
int
…
Full GCGC
Minor
is working
is working
HEAP
Young
Old
Object
int
double
Object link
Survivor objects
…
…
Object
float
Object link
Object
Object link
…
…
Only after major (or full) gc
old objects are removed

12.

Heap configuration
HEAP
-XX:MaxHeapSize ~ -Xmx
- Maxheap size
-Xms
- Initial heap size
-XX:MinHeapFreeRatio=40
- minimum allowed heap free size (in percent)
-XX:MaxHeapFreeRatio=75
- maximum allowed heap free size (in percent)

13.

GC goals
• Maximum Pause Time Goal
-XX:MaxGCPauseMillis=<nnn>
• Throughput Goal
-XX:GCTimeRatio=<nnn>
• Footprint Goal

14.

GC clearing types
Young generation
Old generation
Minor GC
Major GC
Full GC
-XX:+UseGCOverheadLimit – throw OOM if GC spent more that 98% execution time
-XX:-UseGCOverheadLimit

15.

Serial GC
-XX:+UseSerialGC
Young
Eden
Old
S0
S1
Tenured

16.

Serial GC (example minor GC)
Eden
To
From
S0
S1
Old

17.

Serial GC (specifics)
Eden
To
From
S0
S1
Old

18.

Serial GC (specifics
prod.)
Eden
To
From
S0
S1
Old

19.

Serial GC (specifics
prod.)
Eden
To
From
S0
S1
Old

20.

Serial GC (example full GC)
Eden
Minor GC
GC
Major
To
S0
From
Mark
S1
Sweep
Compact
Sometimes major GC performs first
Old

21.

Serial GC configuration
Generation sizes
1/3 Heap size
2/3 Heap size
Young
Old
Eden
S0
8/10 Young size
S1
Tenured
1/10 Young size
-XX:NewRatio=2
OLD size is 2 YOUNG sizes (1/3 takes young and 2/3 takes old)
-XX:SurvivorRatio=8
Eden size is 8 survivor sizes
( 8/10 takes Eden and 1/10 takes survivors)
-XX:NewSize
-XX:MaxNewSize
-Xmn
Initial young size
Max young size
XX:NewSize = XX:MaxNewSize

22.

Author
Suslov Daniil
English     Русский Правила