26
Performance Tuning Performance Tuning Java Applications Java Applications Dave Scruggs Dave Scruggs Systems Engineer Systems Engineer

Performance Tuning Java Applications

Embed Size (px)

DESCRIPTION

Presenter(s): Steve HudsonDate: Sep 11, 2003Location: Chattanooga Java Users Group Site: www.cjug.netLinkedIn: www.linkedin.com/groups?gid=1167347Presentation------------While monitoring the infrastructure is fundamental, understanding what goes on inside your critical applications and how they interact is the real pulse of your system health. Responsible Enterprise Java Application performance management doesn't just happen. It requires purposeful implementation of a performance management strategy that is positioned to handle future growth while cost effectively meeting today's needs. With Wily's methodology for managing high performance web applications, you will learn the full performance management lifecycle and the process to help deliver high performance web applications in the production environment. You will learn that performance problems can be addressed beyond a reactionary point of view as well as preventing any future performance failures all together. This presentation will cover the following topics:Challenges in Managing Web/J2EE Applications A Proven Methodology for Successful Applications The Benefits of Performance Management Introscope Platform Technology Overview & Demo Biography---------Steve Hudson is a member of the Wily Technology Services team and has also contributed to several Introscope development projects. Steve works extensively with enterprise customers and partners to help customers carry out a successful performance management solution. His expertise includes performance benchmarking, performance best practices, and Java related-technology.

Citation preview

Page 1: Performance Tuning Java Applications

Performance Tuning Performance Tuning Java ApplicationsJava Applications

Dave ScruggsDave ScruggsSystems EngineerSystems Engineer

Page 2: Performance Tuning Java Applications

OutlineOutlineOutline

1.1. General overviewGeneral overview2.2. Suggested toolkit and proceduresSuggested toolkit and procedures

Demo 1Demo 1——Temporary ObjectsTemporary Objects

3.3. Compilers and the JVMCompilers and the JVMDemo 2Demo 2——Memory LeaksMemory Leaks

4.4. Networking and I/ONetworking and I/ODemo 3Demo 3——nonnon--blocking I/Oblocking I/O

5.5. ThreadingThreadingDemo 4Demo 4——CPU BottlenecksCPU Bottlenecks

Page 3: Performance Tuning Java Applications

Know your targetKnow your targetKnow your target

What kind of application are you writing?What kind of application are you writing?Embedded, Workstation, Workgroup, Intranet, or Enterprise?Embedded, Workstation, Workgroup, Intranet, or Enterprise?Personalized or Generalized?Personalized or Generalized?

What is your user audience?What is your user audience?Technically Sophisticated?Technically Sophisticated?Impatient?Impatient?

What is your data like?What is your data like?Detailed, hard to generate reports or simple data? Detailed, hard to generate reports or simple data? Persistent or volatile?Persistent or volatile?OrderOrder--entry or decision support?entry or decision support?

How are you interacting?How are you interacting?Synchronous or asynchronous?Synchronous or asynchronous?

Page 4: Performance Tuning Java Applications

Why doesn’t my application perform?Why doesn’t my application perform?Why doesn’t my application perform?

Political Constraints (Boss, Client requires it “done a certain Political Constraints (Boss, Client requires it “done a certain way)way)InfrastructureInfrastructure——both the infrastructure you have and what you both the infrastructure you have and what you don’tdon’t..

Memory, CPU, OS, Disk SpaceMemory, CPU, OS, Disk SpaceDirectory Servers, Databases, Legacy ApplicationsDirectory Servers, Databases, Legacy ApplicationsNetworkNetworkApplication ServerApplication Server

TimeTime——you have limited resourcesyou have limited resourcesMoneyMoneyThirdThird--party technologiesparty technologiesJVM and/or JDKJVM and/or JDKCompilerCompiler

Page 5: Performance Tuning Java Applications

How It Doesn’t PerformHow It Doesn’t PerformHow It Doesn’t Perform

Scalability: “It worked great for the first 50 users…” Scalability: “It worked great for the first 50 users…” CPU UtilizationCPU UtilizationMemory UtilizationMemory UtilizationI/O UtilizationI/O Utilization

Reliability: “Where did it go?”Reliability: “Where did it go?”Latency: “It works great, once it gets started…”Latency: “It works great, once it gets started…”Throughput: “How long, on average, does it take to do ‘x’?”Throughput: “How long, on average, does it take to do ‘x’?”

Algorithm DesignAlgorithm DesignArchitectureArchitecture

Page 6: Performance Tuning Java Applications

What to Do…What to Do…What to Do…

