Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
©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
he...
11
Serial GC
Min & Max heap size - Xms/Xmx
Free space ratio in each generation:
MinHeapFreeRatio=?, MaxHeapFreeRatio=?
You...
12
Parallel GC
Supports automatic self-tuning for optimal performance
Memory fragmentation
Perfectly consumes multi-core C...
13
Parallel GC
Supports automatic self-tuning for optimal performance
Memory fragmentation
Perfectly consumes multi-core C...
14
Concurrent Mark-Sweep GC
Works as Parallel GC in case of minor GC
Minimizes pauses, but sacrifices CPU and throughput
C...
15
Concurrent Mark-Sweep GC
Works as Parallel GC in case of minor GC
All options of Serial and Parallel GC are applicable
...
16
Garbage First (G1) GC
JEP 248: Make G1 the Default Garbage Collector on 32- and
64-bit server configurations starting f...
17
Garbage First (G1) GC
JEP 248: Make G1 the Default Garbage Collector on 32- and
64-bit server configurations starting f...
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 Reading
Y Axis – Op/Second in M...
20
Memory Access
Heap
Direct Memory Access
(Off-Heap)
Non-Direct ByteBuffer
X Axis – No Of Reading
Y Axis – Op/Second in M...
21
Memory Access
Heap
Direct Memory Access
(Off-Heap)
Non-Direct ByteBuffer
X Axis – No Of Reading
Y Axis – Op/Second in M...
22
Memory Access
Heap
Direct Memory Access
(Off-Heap)
Non-Direct ByteBuffer
X Axis – No Of Reading
Y Axis – Op/Second in M...
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(...
25
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than
unaligned access.
M...
26
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than
unaligned access.
M...
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.
M...
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.
M...
31
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than
unaligned access.
M...
32
Cost of Access Based on Cache Line Location
Offset
Number of pagesRelative cost
33
Comparing Aligned/Unaligned Access Performance
Type aligned access provides better performance than
unaligned access.
M...
34
sun.misc.Unsafe
Direct Object
Manipulations
Class Structure
Manipulations
Arrays
Manipulations
Synchronization
Primitiv...
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 execut...
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 no...
43
Measure Shallow Size of an Object
Looking through all
non-static fields
Calculating offset of
the last field
Taking int...
44
Measure Shallow Size of an Object
Getting data from
class struct
Converting signed
integer to long
Taking into account
...
45
Low-Level Memory Allocation
…for those who need extremely large arrays
46
Low-Level Memory Allocation
Element 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.
Nächste SlideShare
Wird geladen in …5
×

Java Memory Management Tricks

3.320 Aufrufe

Veröffentlicht am

This presentation is about Java performance and the most effective ways to work with Java memory, including memory saving techniques and overcoming of memory barriers. Moreover, it contains debunking of the most popular myths on speed boosting.

This presentation by Andrii Antilikatorov (Consultant, GlobalLogic) was delivered at GlobalLogic Java Conference #2 in Krakow on April 23, 2016.

Veröffentlicht in: Software
  • Als Erste(r) kommentieren

Java Memory Management Tricks

  1. 1. ©2015 GlobalLogic Inc.
  2. 2. 2 Java Memory Management Tricks By Andrii Antilikatorov
  3. 3. 3
  4. 4. 4
  5. 5. 5 Agenda
  6. 6. 6 When we start to think about memory-related things?
  7. 7. 7 Battle of Garbage Collectors Serial Parallel CMS G1
  8. 8. 8 Battle of Garbage Collectors Do I really need to think about this?
  9. 9. 9 Battle of Garbage Collectors
  10. 10. 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. 11. 11 Serial GC Min & Max heap size - Xms/Xmx Free space ratio in each generation: MinHeapFreeRatio=?, MaxHeapFreeRatio=? Young/Old generations ratio: NewRatio=? Young generation min/max size: NewSize=?, MaxNewSize=? Eden/Survivor ratio: SurvivorRatio=? Set GC activity limit: UseGCOverheadLimit
  12. 12. 12 Parallel GC Supports automatic self-tuning for optimal performance Memory fragmentation Perfectly consumes multi-core CPU power
  13. 13. 13 Parallel GC Supports automatic self-tuning for optimal performance Memory fragmentation Perfectly consumes multi-core CPU power All options of Serial GC are applicable Number of GC threads: ParallelGCThreads=? Disable compaction in Old Gen: UseParallelOldGC Performance options: MaxGCPauseMillis=?, GCTimeRatio=? Generation size increment: YoungGenerationSizeIncrement, TenuredGenerationSizeIncrement Generation size decrease: AdaptiveSizeDecrementScaleFactor
  14. 14. 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. 15. 15 Concurrent Mark-Sweep GC Works as Parallel GC in case of minor GC All options of Serial and Parallel GC are applicable Major GC threshold: CMSInitiatingOccupancyFraction=?
  16. 16. 16 Garbage First (G1) GC JEP 248: Make G1 the Default Garbage Collector on 32- and 64-bit server configurations starting from Java 9 Designed for systems where limiting latency is more important than maximizing throughput More accurate pause prediction No memory fragmentation High CPU utilization
  17. 17. 17 Garbage First (G1) GC JEP 248: Make G1 the Default Garbage Collector on 32- and 64-bit server configurations starting from Java 9 Designed 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. 18. 18 Memory Access Heap Direct Memory Access (Off-Heap) Non-Direct ByteBuffer Direct ByteBuffer
  19. 19. 19 Memory Access Heap Direct Memory Access (Off-Heap) Non-Direct ByteBuffer X Axis – No Of Reading Y Axis – Op/Second in Millions Direct ByteBuffer
  20. 20. 20 Memory Access Heap Direct Memory Access (Off-Heap) Non-Direct ByteBuffer X Axis – No Of Reading Y Axis – Op/Second in Millions Direct ByteBuffer
  21. 21. 21 Memory Access Heap Direct Memory Access (Off-Heap) Non-Direct ByteBuffer X Axis – No Of Reading Y Axis – Op/Second in Millions Direct ByteBuffer
  22. 22. 22 Memory Access Heap Direct Memory Access (Off-Heap) Non-Direct ByteBuffer X Axis – No Of Reading Y Axis – Op/Second in Millions Direct ByteBuffer
  23. 23. 23 Direct Memory Alignment in Java Type alignment Page size alignment Cache line alignment Memory Alignment
  24. 24. 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. 25. 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. 26. 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. 27. 27 Type-aligned vs unaligned Access Test Number of pages Alignment Relative cost
  28. 28. 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. 29. 29 Cross-line cache access Number of pages Offset Relative cost
  30. 30. 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. 31. 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. 32. 32 Cost of Access Based on Cache Line Location Offset Number of pagesRelative cost
  33. 33. 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. 34. 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. 35. 35 sun.misc.Unsafe – Obtain Unobtainable Unsafe unsafe = Unsafe.getUnsafe();
  36. 36. 36 sun.misc.Unsafe – Obtain Unobtainable Unsafe unsafe = Unsafe.getUnsafe();
  37. 37. 37 sun.misc.Unsafe – Obtain Unobtainable
  38. 38. 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. 39. 39 Create an Instance Without Calling a Constructor
  40. 40. 40 Create an Instance Without Calling a Constructor
  41. 41. 41 Create an Instance Without Calling a Constructor 50 50 0
  42. 42. 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. 43. 43 Measure Shallow Size of an Object Looking through all non-static fields Calculating offset of the last field Taking into account memory alignment
  44. 44. 44 Measure Shallow Size of an Object Getting data from class struct Converting signed integer to long Taking into account header size
  45. 45. 45 Low-Level Memory Allocation …for those who need extremely large arrays
  46. 46. 46 Low-Level Memory Allocation Element size Direct memory allocation Get/Put elements to array
  47. 47. 47 How About a Kind of Multiple Inheritance? No multiple inheritance Really?
  48. 48. 48 How About a Kind of Multiple Inheritance?
  49. 49. 49 How About a Kind of Multiple Inheritance?
  50. 50. 50
  51. 51. ©2015 GlobalLogic Inc.

×