· “promotions” of longer lived objects during young gcs new allocations. 7 ... • large...

37
1

Upload: others

Post on 04-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

1

Page 2:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

<Insert Picture Here>

How to Tune and Write Low-Latency Applications on the Java Virtual MachineSimon Ritter - Java Technology Evangelist

Page 3:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

3

<Insert Picture Here>

Program

• Vocabulary• What Is Low-Latency Java• JVM Features• What To Think About• Application Analysis

Page 4:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

4

Vocabulary

• Nursery == Young Space == Eden• Nursery Collection == Young Collection == Minor GC• Promotion

– Move objects to Old Space•• Old Space == Old Generation• Old Collection == Full Collection• Compaction

– Move objects together to remove fragmentation

Page 5:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

5

JVM Heap Layout

The Heap

Permanent Generation : for VM metadata

Old Space / Old Generation: for older objects

Nursery / Young Generation: for new objects

Page 6:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

6

Permanent Generation : for VM metadata

Old Space / Old Generation: for older objects

Nursery / Young Generation: for new objects

JVM Heap Layout

The Heap“Promotions” of LongerLived ObjectsDuring Young GCs

NewAllocations

Page 7:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

7

Permanent Generation : for VM metadata

Old Generation: for older objects

Survivor

HotSpot JVM Heap – In More Detail…

The Heap

Eden Survivor

Page 8:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

8

Permanent Generation : for VM metadata

Old Generation: for older objects

Survivor

HotSpot JVM Heap – In More Detail…

The Heap

NewAllocations

Eden Survivor

“Promotions” of LongerLived ObjectsDuring Young GCs

Retention ofYoung ObjectsDuring Young GCs

Page 9:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

9

Low-Latency Java

Page 10:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

10

What is Low-Latency Java

• Soft Real-Time– High throughput with low latency– No hard response time guarantees– No catastrophe happens at response time failures

• But several nines response time guarantees

• Normal Java Code– No special APIs– Code tuning with focus on latency

Page 11:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

11

What is Low-Latency Java

• Run standard Java code with great response times

0

15

30

45

60

75

90

105

120

0 2000 4000 6000 8000 10000 12000 14000 16000 18000

Low-Latency CollectorDeterministic GC pauses, allowing guarantees of SLAs.

0

15

30

45

60

75

90

105

120

0 2000 4000 6000 8000 10000 12000 14000 16000 18000

Normal Garbage CollectorGC spikes and occasional SLA breach occurs.

Page 12:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

12

What is Low-Latency Java

• Response Time Requirements– Typical latency requirement around 5 – 50 ms– Max latency = transaction time + max pause time

• Typical Applications– Financial

• Automatic trading• Matching server

– Telecom• SIP applications

– Event processing• RFID scanning

Page 13:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

13

JVM Features

Page 14:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

14

What Do the JVMs Offer

• GC Implementations– Generational Concurrent Collectors

• CMS – HotSpot• Gencon – JRockit

– Regional Heap Collector• G1 – HotSpot (In development, EA available)

– Optimized Concurrent Collector• Deterministic GC – JRockit Real-Time

Page 15:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

15

Generational Concurrent Collectors

• Use Case– Normal heap sizes– Reasonable amount of live data

• Nursery Collector– Stop the world– Parallel

• Old Collection– Mostly concurrent Mark and Sweep

• Some short parallel pauses– Partial compaction

Page 16:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

16

Generational Concurrent Collectors

• Tuning– Nursery sizing

• Large enough to reduce frequency• Small enough to get adequate pauses

– Compaction tuning• How large part of the heap to compact each GC

– Large enough to avoid fragmentation– Small enough to get adequate pauses

Page 17:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

17

Generational Concurrent Collectors

• Tuning Cont.– Concurrent GC threshold

• Start concurrent GC early enough• Complete GC before memory become scarce• Tuned automatically

Page 18:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

18

G1 – Regional Heap Collector

• Use Case– Large heaps– Large amount of live data

• Regional Nursery Collector– Stop the world– Parallel