Political Constraints: Political Constraints: Work within constraintsWork within constraintsget a friend who knows how to work these situationsget a friend who knows how to work these situations

Infrastructure:Infrastructure:Test software on the systems you’ll be usingTest software on the systems you’ll be usingIdentify weak portions of technology and avoid themIdentify weak portions of technology and avoid themTune your codeTune your code

TimeTime——get good tools, choose methodology wellget good tools, choose methodology wellMoneyMoney——see “Political Constraints”see “Political Constraints”ThirdThird--party technologiesparty technologies

Tune your codeTune your codeJVM, JDK, and CompilerJVM, JDK, and Compiler——choose faster option, if supported and choose faster option, if supported and availableavailable

Page 7: Performance Tuning Java Applications

A Tuner’s ToolkitA Tuner’s ToolkitA Tuner’s Toolkit

Packet Packet SnifferSnifferProfiler (Code coverage, Memory profiler, Thread profiler)Profiler (Code coverage, Memory profiler, Thread profiler)Unit testsUnit testsLoad testerLoad tester

JMeterJMeterjava.java.awtawt.Robot.RobotExpect (if you can use Expect (if you can use TclTcl))Mercury, Silk, other commercial packagesMercury, Silk, other commercial packages

Network, Architecture, and Class DiagramsNetwork, Architecture, and Class DiagramsOther environments (OS, Foreign languages, AppServer, Other environments (OS, Foreign languages, AppServer, VMWareVMWare, Browsers…), Browsers…)

Page 8: Performance Tuning Java Applications

Performance Testing WorkflowPerformance Testing WorkflowPerformance Testing Workflow

design develop

unit test

O.K.no

yes

start analysis

perf test

profile stopperf o.k.? yesno

Page 9: Performance Tuning Java Applications

Temporary Objects Temporary Objects Profiling DemoProfiling Demo

Page 10: Performance Tuning Java Applications

What Is a JVM?What Is a JVM?What Is a JVM?

Can be thought of as a “processor on top of a processor.”Can be thought of as a “processor on top of a processor.”Has a program counter, stacks, method memory space, and Has a program counter, stacks, method memory space, and heap space.heap space.Direct support for the following types:Direct support for the following types: intint, long, float, double, , long, float, double, “references”, and return addresses (hidden, no pointers).“references”, and return addresses (hidden, no pointers).Partial support forPartial support for booleanboolean, byte, char, and short, byte, char, and short——partially partially treated as integers.treated as integers.Return values are kept in “frames”, which are allocated andReturn values are kept in “frames”, which are allocated anddeallocateddeallocated as methods run and complete.as methods run and complete.

Distinct from the class libraries supplied with the JDK or java Distinct from the class libraries supplied with the JDK or java runtime.runtime.Native methods throw a “twist” into how the JVM “hardware” Native methods throw a “twist” into how the JVM “hardware” values are interpretedvalues are interpreted----native method “stacks” may be handled native method “stacks” may be handled differently by differentdifferently by different VM’sVM’s (or not supported at all).(or not supported at all).

Page 11: Performance Tuning Java Applications

JVM DiagramJVM DiagramJVM Diagram

pc

stack………

irbne…

dup…………

Thread-local

nativestack………

Execution Engine(“Clock”, ALU, ...)

heap…………………

Page 12: Performance Tuning Java Applications

JVM Characteristics to Watch Out forJVM Characteristics to Watch Out forJVM Characteristics to Watch Out for

ManyMany JVM’sJVM’s (including sun’s) allow the heap to grow. Although (including sun’s) allow the heap to grow. Although garbage collection frees space in the heap, the heap itself garbage collection frees space in the heap, the heap itself doesn’t shrink. doesn’t shrink. Consequently, creating a lot of objects can steal Consequently, creating a lot of objects can steal memory away from other processes that can’t be reclaimed memory away from other processes that can’t be reclaimed without killing a java processwithout killing a java process..There are two “sizes” of objects that go on the stackThere are two “sizes” of objects that go on the stack——intint width width and long width. Variables of long and double width take up two and long width. Variables of long and double width take up two entries on the stack, and take twice as long to hop on or off. entries on the stack, and take twice as long to hop on or off. The The short,short, booleanboolean, byte, references, and char variables take up an, byte, references, and char variables take up anintint entryentry——the rest of the space (if any) is wasted.the rest of the space (if any) is wasted.Frames hold local variables, incoming operands, and return Frames hold local variables, incoming operands, and return values. Again, doubles and longs take up twice as much room.values. Again, doubles and longs take up twice as much room.

