Java Memory Management Tricks
-
Upload
globallogic-ukraine -
Category
Software
-
view
907 -
download
3
Transcript of Java Memory Management Tricks
©2015 GlobalLogic Inc.
2
Java Memory Management Tricks
By Andrii Antilikatorov
3
4
5
Agenda
6
When we start to think about memory-related things?
7
Battle of Garbage Collectors
Serial Parallel CMS G1
8
Battle of Garbage Collectors
Do I really need to think about this?
9
Battle of Garbage Collectors
10
Serial GC
Runs great on low-end computers
Long pauses during garbage collections
Recommended for single-core CPU and heaps under 100Mb
…old, but not obsolete! ©
11
Serial GC
12
Parallel GC
Supports automatic self-tuning for optimal performance
Memory fragmentation
Perfectly consumes multi-core CPU power
13
Parallel GC
Supports automatic self-tuning for optimal performance
Memory fragmentation
Perfectly consumes multi-core CPU power
14
Concurrent Mark-Sweep GC
Works as Parallel GC in case of minor GC
Minimizes pauses, but sacrifices CPU and throughput
Consumes more memory (+20%)
Long pauses in case of concurrency mode failures
Works great with big data with long-living objects
15
Concurrent Mark-Sweep GC
Works as Parallel GC in case of minor GC
16
Garbage First (G1) GC
JEP 248: Make G1 the Default Garbage Collector on 32- and 64-bit server configurations starting from Java 9Designed for systems where limiting latency is more important than maximizing throughput
More accurate pause prediction
No memory fragmentation
High CPU utilization
17
Garbage First (G1) GC
JEP 248: Make G1 the Default Garbage Collector on 32- and 64-bit server configurations starting from Java 9Designed for systems where limiting latency is more important than maximizing throughput
More accurate pause prediction
No memory fragmentation
High CPU utilization
Number of GC threads and Marking threads: ParallelGCThreads=?, ConcGCThreads=?
Heap region size: G1HeapRegionSize=?
Pause minimization: MaxGCPauseMillis=?
Heap memory allocation threshold: InitiatingHeapOccupancyPercent=?
Options for real geeks: UnlockExperimentalVMOptions, AggressiveOpts
18
Memory Access
Heap
Direct Memory Access (Off-Heap)
Non-Direct ByteBuffer
Direct ByteBuffer
19
Memory Access
Heap
Direct Memory Access (Off-Heap)
Non-Direct ByteBuffer
X Axis – No Of ReadingY Axis – Op/Second in Millions
Direct ByteBuffer
20
Memory Access
Heap
Direct Memory Access (Off-Heap)
Non-Direct ByteBuffer
X Axis – No Of ReadingY Axis – Op/Second in Millions
Direct ByteBuffer
21
Memory Access
Heap
Direct Memory Access (Off-Heap)
Non-Direct ByteBuffer
X Axis – No Of ReadingY Axis – Op/Second in Millions
Direct ByteBuffer
22
Memory Access
Heap
Direct Memory Access (Off-Heap)
Non-Direct ByteBuffer
X Axis – No Of ReadingY Axis – Op/Second in Millions
Direct ByteBuffer
23
Direct Memory Alignment in Java
Type alignment Page size alignment
Cache line alignment
Memory Alignment
24
Direct Memory Alignment in Java
Type alignment Page size alignment
Cache line alignment
Memory Alignment“= new SomeObj()” always type-aligned.
“Unsafe.allocateMemory” always 8-bytes aligned.
“ByteBuffer.allocateDirect” …
Memory is 0-ed out automatically
Memory is page-aligned in JDK ≤ 1.6 and ‘8-bytes’ aligned in JDK ≥ 1.7
Memory is is freed as part of the ByteBuffer object GC
25
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than unaligned access.
Memory access that spans 2 cache lines has far worse performance than aligned mid-cache line access.
Cache line access performance changes based on cache line location.
26
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than unaligned access.
Memory access that spans 2 cache lines has far worse performance than aligned mid-cache line access.
Cache line access performance changes based on cache line location.
27
Type-aligned vs unaligned Access Test
Number of pages
Alignment
Relative cost
28
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than unaligned access.
Memory access that spans 2 cache lines has far worse performance than aligned mid-cache line access.
Cache line access performance changes based on cache line location.
29
Cross-line cache access
Number of pages
Offset
Relative cost
30
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than unaligned access.
Memory access that spans 2 cache lines has far worse performance than aligned mid-cache line access.
Cache line access performance changes based on cache line location.
31
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than unaligned access.
Memory access that spans 2 cache lines has far worse performance than aligned mid-cache line access.
Cache line access performance changes based on cache line location.
32
Cost of Access Based on Cache Line LocationOffset
Number of pagesRelative cost
33
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than unaligned access.
Memory access that spans 2 cache lines has far worse performance than aligned mid-cache line access.
Cache line access performance changes based on cache line location.
34
sun.misc.Unsafe
Direct Object Manipulations
Class Structure Manipulations
Arrays Manipulations
Synchronization Primitives
Low-level Memory Access
Low-level Memory Info
sun.misc.Unsafe
35
sun.misc.Unsafe – Obtain Unobtainable
Unsafe unsafe = Unsafe.getUnsafe();
36
sun.misc.Unsafe – Obtain Unobtainable
Unsafe unsafe = Unsafe.getUnsafe();
37
sun.misc.Unsafe – Obtain Unobtainable
38
Create an Instance Without Calling a Constructor
Need a “hack” to create new instance of Singleton
Need to avoid execution of heavy constructor logic
Custom serialization/deserialization.
39
Create an Instance Without Calling a Constructor
40
Create an Instance Without Calling a Constructor
41
Create an Instance Without Calling a Constructor
50
50
0
42
Measure Shallow Size of an Object
Sizes of data structures are fixed for 32/64bit platforms
According to ‘sizeof’ is not required because…
Java VM’s GC does complete memory management
…and you still can ‘measure’ the object by serializing to byte stream and looking at its length…
43
Measure Shallow Size of an ObjectLooking through all
non-static fields
Calculating offset of the last field
Taking into account memory alignment
44
Measure Shallow Size of an Object
Getting data from class struct
Converting signed integer to longTaking into account
header size
45
Low-Level Memory Allocation
…for those who need extremely large arrays
46
Low-Level Memory AllocationElement size
Direct memory allocation
Get/Put elements to array
47
How About a Kind of Multiple Inheritance?
No multiple inheritance
Really?
48
How About a Kind of Multiple Inheritance?
49
How About a Kind of Multiple Inheritance?
50
©2015 GlobalLogic Inc.