• Regional Old Collector– Evacuation of live data– Remembered sets

• No mark phase

Page 19:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

19

G1 – Partial Heap Collector

• Tuning– Pause target

• Lower pause target requires a more GC aware code• -XX:MaxGCPauseMillis=X

– Nursery sizing• Implicit by pause target tuning

– Evacuation tuning• Implicit by pause target tuning

Page 20:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

20

Deterministic GC – Optimized Concurrent Collector

• Use Case– Normal heap sizes– Live data around 1/2 of the heap

• Old Collection– Parallel mostly concurrent Mark and Sweep– Heavily optimized pauses– Abortable compaction

Page 21:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

21

Deterministic GC – Optimized Concurrent Collector

• Tuning– Pause target

• Lower pause target requires a more GC aware code• -XXpausetarget=Xms

– Heap sizing• Large enough to hold live data and free heap• Small enough to get adequate pauses

– Compaction tuning• Implicit by pause target tuning• Ensure enough pause time to keep heap

unfragmented

Page 22:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

22

What To Think About

Page 23:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

23

What To Think About – Throughput

• Potential Performance Impact– Bookkeeping of objects

• Tracking new objects during a concurrent collection• Updating remembered sets

• Don’t saturate the CPU– The concurrent GC threads shares the CPU

Page 24:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

24

What To Think About – Allocation

• Allocation Rate– More allocation => More GCs => More pauses– Need enough time to finish concurrent phases

• Complete GC before memory becomes scarce– Can still handle high allocation rates

• Hundreds of MB per second• Large Objects/Arrays

– Requires free consecutive memory– Increases the requirement to do compaction

Page 25:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

25

What To Think About – Allocation

• Avoid System.gc()– Let the memory system handle GCs

• Semi-Long Lived Objects– Increases YC times due to copying during promotion– Increases fragmentation in Old Space

• Mix of short and long lived data– Avoid storing data from each transaction– Make sure tenuring thresholds is configured properly

Page 26:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

26

What To Think About – Data Structures

• Understand Your Data Structures– Resizing Data Structures

• Size your HashMap correctly– Rehashing when increasing size takes time– Hard to detect

• StringBuilder/Buffer expands and copying– We do optimizations to try to avoid this

– Optimal Use Case• ArrayList add/remove at beginning of list causes

copying

Page 27:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

27

What To Think About – Data Structures

• GC Friendly Data Structures– Mark phase iterates over live data– Some data structures are hard to mark in parallel

Page 28:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

28

What To Think About – Reference Objects

• Use Reference Objects Sensibly– Soft, weak and phantom references require special

processing during GC– Finalizers should be avoided

• Will keep objects alive for one extra GC cycle• Detrimental to pause time goals

Page 29:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

29

What To Think About – Profiling

• Externally Measure End-to-End Response Time– Internal measurements affected in the same way as the

application• GC Analysis

– Understand your object allocation and survival patterns– GC pause times

• The name of the pause is often a good hint• Lock Profiling

– #1 issue for scaling problems and unexplained outliers

Page 30:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

30

What To Think About – Finally

• Don’t Overdo It– The JVM can handle a lot– Analyze to detect your current bottleneck

• Optimize and tune

Page 31:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

31

Application Analysis

Page 32:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

32

Know What to Optimize

• Analyze Your Application– Garbage collections

• Pauses• Heap usage

– Hot Methods– Lock contention– IO events

Page 33:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

33

HotSpot VisualVM

Page 34:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

34

JRockit Mission Control

Page 35:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

35

Other Tools

• Java Tools– IBM Health Center– JProfiler

• Low Level Tools – Hardware Profiling– VTune– CodeAnalyst– Oracle Solaris Studio– oProfile

Page 36:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

36

JavaOne and Oracle Develop Russia 2011

April 9-10, 2011

Page 37:  · “Promotions” of Longer Lived Objects During Young GCs New Allocations. 7 ... • Large enough to reduce frequency • Small enough to get adequate

37