Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.
Simon Roberts — Performance Page 1
PerformanceSimon RobertsSun Microsystems, [email protected]
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
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
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
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
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
Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.
Simon Roberts — Performance Page 7
Garbage Collector EffectsGC increases reliabilityGC reduces performance
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)
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
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
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
Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.
Simon Roberts — Performance Page 12
Smarter Copying – 1
Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.
Simon Roberts — Performance Page 13
Smarter Copying – 2
Colorado Software Summit: October 26 – 31, 2003 © Copyright 2003, Sun Microsystems, Inc.
Simon Roberts — Performance Page 14
Smarter Copying – 3
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
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
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
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)
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
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
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
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=<???>
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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