logging in or signing up Level 2.3 CBT course on Core Java Fundamentals - Performance Optimizat kaslijain Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 51 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: March 03, 2011 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Slide 1: 3/3/2011 TPG Confidential 1 Technology Practices Group Java Competency Framework "Incubate, Nurture and Deploy Technology Experts Innovate, Build and Deliver Technology Solutions” Slide 2: 3/3/2011 TPG Confidential 2 Introduction Revision History : 3/3/2011 TPG Confidential 3 Revision History Slide 4: 3/3/2011 TPG Confidential 4 Table of Contents Introduction To Performance Optimization Optimization in Java Areas of optimization in Java Reasons Not to Optimize References Slide 5: 3/3/2011 TPG Confidential 5 Chapter 1 : Introduction To Performance Optimization Slide 6: 3/3/2011 TPG Confidential 6 What is performance ? Performance is the way a computer system behaves given a particular workload. Performance is measured in terms of system response time, throughput and availability. Performance is also affected by: - The resources available in the system - How well those resources are used and shared. Slide 7: 3/3/2011 TPG Confidential 7 What is performance Tuning? Performance Tuning is the technique to improve the performance of application. It is easier than debugging, focus on the bottlenecks. A good round of Performance Tuning makes an application run fast. In general, the system is tuned to improve its cost-benefit ratio. Slide 8: 3/3/2011 TPG Confidential 8 Reasons for Performance Optimization Any application needs considerable amount of following resources for its execution. CPU Memory Network bandwidth. Sometimes there may be very limited resources and the program execution will fail. The resources are consumed at a larger pace because of the inefficient code . Slide 9: 3/3/2011 TPG Confidential 9 Reasons (Contd) To use the available resources efficiently. Most of the business applications involve multi users operations and the time to perform any activity should be minimal as much as possible. To obtain faster system response times or higher throughput without increasing the processing costs. . Slide 10: 3/3/2011 TPG Confidential 10 Reasons (Contd) Code fragments that occur multiple times throughout a program are likely to be size-sensitive, while code with many execution iterations may be speed-sensitive . To add more users in the system at later stages. Writing optimized working code rather than working code became mandatory to be successful in the current competitive world. Slide 11: 3/3/2011 TPG Confidential 11 Chapter 2 : Optimization in Java Slide 12: 3/3/2011 TPG Confidential 12 Optimization in Java Java performance tuning means optimizing code for speed, reliability, scalability and maintainability. It is required to find out which parts of the applications represent bottlenecks and which are behaving efficiently. Once bottlenecks are identified, tuning can be done on various elements to improve the performance of the application. Slide 13: 3/3/2011 TPG Confidential 13 Chapter 3 : Areas of Optimization in Java Slide 14: 3/3/2011 TPG Confidential 14 Areas of Optimization in Java In a typical java based application, the various areas where performance can be optimized are: Object creation Loops String Manipulation IO Serialization Exceptions Collections Threading Effective Usage of Size Slide 15: 3/3/2011 TPG Confidential 15 Object Creation - Optimization Brief Introduction on objects: Objects are instances of the class. Class contains data and methods to manipulate the data. There are two steps to create the objects of a class. They are: Declare a variable of class type. Create a physical copy of an object using ‘new’ operator. ‘new’ operator dynamically (during runtime) allocates space in memory for an object Slide 16: 3/3/2011 TPG Confidential 16 Brief Introduction on objects (contd): We can create as many objects as it needs during the execution of time. There is a possibility that memory is not sufficient during run time and run-time exception will occur. Whenever an object is created, the following steps are involved: Memory is allocated to all the variables Memory allocated to all super class variables All variables – super class, sub class are initialized The constructor is invoked Slide 17: 3/3/2011 TPG Confidential 17 Brief Introduction on objects (contd): Whenever an object is created all the above steps are repeated and consume considerable resources. So decision to create new object plays a crucial role for memory management. In memory all the objects are placed in heap and their address in the heap are stored in stack. All primitive data types are stored in the stack Slide 18: 3/3/2011 TPG Confidential 18 Bottlenecks in Object Creation: Memory leaks by holding on to objects, without releasing references. This stops the garbage collector from reclaiming those objects. Java does not support creation of large number of objects simultaneously. Creating objects, Garbage Collection and memory recycling cost more time and CPU effort. Slide 19: 3/3/2011 TPG Confidential 19 Guidelines for Optimization - Objects Creation: Avoid creation of objects in loops Avoid creating objects in frequently used routines. Try to use String literals, instead of String objects created using ‘new’ keyword. Example: Use String str = “Optimization”; Intead of String str = new String(“Optimization”); Pre-size a Collection to its largest potential size to reduce the number of objects created. Slide 20: 3/3/2011 TPG Confidential 20 Guidelines for Optimization - Objects Creation: Use primitive datatype instead of using wrapper classes. Using primitive datatype speeds up garbage collection. For example an object with just one instance variable holding an int is reclaimed in one object reclaim, whereas if it holds an Integer object, the garbage collector needs to reclaim two objects. As accessing local variables are faster than accessing class variables, use local variables instead of using class variables whenever possible. Slide 21: 3/3/2011 TPG Confidential 21 Guidelines for Optimization - Objects Creation: Make use of static variables, when multiple instances of a class need to access a particular entity. Avoid initializing instance variables more than once. Whenever the object use is completed, make the reference of the object as null so that the object becomes eligible for garbage collection . Empty Collection objects before reusing them. Slide 22: 3/3/2011 TPG Confidential 22 Guidelines for Optimization - Objects Creation: Reduce the number of temporary objects being used, especially in loops. Keep constructors simple and inheritance hierarchies shallow Reuse Exception instances, when a stack trace is required. Replace constant objects with integers (enumerating constants), which can provide both speed and memory as enumeration requires less memory than the equivalent Strings. Slide 23: 3/3/2011 TPG Confidential 23 Guidelines for Optimization - Objects Creation: Use lazy evaluation techniques. In lazy evaluation technique, the computations will be done only when it is necessary. By doing this, some computations may not be done at all when a scenario is executed . Similarly, use lazy objects creation. In this the objects should be created before they are going to be used. By this way, lot of memory can be saved. Slide 24: 3/3/2011 TPG Confidential 24 Loops - Optimization Brief Introduction on Loops: The Java programming language has three categories of control statements . They are: Selection statements Iteration statements Jump Statements Iteration statements are used to execute the same set of code as many times as required. Thus iteration statements are called loops . Slide 25: 3/3/2011 TPG Confidential 25 Brief Introduction on Loops (Contd): Java has three types of loops . They are: for while do-while. “for” loop is used when the number of iterations required is known in advance. “while” loop is used when the number of iterations required is not known. In “while” loop, the condition is checked for each time execution and body of the loop is executed if the condition is true. Slide 26: 3/3/2011 TPG Confidential 26 Brief Introduction on Loops (Contd): The “do-while” loop is used when the loop needs to get executed atleast once. As loops are extensively used in all kinds of applications, these statements have considerable impact on the performance of the application. Slide 27: 3/3/2011 TPG Confidential 27 Guidelines for Optimization - Loops: Use the int data type as loop index variable instead of using byte or short data types, as short and byte data types will be converted as int data type by implicit type cast . Try to use the fastest tests in loops Always avoid anything that can be done outside of the loop like method calls, assigning values to variables, or testing for conditions. Eliminate unneeded temporary variables from loops. Slide 28: 3/3/2011 TPG Confidential 28 Guidelines for Optimization - Loops: Avoid using method calls in loops for conditional statement as this is expensive. Instead use a temporary variable to store the value and test the terminating condition. Example: instead of writing, for (int i=0 ; i < s.length(); i++) { charc c = s.charAt(i); } write it as , int j =str.length(); for (int i =0 ; i < j; i++) { charc c =s.charAt(i); } By doing this, an extra overhead on conditional statement is removed. Slide 29: 3/3/2011 TPG Confidential 29 Guidelines for Optimization - Loops: Use short circuit operators if there is more than one conditional statement in loops. If there are only || (or) operators, put the expression that is most likely to evaluate to true at the extreme left. If there is any && (and) operator, put the expression that is most likely to evaluate false at the extreme left. This helps to avoid all the following expressions being tested. Avoid method calls in loops when possible, even if this requires rewriting or inlining. Slide 30: 3/3/2011 TPG Confidential 30 Guidelines for Optimization - Loops: To copy the elements from one array to another, use System.arraycopy() instead of using loops . Example: Public Class TestArrayCopy { public static void main(String s[]) { //Declaring Variables long startTime, endTime; int[] a = new int[2500000]; int[] b = new int[2500000]; //Initializing array for (int i = 0; i < a.length; i++) { a[i] = i; } Slide 31: 3/3/2011 TPG Confidential 31 Arraycopy Example (contd); //Copying using loop startTime = System.currentTimeMillis(); for (int j = 0; j < a.length; j++) { b[j] = a[j]; } endTime = System.currentTimeMillis(); System.out.println(endTime - startTime + " milli seconds for loop copy "); } } The output of the above is: 32 milli seconds for loop copy 15 milli seconds for System.arraycopy() Slide 32: 3/3/2011 TPG Confidential 32 Guidelines for Optimization - Loops: Do not use exception handling inside loops. (i.e) Do not place using try-catch inside the loops instead place the loops inside the try-catch for better performance . Example: Instead of writing, String s = "Learning Java"; for(int i=0; i< 10; i++){ try{ System.out.println("Character in s at:" + i +":" + s.charAt(i)); } catch(Exception exp){ } } Slide 33: 3/3/2011 TPG Confidential 33 Exception Handling Example (contd); Write it as, for(int i=0; i< 10; i++){ System.out.println("Character in s at:" + i +":" + s.charAt(i)); } } catch(Exception exp){ } Slide 34: 3/3/2011 TPG Confidential 34 Strings – Optimization Brief Introduction on Strings: String objects are immutable objects and StringBuffer objects are mutable objects. Mutable objects can be modified after their creation whereas immutable objects cannot be modified after creation. Both String and StringBuffer classes can not be sub classed. Slide 35: 3/3/2011 TPG Confidential 35 Guidelines for Optimization - Strings: There are two ways to create the Strings. They are: String S1 = “Optimization” String S2 = “Optimization” String S1 = new String(“Optimization”) String S2 = new String(“Optimization”) As creating strings with new keyword is expensive, the first method is better method for creating string with respect to performance. Slide 36: 3/3/2011 TPG Confidential 36 Guidelines for Optimization - Strings: There are three ways to concatenate multiple strings. They are: + operator String.concat() method StringBuffer.append() The choice to concatenate strings should be made based on the scenario. If the values are known at the compile time, use ‘+’ operator for string concatenation. This is called compile time resolution. Slide 37: 3/3/2011 TPG Confidential 37 Guidelines for Optimization - Strings: If the values are not known at the compile time and available at compile time, use StringBuffer concatenation for string concatenation. This is called runtime resolution. Manipulate characters in char arrays rather than String and StringBuffer manipulation. Avoid using the StreamTokenizer. Slide 38: 3/3/2011 TPG Confidential 38 Guidelines for Optimization - Strings: Set the initial capacity of the StringBuffer using its constructor. StringBuffer maintains a character array internally. When the StringBuffer is created with default constructor without length, the StringBuffer is initialized with 16 characters. When the StringBuffer reaches its maximum capacity, the size of it will be increased by twice the size plus 2. Thus if default size is used initially and go on adding characters, then it increases its size by 34(2*16 +2) after it adds 16th character and it increases its size by 70(2*34+2) after it adds 34th character. Slide 39: 3/3/2011 TPG Confidential 39 Guidelines for Optimization - Strings: Whenever it reaches its maximum capacity it has to create a new character array and recopy old and new characters. It is obviously expensive. So it is always good to initialize with proper size that gives very good performance. Logically partition strings into that require internationalization support and those that don’t need internationalization. The later can be replaced for tuning with char arrays. Slide 40: 3/3/2011 TPG Confidential 40 Guidelines for Optimization - Strings: Interning Strings for String comparison: If the == operator is used to compare the strings, it will return true if the string references are same. If the references are same those objects are identical. The String class has a method intern() to create an internal pool of unique strings. Example: str = str.intern() Interned strings can be compared with each other using ==, which is much cheaper than equals() method. String literals are always interned. Slide 41: 3/3/2011 TPG Confidential 41 Input/Output – Optimization Brief Introduction on I/O: Input and Output streams are used read from and write to devices such as file or network or console. A stream is a logical entity that either produces or consumes information. java.io package provides I/O classes to manipulate streams. This package supports two types of streams - binary streams which handle binary data and character streams which handle character data InputStream and OutputStream are high level interfaces for manipulating binary streams. Reader and Writer are high level interfaces for manipulating character streams Disk and network transfers are expensive activities and are two of the most likely problems for performance. Slide 42: 3/3/2011 TPG Confidential 42 Input/Output – Optimization Relationship between Streams: Slide 43: 3/3/2011 TPG Confidential 43 Guidelines for Optimization - I/O: Two standard optimization techniques for reducing I/O overhead are: buffering and caching. Buffering groups the data into larger chunks, improving the efficiency of the I/O by reducing the no. of I/O operations. Caching the objects or data are accessed repeatedly can replace an I/O call with a hugely faster memory access. By default, most of the streams read or write one byte at a time. Reading and writing data in small chunks can be very slow. That is, it takes lot of time to read/write byte by byte when the large amounts of data needs to be handled. Slide 44: 3/3/2011 TPG Confidential 44 Guidelines for Optimization - I/O: I/O provides Buffered streams to override this byte by byte default behaviors. Use BufferedInputStream and BufferedOutputStream to batch requests to improve performance. Example: FileInputStream facilitates reading from a file and FileOutputStream facilitates writing to a file. These classes have two types of methods, one to read/write one byte at a time and the other method to read/write array of bytes. Slide 45: 3/3/2011 TPG Confidential 45 Guidelines for Optimization - I/O: Methods for FileInputStream: FileInputStream.read(); FileInputStream.read(byte[] b); FileInputStream.read(byte[] b, int off, int length); Methods for FileOutPutStream FileInputStream.read(); FileInputStream.read(byte[] b); FileInputStream.read(byte[] b, int off, int length); Methods for BufferedInputStream BufferedInputStream.read(); BufferedInputStream.read(byte[] b, int off, int len); Slide 46: 3/3/2011 TPG Confidential 46 Guidelines for Optimization - I/O: Methods for BufferedOutputStream BufferedOutputStream.write(int b); BufferedOutputStream.write(byte[] b, int off, int len); The following figure shows how buffered streams divert the data flow Source File FileInputStream BufferedInputStream Destination File FileOutputStream BufferedOutputStream Slide 47: 3/3/2011 TPG Confidential 47 Guidelines for Optimization - I/O: The default size for buffer streams is 512 bytes. It accumulates the data till it reaches this capacity and reads from or write to destination. The default size can be customized by passing required buffer size through constructor by: BufferedInputStream(InputStream in, int size); BufferedOutputStream(OutputStream out, int size); In some situations custom buffering techniques can be used to maximize performance. It means creating our own buffer while reading or writing streams using existing methods. Execute I/O in the background thread. Avoid or reduce I/O calls in loops. Keep files open and navigate around them rather than repeatedly opening and closing files. Replace System.out and System.err with customized PrintStream classes to control Console output. Slide 48: 3/3/2011 TPG Confidential 48 Guidelines for Optimization - I/O: Avoid the use of System.out.println: System.out.println synchronizes processing for the duration of disk I/O, and that can slow throughput significantly As much as possible, avoid the use of this. Eventhough sophisticated debugging tools are available, sometimes System.out.println remains useful for tracing purpose, or for error and debugging situations. The application should be configured so that it gets turn on during error and debugging situations only. This can be done by using a final Boolean variable. This helps to optimize both the check and execution of the tracing at compile time. Optimize network transfers by transferring only the data and objects needed and no more. Avoid compression when the system has a heavily loaded CPU. Consider searching directly, against compressed data without decompressing. Slide 49: 3/3/2011 TPG Confidential 49 Serialization - Optimization Brief Introduction on serialization: Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket. Deserialization is the process of reading back that serialized java object stream from input stream. All primitive data types and some of standard java API classes are serializable A java object is serializeable and deserializeable if that class follows the following rules The java class must implement java.io.Serializable interface or java.io.Externalizable interface or inherit that implementation from any one of it's super class implementation. All instance variables of that class must implement Serializable interface or Externalizable interface or inherit from one of it's super class Serialization process ignores class (static) variables. Slide 50: 3/3/2011 TPG Confidential 50 Brief Introduction on serialization: Serializable interface is a marker interface and does not have any methods. All major java technologies like RMI, EJB are based on serialization process to pass the objects through network. These technologies implicitly do all the serialization work. When you write or read an object to a file or network or other stream using serialization process, it writes/reads the complete object state. i.e. it writes the object, it's instance variables, and super class instance variables except transient variables and class (static) variables. Serializable interface is a marker interface and does not have any methods. All major java technologies like RMI, EJB are based on serialization process to pass the objects through network. These technologies implicitly do all the serialization work. When you write or read an object to a file or network or other stream using serialization process, it writes/reads the complete object state. i.e. it writes the object, it's instance variables, and super class instance variables except transient variables and class (static) variables. Slide 51: 3/3/2011 TPG Confidential 51 Guidelines for Optimization - Serialization: Serialization can be very costly. Using the transient keyword reduces the amount of data serialized. Because the variables having transient access modifier will not be read from or written to streams. It boost performance by avoiding unnecessary data into streams. Spread the de-serialization overhead to other times. Handle serializing explicitly, rather than using default serialization mechanisms. Slide 52: 3/3/2011 TPG Confidential 52 Exceptions – Optimization Brief Introduction on Exceptions: Java provides an efficient way to handle unexpected conditions that can occur in the program. When there is an error or bug in the program then the program terminates as soon as an error is encountered leaving the program in an in consistent state. To avoid this Java makes use of Exception Handling mechanism to a great advantage so that when ever there is an exceptional condition then the program handles it gracefully and continues program execution . The whole concept of exceptions/errors is handled by the java.lang.Throwable class . Throwable class has two subclasses. They are: Error and Exception The application should handle all the Exceptions whereas Errors are normally handled by JVM. Exceptions are of two types -Checked exceptions and Unchecked exceptions Checked exceptions can either be declared in the throws clause or caught in the catch block. Unchecked exceptions need not be declared in the throws clause but can be caught in the catch clause Slide 53: 3/3/2011 TPG Confidential 53 Guidelines for Optimization - Exceptions: try-catch blocks generally use no extra time, if no exception is thrown. But, throwing an exception and executing the catch block has a significant overhead. This overhead seems to be due mainly to the cost of getting a snapshot of the stack when the exception is created. Do not use Exception handling for anything other than exception handling like to control the flow of the program. Avoid using generic class Exception in the catch block. Use specific exception classes in the catch block. Avoid using generic class Exception in the throws clause. Use specific exception classes in the throws clause. Consider throwing exceptions without generating a stack trace by reusing a previously created instance. The disadvantage of reusing an exception instance is that the instance does not have the stack trace. Always use the finally block to release the resources like a database connection, closing a file or socket connection etc. This prevents resource leaks even if an exception occurs . Slide 54: 3/3/2011 TPG Confidential 54 Guidelines for Optimization - Exceptions: Use instanceof instead of making speculative class casts in a try-catch block. Handle Exceptions locally always. i.e. When using method calls always handle the exceptions in the method where they occur, do not allow them to propagate to the calling method unless it is specifically required. It is efficient to handle them locally since allowing them to propagate to the calling method takes more execution time. Do not use Exception handling in loops. It is better to place loops inside try/catch blocks than vice versa. Here is an code snippet that gives bench mark. Slide 55: 3/3/2011 TPG Confidential 55 Collections Framework – Optimization Brief Introduction on Collections: Collection is a group of objects. The framework provides highly efficient fundamental collections such as dynamic arrays, linked lists, trees and hash tables. This framework helps to all types of collections to work in a similar manner. java.util package provides important types of collections. There are two fundamental types collections. They are Collection and Map. Collection helps to maintain a group of objects. Example for Collection type: Lists and sets Map helps to maintain a group of objects with the pair value. Example for Map type: Hashmap, Hashtable Lists: These are used to maintain an ordered collection of objects. Arraylist, LinkedList, Vector and Stack are some of the list implementation classes Slide 56: 3/3/2011 TPG Confidential 56 Brief Introduction on Collections: The common operation in the lists are: Adding objects, removing objects, accessing objects and iterating through the list. Sets: It is a collection of unique objects. It doesn’t allow duplicate objects and modification of existing objects. The common operation in the sets are: Adding objects, removing objects, accessing objects and iterating through the list. But the objects can not be modified. HashSet and TreeSet are the Set implementation classes. Maps: It is a collection of key and value object associations. The common operation in the maps are: Adding, removing and accessing key value pairs. HashMap, Hashtable, WeakHashMap and TreeMap are the Map implementation classes. HashMap, Hashtable and WeakHashMap have similar implementations. TreeMap is meant for sorted collection. Slide 57: 3/3/2011 TPG Confidential 57 Guidelines for Optimization - Collections: The optimization strategy is different for each class mentioned above based on the specific operations. The strategy depends upon the below options: Thread Safe Collection Size of collection and Type of operation – adding, removing, accessing or iterating The following slides have the strategy for each type of Collections based on the above options. Slide 58: 3/3/2011 TPG Confidential 58 Guidelines for Optimization - Lists: Thread Safe Lists: Use Vector and Stack to have the list as thread safe as both have synchronized methods. ArrayList and LinkedList are not thread safe. If the collection need not be thread safe, use ArrrayList for accessing and iterating objects and use LinkedList for adding and removing objects. ArrayList with initialization gives better performance than others because its methods are non-synchronized. Synchronized methods are bit expensive because JVM has to lock the objects whenever it finds synchronized methods. ArrayList and Vector maintain internal Object array. Whenever an object is added, it is added at the end of list. If the object needs to be added any other position, it creates a new object array and recopies all the objects which is expensive. This is the reason for taking more time when the objects are added at the beginning or middle of the list. LinkedList gives good performance when adding elements at the end and beginning but it is worse when adding objects at middle because it needs to scan the node whenever it needs to add an object. LinkedList cannot be initialized. Slide 59: 3/3/2011 TPG Confidential 59 Guidelines for Optimization - Lists: LinkedLists give better performance when the objects are removed from the beginning. All lists give same performance when the objects are removed from the end. When the objects are removed from the middle, use ArrayList or Vector as LinkedList takes more time than the ArrayList or Vector. Use ListIterator than Iterator and Enumeration for List types. Conclusion: Thread safe Lists : Vector and Stack Not Thread Safe Lists: ArrayList (Accessing and Iterating), LinkedList(Adding and Removing) Slide 60: 3/3/2011 TPG Confidential 60 Guidelines for Optimization - Sets: HashSet gives better performance than TreeSet because TreeSet is an ordered collection of objects. Whenever an object is inserted in the TreeSet, the objects are sorted whereas the objects in HashSet are added in the adhoc manner. All the operations in the TreeSet takes time as it has to compare and sort. Use HashSet and convert that to TreeSet after all the operations for better performance. Initialize HashSet or TreeSet with the capacity, if the number of objects to be added are known. Slide 61: 3/3/2011 TPG Confidential 61 Guidelines for Optimization - Maps: WeakHashMap is a special purpose map which uses an internal hashtable. When there are no more references to key object except weak reference maintained by WeakHashMap, the garbage collector reclaims the key object and mapping between key object and value object is also reclaimed, if the value object does not have any other references then the value object is also reclaimed by the garbage collector . HashMap and WeakHashMap are not synchronized whereas Hashtable is synchronized. If the collection to be thread safe use Hashtable, otherwise use HashMap. In general, HashMap gives better performance than Hashtable because of non-synchronized methods. A TreeMap provides an efficient means of storing key/ value pairs in sorted order, and allows rapid retrieval. This can be used when the collection needs to be ordered in a specified order. Slide 62: 3/3/2011 TPG Confidential 62 Guidelines for Optimization - Algorithms: Consider the scaling characteristics of the algorithms for tuning. Consider the possibility of switching to one of the alternative algorithms rather than tuning the algorithm that is already being used. Test if changing a recursive algorithm to an iterative algorithm provides an useful speedup. In general, Add caching to collections where some elements are accessed more often. Consider wrapping the elements of the collection in a specialized class that improves access times. Slide 63: 3/3/2011 TPG Confidential 63 Threading – Optimization Brief Introduction on Threading: Multithreading allows an application to do multiple things are the same time. Single threading uses even looping or polling and wastes CPU time. Multithreading helps to avoid wastage of CPU cycles as the polling is eliminated. One thread can pause without stopping other parts of the program. In Java, the priorities can be set for the threads. Slide 64: 3/3/2011 TPG Confidential 64 Guidelines for Optimization - Threading: Keep the user-interface in a separate thread from other work so that the application feels more responsive. Avoid designs and implementations that force points of serialized execution. Parallelize tasks with threads to speed up the calculations. Avoid synchronizing methods of stateless objects. Use thread pools to reuse threads if many threads are needed or if threads are needed for very short tasks. Avoid locking more resources than necessary. When tuning threads it is easy to make changes, but my end up with total confusion and deadlock. Before start tuning threads, it is important to have a good understanding of how they interact, how to make them cooperate and control each other. Slide 65: 3/3/2011 TPG Confidential 65 Guidelines for Effective Usage of Size: Controlling .class File Size: Java compilers generate .class file containing bytecode for the methods. .class file has various attributes. The Code attribute contains actual bytecodes for methods, SourceFile information on the source file name used to generate this file, LineNumberTable mapping information between bytecode offsets and source file line numbers, and LocalVariableTable mapping information between stack frame offsets and local variable names. Various options are: javac Code, SourceFile, LineNumberTable javac –g Code, SourceFile, LineNumberTable, LocalVariableTable javac -g:none Code Slide 66: 3/3/2011 TPG Confidential 66 Controlling .class File Size: The option javac -g:none saves about 20% space over the default javac. The option javac –g takes about 20% more space than the default javac. If the option with none is used, the debugging and exception reporting becomes difficult without the line numbers. Using SoftReference: java.lang.ref.SoftReference is a relatively new class, used to implement smart caches. Slide 67: 3/3/2011 TPG Confidential 67 Using SoftReference: java.lang.ref.SoftReference is a relatively new class, used to implement smart caches. The garbage collection system in the Java Virtual Machine knows about soft references, and it will clear the object reference referred to by a soft reference, before throwing an OutOfMemoryError exception. Example: Object obj = new char[1000000]; SoftReference ref = new SoftReference(obj); Slide 68: 3/3/2011 TPG Confidential 68 Guidelines for Effective Usage of Size: Using SoftReference: If the referent has other references to it, the garbage collector will not clear it. (i.e) If the only reference to an object is via a SoftReference object, and the object’s memory is needed elsewhere in the program, then the object is subject to being cleared by the garbage collector. This scheme can be used to implement various types of caches, where objects are kept around as long as possible, but cleared if memory is tight. Slide 69: 3/3/2011 TPG Confidential 69 Guidelines for Effective Usage of Size: Using BitSet: BitSet is a class used to represent arrays of bits efficiently. BitSet differs from a primitive array of boolean in two ways: A BitSet object is dynamic, that is, the set of bits can be added to at any time. A BitSet is at least potentially more space efficient, in that an array of boolean may in fact be implemented as an array of byte, using 8 bits per value instead of 1. Example: BitSet b = new BitSet(); b.set(37); b.set(59); System.out.println(b); Output is: {37,59} Slide 70: 3/3/2011 TPG Confidential 70 Guidelines for Effective Usage of Size: Using Sparse Arrays: Suppose a large array that needs to be stored, but it’s so large that obtaining a contiguous block of space for it, or perhaps it’s large but sparse (few actual elements are set), and a more efficient way of storing the array is Sparse Arrays. The implementation of SparseArrayList is given in the following slides. The class SparseArrayList extends the collection framework abstract class AbstractList, and thereby provides a standard List interface to the user Slide 71: 3/3/2011 TPG Confidential 71 Guidelines for Effective Usage of Size: Using Sparse Arrays: SparseArraylist Class Implementation is embedded in the below document: Example for implementing SparseArrayList is embedded below. It sets array slots at random, then checks whether the slot that has been set has the appropriate value in it Slide 72: 3/3/2011 TPG Confidential 72 Guidelines for Optimization - Miscellaneous: Recursive Methods: Convert recursive methods to use iteration instead Convert recursive methods to use tail recursion. A tail-recursive function is a recursive function for which each recursive call to itself is a reduction of the original call. Use temporary variables in place of passed parameters to convert a recursive method using a single search path into an iterative methods. Casts: Avoid casting by creating and using type specific collection classes. Casts: Type variables as precisely as possible Use temporary variable of cast type, instead of repeatedly casting. Variables: Use local variables and method argument variables rather than instance or static variable for faster manipulation. Use ints in preference to any other data type. Use primitive data types instead of objects for temporary variables. Slide 73: 3/3/2011 TPG Confidential 73 Guidelines for Optimization - Miscellaneous: Inner Classes: An interesting situation with method call overhead arises when inner classes are used. The inner class specification says that a private method of a class A can be used by a class B, if A encloses B. That is, if B is a class defined within A, it can call A’s private methods. This overhead can be avoided by not using private members in a class, assuming the class has inner classes that use those members. But avoiding the use of private members should not affect the obvious usage of private members. Convert any loops using native method calls to a native call that includes a loop, instead of running the loop in java. Loop iteration parameters should be passed to the native call. Deliver the classes in uncompressed format in JAR files, unless network download is significant. Slide 74: 3/3/2011 TPG Confidential 74 Chapter 4 : Reasons for not Optimizing Slide 75: 3/3/2011 TPG Confidential 75 If the existing code already works fine, there is possibility of introducing new defects by optimizing the code. Optimization tends to make code harder to understand and maintain. Some of the techniques presented here increase speed by reducing the extensibility of the code Optimizing code for one platform may actually make it worse on another platform A lot of time can be spent optimizing, with little gain in performance, and can result in obfuscated code Slide 76: 3/3/2011 TPG Confidential 76 References: precisejava.com J2EE Performance Optimization – Wipro Material Javaworld.com Thirty Ways to Improve the Performance of Your Java™ Programs - Glen McCluskey You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
Level 2.3 CBT course on Core Java Fundamentals - Performance Optimizat kaslijain Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 51 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: March 03, 2011 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Slide 1: 3/3/2011 TPG Confidential 1 Technology Practices Group Java Competency Framework "Incubate, Nurture and Deploy Technology Experts Innovate, Build and Deliver Technology Solutions” Slide 2: 3/3/2011 TPG Confidential 2 Introduction Revision History : 3/3/2011 TPG Confidential 3 Revision History Slide 4: 3/3/2011 TPG Confidential 4 Table of Contents Introduction To Performance Optimization Optimization in Java Areas of optimization in Java Reasons Not to Optimize References Slide 5: 3/3/2011 TPG Confidential 5 Chapter 1 : Introduction To Performance Optimization Slide 6: 3/3/2011 TPG Confidential 6 What is performance ? Performance is the way a computer system behaves given a particular workload. Performance is measured in terms of system response time, throughput and availability. Performance is also affected by: - The resources available in the system - How well those resources are used and shared. Slide 7: 3/3/2011 TPG Confidential 7 What is performance Tuning? Performance Tuning is the technique to improve the performance of application. It is easier than debugging, focus on the bottlenecks. A good round of Performance Tuning makes an application run fast. In general, the system is tuned to improve its cost-benefit ratio. Slide 8: 3/3/2011 TPG Confidential 8 Reasons for Performance Optimization Any application needs considerable amount of following resources for its execution. CPU Memory Network bandwidth. Sometimes there may be very limited resources and the program execution will fail. The resources are consumed at a larger pace because of the inefficient code . Slide 9: 3/3/2011 TPG Confidential 9 Reasons (Contd) To use the available resources efficiently. Most of the business applications involve multi users operations and the time to perform any activity should be minimal as much as possible. To obtain faster system response times or higher throughput without increasing the processing costs. . Slide 10: 3/3/2011 TPG Confidential 10 Reasons (Contd) Code fragments that occur multiple times throughout a program are likely to be size-sensitive, while code with many execution iterations may be speed-sensitive . To add more users in the system at later stages. Writing optimized working code rather than working code became mandatory to be successful in the current competitive world. Slide 11: 3/3/2011 TPG Confidential 11 Chapter 2 : Optimization in Java Slide 12: 3/3/2011 TPG Confidential 12 Optimization in Java Java performance tuning means optimizing code for speed, reliability, scalability and maintainability. It is required to find out which parts of the applications represent bottlenecks and which are behaving efficiently. Once bottlenecks are identified, tuning can be done on various elements to improve the performance of the application. Slide 13: 3/3/2011 TPG Confidential 13 Chapter 3 : Areas of Optimization in Java Slide 14: 3/3/2011 TPG Confidential 14 Areas of Optimization in Java In a typical java based application, the various areas where performance can be optimized are: Object creation Loops String Manipulation IO Serialization Exceptions Collections Threading Effective Usage of Size Slide 15: 3/3/2011 TPG Confidential 15 Object Creation - Optimization Brief Introduction on objects: Objects are instances of the class. Class contains data and methods to manipulate the data. There are two steps to create the objects of a class. They are: Declare a variable of class type. Create a physical copy of an object using ‘new’ operator. ‘new’ operator dynamically (during runtime) allocates space in memory for an object Slide 16: 3/3/2011 TPG Confidential 16 Brief Introduction on objects (contd): We can create as many objects as it needs during the execution of time. There is a possibility that memory is not sufficient during run time and run-time exception will occur. Whenever an object is created, the following steps are involved: Memory is allocated to all the variables Memory allocated to all super class variables All variables – super class, sub class are initialized The constructor is invoked Slide 17: 3/3/2011 TPG Confidential 17 Brief Introduction on objects (contd): Whenever an object is created all the above steps are repeated and consume considerable resources. So decision to create new object plays a crucial role for memory management. In memory all the objects are placed in heap and their address in the heap are stored in stack. All primitive data types are stored in the stack Slide 18: 3/3/2011 TPG Confidential 18 Bottlenecks in Object Creation: Memory leaks by holding on to objects, without releasing references. This stops the garbage collector from reclaiming those objects. Java does not support creation of large number of objects simultaneously. Creating objects, Garbage Collection and memory recycling cost more time and CPU effort. Slide 19: 3/3/2011 TPG Confidential 19 Guidelines for Optimization - Objects Creation: Avoid creation of objects in loops Avoid creating objects in frequently used routines. Try to use String literals, instead of String objects created using ‘new’ keyword. Example: Use String str = “Optimization”; Intead of String str = new String(“Optimization”); Pre-size a Collection to its largest potential size to reduce the number of objects created. Slide 20: 3/3/2011 TPG Confidential 20 Guidelines for Optimization - Objects Creation: Use primitive datatype instead of using wrapper classes. Using primitive datatype speeds up garbage collection. For example an object with just one instance variable holding an int is reclaimed in one object reclaim, whereas if it holds an Integer object, the garbage collector needs to reclaim two objects. As accessing local variables are faster than accessing class variables, use local variables instead of using class variables whenever possible. Slide 21: 3/3/2011 TPG Confidential 21 Guidelines for Optimization - Objects Creation: Make use of static variables, when multiple instances of a class need to access a particular entity. Avoid initializing instance variables more than once. Whenever the object use is completed, make the reference of the object as null so that the object becomes eligible for garbage collection . Empty Collection objects before reusing them. Slide 22: 3/3/2011 TPG Confidential 22 Guidelines for Optimization - Objects Creation: Reduce the number of temporary objects being used, especially in loops. Keep constructors simple and inheritance hierarchies shallow Reuse Exception instances, when a stack trace is required. Replace constant objects with integers (enumerating constants), which can provide both speed and memory as enumeration requires less memory than the equivalent Strings. Slide 23: 3/3/2011 TPG Confidential 23 Guidelines for Optimization - Objects Creation: Use lazy evaluation techniques. In lazy evaluation technique, the computations will be done only when it is necessary. By doing this, some computations may not be done at all when a scenario is executed . Similarly, use lazy objects creation. In this the objects should be created before they are going to be used. By this way, lot of memory can be saved. Slide 24: 3/3/2011 TPG Confidential 24 Loops - Optimization Brief Introduction on Loops: The Java programming language has three categories of control statements . They are: Selection statements Iteration statements Jump Statements Iteration statements are used to execute the same set of code as many times as required. Thus iteration statements are called loops . Slide 25: 3/3/2011 TPG Confidential 25 Brief Introduction on Loops (Contd): Java has three types of loops . They are: for while do-while. “for” loop is used when the number of iterations required is known in advance. “while” loop is used when the number of iterations required is not known. In “while” loop, the condition is checked for each time execution and body of the loop is executed if the condition is true. Slide 26: 3/3/2011 TPG Confidential 26 Brief Introduction on Loops (Contd): The “do-while” loop is used when the loop needs to get executed atleast once. As loops are extensively used in all kinds of applications, these statements have considerable impact on the performance of the application. Slide 27: 3/3/2011 TPG Confidential 27 Guidelines for Optimization - Loops: Use the int data type as loop index variable instead of using byte or short data types, as short and byte data types will be converted as int data type by implicit type cast . Try to use the fastest tests in loops Always avoid anything that can be done outside of the loop like method calls, assigning values to variables, or testing for conditions. Eliminate unneeded temporary variables from loops. Slide 28: 3/3/2011 TPG Confidential 28 Guidelines for Optimization - Loops: Avoid using method calls in loops for conditional statement as this is expensive. Instead use a temporary variable to store the value and test the terminating condition. Example: instead of writing, for (int i=0 ; i < s.length(); i++) { charc c = s.charAt(i); } write it as , int j =str.length(); for (int i =0 ; i < j; i++) { charc c =s.charAt(i); } By doing this, an extra overhead on conditional statement is removed. Slide 29: 3/3/2011 TPG Confidential 29 Guidelines for Optimization - Loops: Use short circuit operators if there is more than one conditional statement in loops. If there are only || (or) operators, put the expression that is most likely to evaluate to true at the extreme left. If there is any && (and) operator, put the expression that is most likely to evaluate false at the extreme left. This helps to avoid all the following expressions being tested. Avoid method calls in loops when possible, even if this requires rewriting or inlining. Slide 30: 3/3/2011 TPG Confidential 30 Guidelines for Optimization - Loops: To copy the elements from one array to another, use System.arraycopy() instead of using loops . Example: Public Class TestArrayCopy { public static void main(String s[]) { //Declaring Variables long startTime, endTime; int[] a = new int[2500000]; int[] b = new int[2500000]; //Initializing array for (int i = 0; i < a.length; i++) { a[i] = i; } Slide 31: 3/3/2011 TPG Confidential 31 Arraycopy Example (contd); //Copying using loop startTime = System.currentTimeMillis(); for (int j = 0; j < a.length; j++) { b[j] = a[j]; } endTime = System.currentTimeMillis(); System.out.println(endTime - startTime + " milli seconds for loop copy "); } } The output of the above is: 32 milli seconds for loop copy 15 milli seconds for System.arraycopy() Slide 32: 3/3/2011 TPG Confidential 32 Guidelines for Optimization - Loops: Do not use exception handling inside loops. (i.e) Do not place using try-catch inside the loops instead place the loops inside the try-catch for better performance . Example: Instead of writing, String s = "Learning Java"; for(int i=0; i< 10; i++){ try{ System.out.println("Character in s at:" + i +":" + s.charAt(i)); } catch(Exception exp){ } } Slide 33: 3/3/2011 TPG Confidential 33 Exception Handling Example (contd); Write it as, for(int i=0; i< 10; i++){ System.out.println("Character in s at:" + i +":" + s.charAt(i)); } } catch(Exception exp){ } Slide 34: 3/3/2011 TPG Confidential 34 Strings – Optimization Brief Introduction on Strings: String objects are immutable objects and StringBuffer objects are mutable objects. Mutable objects can be modified after their creation whereas immutable objects cannot be modified after creation. Both String and StringBuffer classes can not be sub classed. Slide 35: 3/3/2011 TPG Confidential 35 Guidelines for Optimization - Strings: There are two ways to create the Strings. They are: String S1 = “Optimization” String S2 = “Optimization” String S1 = new String(“Optimization”) String S2 = new String(“Optimization”) As creating strings with new keyword is expensive, the first method is better method for creating string with respect to performance. Slide 36: 3/3/2011 TPG Confidential 36 Guidelines for Optimization - Strings: There are three ways to concatenate multiple strings. They are: + operator String.concat() method StringBuffer.append() The choice to concatenate strings should be made based on the scenario. If the values are known at the compile time, use ‘+’ operator for string concatenation. This is called compile time resolution. Slide 37: 3/3/2011 TPG Confidential 37 Guidelines for Optimization - Strings: If the values are not known at the compile time and available at compile time, use StringBuffer concatenation for string concatenation. This is called runtime resolution. Manipulate characters in char arrays rather than String and StringBuffer manipulation. Avoid using the StreamTokenizer. Slide 38: 3/3/2011 TPG Confidential 38 Guidelines for Optimization - Strings: Set the initial capacity of the StringBuffer using its constructor. StringBuffer maintains a character array internally. When the StringBuffer is created with default constructor without length, the StringBuffer is initialized with 16 characters. When the StringBuffer reaches its maximum capacity, the size of it will be increased by twice the size plus 2. Thus if default size is used initially and go on adding characters, then it increases its size by 34(2*16 +2) after it adds 16th character and it increases its size by 70(2*34+2) after it adds 34th character. Slide 39: 3/3/2011 TPG Confidential 39 Guidelines for Optimization - Strings: Whenever it reaches its maximum capacity it has to create a new character array and recopy old and new characters. It is obviously expensive. So it is always good to initialize with proper size that gives very good performance. Logically partition strings into that require internationalization support and those that don’t need internationalization. The later can be replaced for tuning with char arrays. Slide 40: 3/3/2011 TPG Confidential 40 Guidelines for Optimization - Strings: Interning Strings for String comparison: If the == operator is used to compare the strings, it will return true if the string references are same. If the references are same those objects are identical. The String class has a method intern() to create an internal pool of unique strings. Example: str = str.intern() Interned strings can be compared with each other using ==, which is much cheaper than equals() method. String literals are always interned. Slide 41: 3/3/2011 TPG Confidential 41 Input/Output – Optimization Brief Introduction on I/O: Input and Output streams are used read from and write to devices such as file or network or console. A stream is a logical entity that either produces or consumes information. java.io package provides I/O classes to manipulate streams. This package supports two types of streams - binary streams which handle binary data and character streams which handle character data InputStream and OutputStream are high level interfaces for manipulating binary streams. Reader and Writer are high level interfaces for manipulating character streams Disk and network transfers are expensive activities and are two of the most likely problems for performance. Slide 42: 3/3/2011 TPG Confidential 42 Input/Output – Optimization Relationship between Streams: Slide 43: 3/3/2011 TPG Confidential 43 Guidelines for Optimization - I/O: Two standard optimization techniques for reducing I/O overhead are: buffering and caching. Buffering groups the data into larger chunks, improving the efficiency of the I/O by reducing the no. of I/O operations. Caching the objects or data are accessed repeatedly can replace an I/O call with a hugely faster memory access. By default, most of the streams read or write one byte at a time. Reading and writing data in small chunks can be very slow. That is, it takes lot of time to read/write byte by byte when the large amounts of data needs to be handled. Slide 44: 3/3/2011 TPG Confidential 44 Guidelines for Optimization - I/O: I/O provides Buffered streams to override this byte by byte default behaviors. Use BufferedInputStream and BufferedOutputStream to batch requests to improve performance. Example: FileInputStream facilitates reading from a file and FileOutputStream facilitates writing to a file. These classes have two types of methods, one to read/write one byte at a time and the other method to read/write array of bytes. Slide 45: 3/3/2011 TPG Confidential 45 Guidelines for Optimization - I/O: Methods for FileInputStream: FileInputStream.read(); FileInputStream.read(byte[] b); FileInputStream.read(byte[] b, int off, int length); Methods for FileOutPutStream FileInputStream.read(); FileInputStream.read(byte[] b); FileInputStream.read(byte[] b, int off, int length); Methods for BufferedInputStream BufferedInputStream.read(); BufferedInputStream.read(byte[] b, int off, int len); Slide 46: 3/3/2011 TPG Confidential 46 Guidelines for Optimization - I/O: Methods for BufferedOutputStream BufferedOutputStream.write(int b); BufferedOutputStream.write(byte[] b, int off, int len); The following figure shows how buffered streams divert the data flow Source File FileInputStream BufferedInputStream Destination File FileOutputStream BufferedOutputStream Slide 47: 3/3/2011 TPG Confidential 47 Guidelines for Optimization - I/O: The default size for buffer streams is 512 bytes. It accumulates the data till it reaches this capacity and reads from or write to destination. The default size can be customized by passing required buffer size through constructor by: BufferedInputStream(InputStream in, int size); BufferedOutputStream(OutputStream out, int size); In some situations custom buffering techniques can be used to maximize performance. It means creating our own buffer while reading or writing streams using existing methods. Execute I/O in the background thread. Avoid or reduce I/O calls in loops. Keep files open and navigate around them rather than repeatedly opening and closing files. Replace System.out and System.err with customized PrintStream classes to control Console output. Slide 48: 3/3/2011 TPG Confidential 48 Guidelines for Optimization - I/O: Avoid the use of System.out.println: System.out.println synchronizes processing for the duration of disk I/O, and that can slow throughput significantly As much as possible, avoid the use of this. Eventhough sophisticated debugging tools are available, sometimes System.out.println remains useful for tracing purpose, or for error and debugging situations. The application should be configured so that it gets turn on during error and debugging situations only. This can be done by using a final Boolean variable. This helps to optimize both the check and execution of the tracing at compile time. Optimize network transfers by transferring only the data and objects needed and no more. Avoid compression when the system has a heavily loaded CPU. Consider searching directly, against compressed data without decompressing. Slide 49: 3/3/2011 TPG Confidential 49 Serialization - Optimization Brief Introduction on serialization: Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket. Deserialization is the process of reading back that serialized java object stream from input stream. All primitive data types and some of standard java API classes are serializable A java object is serializeable and deserializeable if that class follows the following rules The java class must implement java.io.Serializable interface or java.io.Externalizable interface or inherit that implementation from any one of it's super class implementation. All instance variables of that class must implement Serializable interface or Externalizable interface or inherit from one of it's super class Serialization process ignores class (static) variables. Slide 50: 3/3/2011 TPG Confidential 50 Brief Introduction on serialization: Serializable interface is a marker interface and does not have any methods. All major java technologies like RMI, EJB are based on serialization process to pass the objects through network. These technologies implicitly do all the serialization work. When you write or read an object to a file or network or other stream using serialization process, it writes/reads the complete object state. i.e. it writes the object, it's instance variables, and super class instance variables except transient variables and class (static) variables. Serializable interface is a marker interface and does not have any methods. All major java technologies like RMI, EJB are based on serialization process to pass the objects through network. These technologies implicitly do all the serialization work. When you write or read an object to a file or network or other stream using serialization process, it writes/reads the complete object state. i.e. it writes the object, it's instance variables, and super class instance variables except transient variables and class (static) variables. Slide 51: 3/3/2011 TPG Confidential 51 Guidelines for Optimization - Serialization: Serialization can be very costly. Using the transient keyword reduces the amount of data serialized. Because the variables having transient access modifier will not be read from or written to streams. It boost performance by avoiding unnecessary data into streams. Spread the de-serialization overhead to other times. Handle serializing explicitly, rather than using default serialization mechanisms. Slide 52: 3/3/2011 TPG Confidential 52 Exceptions – Optimization Brief Introduction on Exceptions: Java provides an efficient way to handle unexpected conditions that can occur in the program. When there is an error or bug in the program then the program terminates as soon as an error is encountered leaving the program in an in consistent state. To avoid this Java makes use of Exception Handling mechanism to a great advantage so that when ever there is an exceptional condition then the program handles it gracefully and continues program execution . The whole concept of exceptions/errors is handled by the java.lang.Throwable class . Throwable class has two subclasses. They are: Error and Exception The application should handle all the Exceptions whereas Errors are normally handled by JVM. Exceptions are of two types -Checked exceptions and Unchecked exceptions Checked exceptions can either be declared in the throws clause or caught in the catch block. Unchecked exceptions need not be declared in the throws clause but can be caught in the catch clause Slide 53: 3/3/2011 TPG Confidential 53 Guidelines for Optimization - Exceptions: try-catch blocks generally use no extra time, if no exception is thrown. But, throwing an exception and executing the catch block has a significant overhead. This overhead seems to be due mainly to the cost of getting a snapshot of the stack when the exception is created. Do not use Exception handling for anything other than exception handling like to control the flow of the program. Avoid using generic class Exception in the catch block. Use specific exception classes in the catch block. Avoid using generic class Exception in the throws clause. Use specific exception classes in the throws clause. Consider throwing exceptions without generating a stack trace by reusing a previously created instance. The disadvantage of reusing an exception instance is that the instance does not have the stack trace. Always use the finally block to release the resources like a database connection, closing a file or socket connection etc. This prevents resource leaks even if an exception occurs . Slide 54: 3/3/2011 TPG Confidential 54 Guidelines for Optimization - Exceptions: Use instanceof instead of making speculative class casts in a try-catch block. Handle Exceptions locally always. i.e. When using method calls always handle the exceptions in the method where they occur, do not allow them to propagate to the calling method unless it is specifically required. It is efficient to handle them locally since allowing them to propagate to the calling method takes more execution time. Do not use Exception handling in loops. It is better to place loops inside try/catch blocks than vice versa. Here is an code snippet that gives bench mark. Slide 55: 3/3/2011 TPG Confidential 55 Collections Framework – Optimization Brief Introduction on Collections: Collection is a group of objects. The framework provides highly efficient fundamental collections such as dynamic arrays, linked lists, trees and hash tables. This framework helps to all types of collections to work in a similar manner. java.util package provides important types of collections. There are two fundamental types collections. They are Collection and Map. Collection helps to maintain a group of objects. Example for Collection type: Lists and sets Map helps to maintain a group of objects with the pair value. Example for Map type: Hashmap, Hashtable Lists: These are used to maintain an ordered collection of objects. Arraylist, LinkedList, Vector and Stack are some of the list implementation classes Slide 56: 3/3/2011 TPG Confidential 56 Brief Introduction on Collections: The common operation in the lists are: Adding objects, removing objects, accessing objects and iterating through the list. Sets: It is a collection of unique objects. It doesn’t allow duplicate objects and modification of existing objects. The common operation in the sets are: Adding objects, removing objects, accessing objects and iterating through the list. But the objects can not be modified. HashSet and TreeSet are the Set implementation classes. Maps: It is a collection of key and value object associations. The common operation in the maps are: Adding, removing and accessing key value pairs. HashMap, Hashtable, WeakHashMap and TreeMap are the Map implementation classes. HashMap, Hashtable and WeakHashMap have similar implementations. TreeMap is meant for sorted collection. Slide 57: 3/3/2011 TPG Confidential 57 Guidelines for Optimization - Collections: The optimization strategy is different for each class mentioned above based on the specific operations. The strategy depends upon the below options: Thread Safe Collection Size of collection and Type of operation – adding, removing, accessing or iterating The following slides have the strategy for each type of Collections based on the above options. Slide 58: 3/3/2011 TPG Confidential 58 Guidelines for Optimization - Lists: Thread Safe Lists: Use Vector and Stack to have the list as thread safe as both have synchronized methods. ArrayList and LinkedList are not thread safe. If the collection need not be thread safe, use ArrrayList for accessing and iterating objects and use LinkedList for adding and removing objects. ArrayList with initialization gives better performance than others because its methods are non-synchronized. Synchronized methods are bit expensive because JVM has to lock the objects whenever it finds synchronized methods. ArrayList and Vector maintain internal Object array. Whenever an object is added, it is added at the end of list. If the object needs to be added any other position, it creates a new object array and recopies all the objects which is expensive. This is the reason for taking more time when the objects are added at the beginning or middle of the list. LinkedList gives good performance when adding elements at the end and beginning but it is worse when adding objects at middle because it needs to scan the node whenever it needs to add an object. LinkedList cannot be initialized. Slide 59: 3/3/2011 TPG Confidential 59 Guidelines for Optimization - Lists: LinkedLists give better performance when the objects are removed from the beginning. All lists give same performance when the objects are removed from the end. When the objects are removed from the middle, use ArrayList or Vector as LinkedList takes more time than the ArrayList or Vector. Use ListIterator than Iterator and Enumeration for List types. Conclusion: Thread safe Lists : Vector and Stack Not Thread Safe Lists: ArrayList (Accessing and Iterating), LinkedList(Adding and Removing) Slide 60: 3/3/2011 TPG Confidential 60 Guidelines for Optimization - Sets: HashSet gives better performance than TreeSet because TreeSet is an ordered collection of objects. Whenever an object is inserted in the TreeSet, the objects are sorted whereas the objects in HashSet are added in the adhoc manner. All the operations in the TreeSet takes time as it has to compare and sort. Use HashSet and convert that to TreeSet after all the operations for better performance. Initialize HashSet or TreeSet with the capacity, if the number of objects to be added are known. Slide 61: 3/3/2011 TPG Confidential 61 Guidelines for Optimization - Maps: WeakHashMap is a special purpose map which uses an internal hashtable. When there are no more references to key object except weak reference maintained by WeakHashMap, the garbage collector reclaims the key object and mapping between key object and value object is also reclaimed, if the value object does not have any other references then the value object is also reclaimed by the garbage collector . HashMap and WeakHashMap are not synchronized whereas Hashtable is synchronized. If the collection to be thread safe use Hashtable, otherwise use HashMap. In general, HashMap gives better performance than Hashtable because of non-synchronized methods. A TreeMap provides an efficient means of storing key/ value pairs in sorted order, and allows rapid retrieval. This can be used when the collection needs to be ordered in a specified order. Slide 62: 3/3/2011 TPG Confidential 62 Guidelines for Optimization - Algorithms: Consider the scaling characteristics of the algorithms for tuning. Consider the possibility of switching to one of the alternative algorithms rather than tuning the algorithm that is already being used. Test if changing a recursive algorithm to an iterative algorithm provides an useful speedup. In general, Add caching to collections where some elements are accessed more often. Consider wrapping the elements of the collection in a specialized class that improves access times. Slide 63: 3/3/2011 TPG Confidential 63 Threading – Optimization Brief Introduction on Threading: Multithreading allows an application to do multiple things are the same time. Single threading uses even looping or polling and wastes CPU time. Multithreading helps to avoid wastage of CPU cycles as the polling is eliminated. One thread can pause without stopping other parts of the program. In Java, the priorities can be set for the threads. Slide 64: 3/3/2011 TPG Confidential 64 Guidelines for Optimization - Threading: Keep the user-interface in a separate thread from other work so that the application feels more responsive. Avoid designs and implementations that force points of serialized execution. Parallelize tasks with threads to speed up the calculations. Avoid synchronizing methods of stateless objects. Use thread pools to reuse threads if many threads are needed or if threads are needed for very short tasks. Avoid locking more resources than necessary. When tuning threads it is easy to make changes, but my end up with total confusion and deadlock. Before start tuning threads, it is important to have a good understanding of how they interact, how to make them cooperate and control each other. Slide 65: 3/3/2011 TPG Confidential 65 Guidelines for Effective Usage of Size: Controlling .class File Size: Java compilers generate .class file containing bytecode for the methods. .class file has various attributes. The Code attribute contains actual bytecodes for methods, SourceFile information on the source file name used to generate this file, LineNumberTable mapping information between bytecode offsets and source file line numbers, and LocalVariableTable mapping information between stack frame offsets and local variable names. Various options are: javac Code, SourceFile, LineNumberTable javac –g Code, SourceFile, LineNumberTable, LocalVariableTable javac -g:none Code Slide 66: 3/3/2011 TPG Confidential 66 Controlling .class File Size: The option javac -g:none saves about 20% space over the default javac. The option javac –g takes about 20% more space than the default javac. If the option with none is used, the debugging and exception reporting becomes difficult without the line numbers. Using SoftReference: java.lang.ref.SoftReference is a relatively new class, used to implement smart caches. Slide 67: 3/3/2011 TPG Confidential 67 Using SoftReference: java.lang.ref.SoftReference is a relatively new class, used to implement smart caches. The garbage collection system in the Java Virtual Machine knows about soft references, and it will clear the object reference referred to by a soft reference, before throwing an OutOfMemoryError exception. Example: Object obj = new char[1000000]; SoftReference ref = new SoftReference(obj); Slide 68: 3/3/2011 TPG Confidential 68 Guidelines for Effective Usage of Size: Using SoftReference: If the referent has other references to it, the garbage collector will not clear it. (i.e) If the only reference to an object is via a SoftReference object, and the object’s memory is needed elsewhere in the program, then the object is subject to being cleared by the garbage collector. This scheme can be used to implement various types of caches, where objects are kept around as long as possible, but cleared if memory is tight. Slide 69: 3/3/2011 TPG Confidential 69 Guidelines for Effective Usage of Size: Using BitSet: BitSet is a class used to represent arrays of bits efficiently. BitSet differs from a primitive array of boolean in two ways: A BitSet object is dynamic, that is, the set of bits can be added to at any time. A BitSet is at least potentially more space efficient, in that an array of boolean may in fact be implemented as an array of byte, using 8 bits per value instead of 1. Example: BitSet b = new BitSet(); b.set(37); b.set(59); System.out.println(b); Output is: {37,59} Slide 70: 3/3/2011 TPG Confidential 70 Guidelines for Effective Usage of Size: Using Sparse Arrays: Suppose a large array that needs to be stored, but it’s so large that obtaining a contiguous block of space for it, or perhaps it’s large but sparse (few actual elements are set), and a more efficient way of storing the array is Sparse Arrays. The implementation of SparseArrayList is given in the following slides. The class SparseArrayList extends the collection framework abstract class AbstractList, and thereby provides a standard List interface to the user Slide 71: 3/3/2011 TPG Confidential 71 Guidelines for Effective Usage of Size: Using Sparse Arrays: SparseArraylist Class Implementation is embedded in the below document: Example for implementing SparseArrayList is embedded below. It sets array slots at random, then checks whether the slot that has been set has the appropriate value in it Slide 72: 3/3/2011 TPG Confidential 72 Guidelines for Optimization - Miscellaneous: Recursive Methods: Convert recursive methods to use iteration instead Convert recursive methods to use tail recursion. A tail-recursive function is a recursive function for which each recursive call to itself is a reduction of the original call. Use temporary variables in place of passed parameters to convert a recursive method using a single search path into an iterative methods. Casts: Avoid casting by creating and using type specific collection classes. Casts: Type variables as precisely as possible Use temporary variable of cast type, instead of repeatedly casting. Variables: Use local variables and method argument variables rather than instance or static variable for faster manipulation. Use ints in preference to any other data type. Use primitive data types instead of objects for temporary variables. Slide 73: 3/3/2011 TPG Confidential 73 Guidelines for Optimization - Miscellaneous: Inner Classes: An interesting situation with method call overhead arises when inner classes are used. The inner class specification says that a private method of a class A can be used by a class B, if A encloses B. That is, if B is a class defined within A, it can call A’s private methods. This overhead can be avoided by not using private members in a class, assuming the class has inner classes that use those members. But avoiding the use of private members should not affect the obvious usage of private members. Convert any loops using native method calls to a native call that includes a loop, instead of running the loop in java. Loop iteration parameters should be passed to the native call. Deliver the classes in uncompressed format in JAR files, unless network download is significant. Slide 74: 3/3/2011 TPG Confidential 74 Chapter 4 : Reasons for not Optimizing Slide 75: 3/3/2011 TPG Confidential 75 If the existing code already works fine, there is possibility of introducing new defects by optimizing the code. Optimization tends to make code harder to understand and maintain. Some of the techniques presented here increase speed by reducing the extensibility of the code Optimizing code for one platform may actually make it worse on another platform A lot of time can be spent optimizing, with little gain in performance, and can result in obfuscated code Slide 76: 3/3/2011 TPG Confidential 76 References: precisejava.com J2EE Performance Optimization – Wipro Material Javaworld.com Thirty Ways to Improve the Performance of Your Java™ Programs - Glen McCluskey