Sun Microsystems, Inc. simon.roberts@sun - Software Summit

37
Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc. Simon Roberts — Performance Page 1 Performance Simon Roberts Sun Microsystems, Inc. [email protected]

Transcript of Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Page 1: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 1

PerformanceSimon RobertsSun Microsystems, [email protected]

Page 2: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 2

No Easy/Universal AnswersBalance performance/scalability against reliability, usability, maintainability, reusability, portabilityIdeas to keep in mind, questions to ask, not answers

Page 3: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 3

What Is “Performance?”Speed

transaction timetransactions per second

Perceived responseStartup timeReaction timeVisible progress• Time spent updating progress bar

Memory use

Page 4: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 4

Need for ProfilingMeasure, don’t guessMeasure in realistic conditions

Beware of JIT effects

Don’t optimize before you measure

Page 5: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 5

Memory UseObjects

Heap

ClassesPermanent generation

Thread stacksFixed size (typically 1/4 to 1MB, -Xss changes)Method localsNative heap

Native elements

Page 6: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 6

Observing Memory Usehprof, third party tools-verbose:gc / -XPrintGC-XX:+PrintHeapAtGC

Details of memory areas before and after each collection

Page 7: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 7

Garbage Collector EffectsGC increases reliabilityGC reduces performance

Page 8: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 8

Pauses and ThroughputGC takes time/CPU powerGC is lazy

Objects aren’t cleaned immediatelyMemory demand increases

GC is normally single threaded & critical“Stop the world” pauses-Xincgc (train)

Page 9: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 9

Improving GCTypical object life profile:

Many short-lived, some long-livedNot much in between

Clean up young objects more oftenFewer objects to checkHigher “success” ratio

Partition off old/long-livedCalled “generational” garbage collection

Page 10: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 10

Improving GC (2)VM is efficient with this life distributionDon’t pool without a strong reason

Management of pool often negates small benefit Objects that are expensive to create should be pooledConsider returning primitives, or immutable-type references from accessor methods

Page 11: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 11

Simple CopyingCompaction housekeeping is onerousCopying is much more efficient on CPU

But more demanding on memory

Page 12: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 12

Smarter Copying – 1

Page 13: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 13

Smarter Copying – 2

Page 14: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 14

Smarter Copying – 3

Page 15: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 15

Minor CollectionsCopy Eden + 1 survivor to other survivorOverflow directly to tenured space

Generally bad

Young generation guaranteeTenured must have enough for all youngIf not, you must perform full collection

Page 16: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 16

J2SE Platform 1.4.2 Heap DesignTwo (plus one) regions

Young–copying collectorTenured–compacting collectorPermanent

Young consists of:EdenTwo survivor spaces

Page 17: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 17

GC & ParallelismSingle threaded GC impacts system throughput, particularly on multi-CPU hardware

Need for alternate collectorsParallel collection causes fragmentation• Needs more memory

Page 18: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 18

Throughput Collector:Collects young generation with multiple threads-XX:+UseParallelGC-XX:+AggresiveHeap

Larger memory (>= 256 Mbytes)

Page 19: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 19

Concurrent (1.4.2 on)Collects tenured generation concurrently-XX:+UseConcMarkSweepGC

Pauses for initial mark and re-mark-XX:+UseParNewGC

Page 20: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 20

Server Memory ConsiderationsStart with big-enough heap, don’t let server systems resize-Xmx-Xms-XX:MinHeapFreeRatio-XX:MaxHeapFreeRatio

Don’t let VM pageISM/MPSS

Page 21: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 21

Miscellaneous GC IssuesUsing references rather than finalization

Finalization keeps objects around longer Both are non-deterministic

Avoid use of System.gc()Might do nothingMight behave extremelyMight negate optimizations

Page 22: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 22

Class UnloadingCannot be automaticCreate class loader instanceLoad classesAll classes collected when:

All instances of all loaded classes are collectableClass loader instance itself is collectable

-XX:MaxPermSize=<???>

Page 23: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 23

Class LoadingDelayed loading/lazy instantiation

Empty/minimal startup program, especially appletVerifier loads referenced classesMyInterface mi=(MyInterface)(Class.forName(stringVar).newInstance());

Fewer classesClasses take memory in VM to load, more than the code size suggests (~3K)

Page 24: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 24

Class LoadingPerceived performance impactMinimizing class/jar file size

debug info javac -g:noneclass/method/member names (obfuscators)

Jar files, especially over networksBut must load entire jar before startup

WebStart

Page 25: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 25

StringsUse StringBuffer for manipulation

+, concat, replace, substring, trim

Avoids creating intermediate objects

String.intern()Might save memoryAllows comparison with == vs.equalsBut watch the permanent generation size

Page 26: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 26

ReflectionExpensiveHard to compile/optimize (20 times slower on stabilized hot-spotTry to use interfaces instead

Page 27: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 27

Thread IssuesSynchronization

Keep to a correct minimum

DeadlockVector and HashTable

All methods synchronizedDon’t really protect anyway (read, modify, write is potentially non-atomic)

BTW, Deadlock detector

Page 28: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 28

Thread ModelsSolaris libraries & thread models-XX:+UseTLE (1.3), -XX:+UseTLAB (1.4)

For programs using hundreds of threads

Page 29: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 29

Hotspot JITClient & server priorities

Memory use, unloadingStartupCPU investment

Compiled methods aren’t storedLess helpful with

Allocation & GCSynchronizationLow memory systems

Page 30: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 30

JIT BehaviorCompiles whole methods

Called frequentlyLong lived (OSR)Including native wrappersInlines some methods (e.g. trivial accessor/mutator)• At runtime, allowing checks for overriding

Can re-optimize as new classes load

Page 31: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 31

Profiling with JITMicro-profiling is unreliableDead-code, loop invariant, common sub-expressions, constants, null and range check elimination

Page 32: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 32

Watching & Controlling HotSpot-XX:+PrintCompilation

Disable compilation via file.hotspot_compiler

In current directoryContaining, e.g.exclude java/lang/String indexOf

Page 33: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 33

RMI/Distributed SystemsSocketsCORBA, struct-likeRMI

True objects, state and behaviorEmbed different behavior behind same interfaceCaching, read-ahead, granularity control can change with experienceDGC lease renewals

Page 34: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 34

SerializationWrites field names etc.Transitive closure

Watch out for irrelevant stuff

use of transient, private readObject/writeObject

in.defaultReadObject()

Memory leaks if streams not closed

Page 35: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 35

GraphicsClipping, compare:

Cost to calculate clipCost to determine if in/out of clipCost to calculate pixel valueCost to render pixel

Buffering to avoid recalculationPixmap location (X-server vs. X-client) in Java2DBatched updates

Page 36: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 36

MiscellaneousWatch for copies of mutable objects returned from methods

e.g. Dimension object from Graphics.getSize()

Use System.arraycopy()Inner classes accessing variables of enclosing scopes

Page 37: Sun Microsystems, Inc. simon.roberts@sun - Software Summit

Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.

Simon Roberts — Performance Page 37

MiscellaneousDo IO in large chunks

Buffered, NIO

Condition tests are much cheaper than throwCan spoil optimizations