Page 13: Performance Tuning Java Applications

A Very Simple ClassA Very Simple ClassA Very Simple Classpublic voidpublic void setUsernamesetUsername(String username) {(String username) {

this.username = username;this.username = username;}}public Stringpublic String getUsernamegetUsername() {() {return username;return username;

}}public voidpublic void sayHellosayHello(){(){System.out.System.out.printlnprintln(message + " " + username + "!");(message + " " + username + "!");

}}public longpublic long addTwoLongaddTwoLong(long a, long b){(long a, long b){return a + b;return a + b;

}}publicpublic int addTwoIntint addTwoInt((intint a,a, intint b){b){return a + b;return a + b;

}}public static void main(String[]public static void main(String[] argsargs) {) {SimpleHelloSimpleHello simpleHello1 = newsimpleHello1 = new SimpleHelloSimpleHello();();simpleHello1.simpleHello1.sayHellosayHello();();

}}}

package hello;package hello;

/**/*** This is a <B>very</B> simple class, just to show what it take* This is a <B>very</B> simple class, just to show what it takess* to run a simple method.* to run a simple method.*/*/

public classpublic class SimpleHelloSimpleHello {{

// private attributes// private attributesprivate String message;private String message;private String username;private String username;

// Constructors// Constructorspublicpublic SimpleHelloSimpleHello() {() {message = "Hello";message = "Hello";username = "Dude";username = "Dude";

} } public voidpublic void setMessagesetMessage(String message) {(String message) {

this.message = message;this.message = message;}}public Stringpublic String getMessagegetMessage() {() {return message;return message;

}

}

}

Page 14: Performance Tuning Java Applications

How “SimpleHello” Appears to the VMHow “How “SimpleHelloSimpleHello” Appears to the VM” Appears to the VM

SimpleHello ConstructorConstructor0 aload_01 invokespecial #1 <Method java.lang.Object()>4 aload_05 ldc #2 <String "Hello">7 putfield #3 <Field java.lang.String message>10 aload_011 ldc #4 <String "Dude">13 putfield #5 <Field java.lang.String username>16 return

addTwoLong0 lload_11 lload_32 ladd3 lreturn

Local variables for method long addTwoLong(long, long)long b pc=0, length=4, slot=3long a pc=0, length=4, slot=1hello.SimpleHello this pc=0, length=4, slot=0

SimpleHello SimpleHello ConstructorConstructorpublic SimpleHello() {

message = "Hello";username = "Dude";

}

addTwoLongpublic longpublic long addTwoLongaddTwoLong(long a, long b){(long a, long b){

return a + b;return a + b;}}

Page 15: Performance Tuning Java Applications

How to “Say Hello”How to “Say Hello”How to “Say Hello”

sayHellopublic voidpublic void sayHellosayHello(){(){

System.out.System.out.printlnprintln(message + " " + username + "!");(message + " " + username + "!");}

sayHello0 getstatic #6 <Field java.io.PrintStream out>3 new #7 <Class java.lang.StringBuffer>6 dup7 aload_08 getfield #3 <Field java.lang.String message>11 invokestatic #8 <Method java.lang.String valueOf(java.lang.Object)>14 invokestatic #8 <Method java.lang.String valueOf(java.lang.Object)>17 invokespecial #9 <Method java.lang.StringBuffer(java.lang.String)>20 ldc #10 <String " ">22 invokevirtual #11 <Method java.lang.StringBuffer

append(java.lang.String)>25 aload_026 getfield #5 <Field java.lang.String username>29 invokevirtual #11 <Method java.lang.StringBuffer

append(java.lang.String)>32 ldc #12 <String "!">34 invokevirtual #11 <Method java.lang.StringBuffer

append(java.lang.String)>37 invokestatic #8 <Method java.lang.String valueOf(java.lang.Object)>40 invokestatic #8 <Method java.lang.String valueOf(java.lang.Object)>43 invokevirtual #13 <Method void println(java.lang.String)>46 return

}

Page 16: Performance Tuning Java Applications

CompilersCompilersCompilers

JavacJavacBCJ: BCJ: JBuilder’sJBuilder’s compilercompiler——optimizations, including obfuscation, optimizations, including obfuscation, targeting specific releases of the JVM, and controlling the targeting specific releases of the JVM, and controlling the amount of debug information provided within the classes.amount of debug information provided within the classes.JikesJikes: Provides some optimization, and a very useful “lint” : Provides some optimization, and a very useful “lint” featurefeatureUniversity of Indiana’s “JAVAR”University of Indiana’s “JAVAR”——finds parallelism in codefinds parallelism in code

Page 17: Performance Tuning Java Applications

Memory Leak Profiling Memory Leak Profiling DemoDemo

Page 18: Performance Tuning Java Applications

Networking and I/ONetworking and I/ONetworking and I/O

JDK 1.4 has improved I/O classes (JDK 1.4 has improved I/O classes (nonblocking nonblocking I/O, channels)I/O, channels)Nonblocking Nonblocking I/O is available for earlier I/O is available for earlier JDK’s JDK’s at at http://www.cs.berkeley.edu/~mdw/proj/javahttp://www.cs.berkeley.edu/~mdw/proj/java--nbio/nbio/Watch Watch FileInputStreamFileInputStream, , FileOutputStreamFileOutputStream, and , and RandomAccessFileRandomAccessFile. (. (FileReader FileReader and and FileWriter FileWriter use the streams)use the streams)Be careful passing collections of objects through RMI/IIOP or Be careful passing collections of objects through RMI/IIOP or CORBACORBA——these objects are passed as “these objects are passed as “any’sany’s”, which are heavy.”, which are heavy.Minimize objects transferredMinimize objects transferred——transfer only what you have to.transfer only what you have to.Speed generally follows this order:Speed generally follows this order:1.1. Register Access and Cache (local primitive variables)Register Access and Cache (local primitive variables)2.2. RAM (heap variables, arrays, and all classes)RAM (heap variables, arrays, and all classes)3.3. Disk (files)Disk (files)4.4. Network (remote calls)Network (remote calls)

Page 19: Performance Tuning Java Applications

How Do I Cut Down Network Traffic?How Do I Cut Down Network Traffic?How Do I Cut Down Network Traffic?

Keep a local (cached) copy. Value Objects work well, here.Keep a local (cached) copy. Value Objects work well, here.Choose your technology wisely (CORBA and RMI beat SOAP).Choose your technology wisely (CORBA and RMI beat SOAP).Message Queues can help when the data transfer doesn’t require Message Queues can help when the data transfer doesn’t require synchronous connectionssynchronous connections——they won’t block.they won’t block.Identify tight communication “communities”, and Identify tight communication “communities”, and colocate colocate that that code.code.Use value objects for getter’s and setter’sUse value objects for getter’s and setter’s——and reduce the and reduce the network traffic from a number of small calls to one big one.network traffic from a number of small calls to one big one.Validate input data as close to the client as possible.Validate input data as close to the client as possible.Use constants where possible (for instance “1” is easier that Use constants where possible (for instance “1” is easier that “Could not get a database connection, stack trace…”).“Could not get a database connection, stack trace…”).

Page 20: Performance Tuning Java Applications

What About Databases?What About Databases?What About Databases?

Use prepared statements, when possible.Use prepared statements, when possible.Don’t use “SELECT *”.Don’t use “SELECT *”.Use the lightest transaction possible Use the lightest transaction possible 1.1. TRANSACTION_NONETRANSACTION_NONE2.2. TRANSACTION_READ_UNCOMMITTED TRANSACTION_READ_UNCOMMITTED 3.3. TRANSACTION_READ_COMMITTED TRANSACTION_READ_COMMITTED 4.4. TRANSACTION_REPEATABLE_READTRANSACTION_REPEATABLE_READ

5.5. TRANSACTION_SERIALIZABLETRANSACTION_SERIALIZABLE

ODBMS systems work better for pure OO data.ODBMS systems work better for pure OO data.Use Connection Pools.Use Connection Pools.Batch queries, if possible.Batch queries, if possible.

Page 21: Performance Tuning Java Applications

NonNon--blocking I/O Demoblocking I/O Demo

Page 22: Performance Tuning Java Applications

ThreadsThreadsThreads

Even though two threads can Even though two threads can theoreticallytheoretically synchronize at the synchronize at the same time, the same time, the mutexmutex locking process itself is singlelocking process itself is single--threaded. threaded. Prefer Maps to Prefer Maps to HashtablesHashtables, and , and ArrayListsArrayLists to Arrays, since the to Arrays, since the former allow a choice of synchronization. former allow a choice of synchronization. ParallelizingParallelizing work between multiple threads can helpwork between multiple threads can help

If you can, keep as much code as you can within threadIf you can, keep as much code as you can within thread--local local storage, and “synchronize at the end”storage, and “synchronize at the end”Thread pools can save work for heavily loaded applications.Thread pools can save work for heavily loaded applications.

Produce working, threadProduce working, thread--safe code firstsafe code first——then optimize.then optimize.Synchronization on its own is not enough to provide proper Synchronization on its own is not enough to provide proper codecode——threads have to act in the proper order.threads have to act in the proper order.

Page 23: Performance Tuning Java Applications

Collection SynchronizationCollection SynchronizationCollection Synchronization

Vector synchronizes the entire Vector synchronizes the entire object on these methods:object on these methods:

add() add() addAlladdAll() () clone() clone() containsAllcontainsAll()()elements().elements().nextElementnextElement() () equals() equals() get() get() hashCodehashCode()()remove() (calls "remove() (calls "removeElementremoveElement")")removeAllremoveAll() () removeAllElementsremoveAllElements() () set() set() toArraytoArray() () toStringtoString()

HashtableHashtable synchronizes the entire synchronizes the entire object on these methods:object on these methods:

clear()clear()clone()clone()contains(Objectcontains(Object objobj))containsKeycontainsKey(Object(Object objobj))elements()elements()equals(Objectequals(Object objobj))get(Objectget(Object objobj))hashCodehashCode()()keys()keys()put(Objectput(Object objobj, Object obj1), Object obj1)putAllputAll(Map map)(Map map)readObjectreadObject((ObjectInputStream objectinputstreamObjectInputStream objectinputstream))remove() [synchronized(remove() [synchronized(HashtableHashtable.this)].this)]remove(Objectremove(Object objobj) ) toStringtoString()()writeObjectwriteObject((ObjectOutputStream objectoutputstreamObjectOutputStream objectoutputstream))

()

If you use a vector orIf you use a vector or hashtablehashtable for configuration data, you’re blocking other for configuration data, you’re blocking other parts of your application.parts of your application.

Page 24: Performance Tuning Java Applications

Execution OrderExecution OrderExecution Order

Actions performed by one thread are ordered.Actions performed by one thread are ordered.For any single variable on the heap, access is implicitly For any single variable on the heap, access is implicitly synchronized .synchronized .

If two threads write to the same variable, one always writes If two threads write to the same variable, one always writes before the other.before the other.Which thread accesses a heap variable first is undeterminedWhich thread accesses a heap variable first is undeterminedWAW, WAR, and RAW hazards are still possible.WAW, WAR, and RAW hazards are still possible.

Within a monitored (locked) section, access is synchronized.Within a monitored (locked) section, access is synchronized.Actions only happen once.Actions only happen once.

Page 25: Performance Tuning Java Applications

CPU Profiling DemoCPU Profiling Demo

Page 26: Performance Tuning Java Applications

Bibliography and ReferencesBibliography and ReferencesBibliography and References

ShiraziShirazi, Jack, Jack, Java Performance Tuning, O'Reilly and Associates,, Java Performance Tuning, O'Reilly and Associates,SebastapolSebastapol, CA, September 2000 , CA, September 2000 Wilson, Steve and JeffWilson, Steve and Jeff KesselmanKesselman, Java Platform Performance: , Java Platform Performance: Strategies and Tactics (First Edition), Strategies and Tactics (First Edition), Addison Wesley, Reading, MA, Addison Wesley, Reading, MA, http://java.sun.com/docs/books/performance/http://java.sun.com/docs/books/performance/Lea, DougLea, Doug, Concurrent Programming in Java, , Concurrent Programming in Java, Addison Wesley, Reading, Addison Wesley, Reading, MA, 1997MA, 1997LindholmLindholm, Tim and Frank, Tim and Frank YellinYellin, The Java Virtual Machine Specification , The Java Virtual Machine Specification Second Edition, Second Edition, Addison Wesley, Reading, MA,1999Addison Wesley, Reading, MA,1999VennersVenners, Bill, Bill, Inside the Java Virtual Machine, , Inside the Java Virtual Machine, McGrawMcGraw--Hill Professional Hill Professional Publishing, December 1999Publishing, December 1999AlurAlur, Deepak and John, Deepak and John CrupiCrupi and Danand Dan MalksMalks, Core J2EE Patterns: Best , Core J2EE Patterns: Best Practices and Design Strategies, Practices and Design Strategies, Prentice Hall PTR, Palo Alto, CA,2001Prentice Hall PTR, Palo Alto, CA,2001Hennessy, John and David PattersonHennessy, John and David Patterson, Computer Architecture: A , Computer Architecture: A Quantitative Approach, Quantitative Approach, MorganMorgan KaufmannKaufmann Publishers, Palo Alto, CA, 1990Publishers, Palo Alto, CA, 1990