Thirty Ways to Improve the Performance of Your Java™ Programs
Total Page:16
File Type:pdf, Size:1020Kb
Thirty Ways to Improve the Performance of Your Java™ Programs Glen McCluskey E-mail: [email protected] Version 1.0, October 1999 Copyright © 1999 Glen McCluskey & Associates LLC. All Rights Reserved. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. Throughout this paper, when the Java trademark appears alone it is a reference to the Java programming language. When the JDK trademark appears alone it is a reference to the Java Development Kit. The author would like to thank Steve Buroff, Craig Hondo, John Spicer, and Clay Wilson for assistance with proofreading. Thirty Ways to Improve the Performance of Your Java™ Programs Contents 1 Introduction........................................................................................................................................... 4 1.1 When to Worry About Performance................................................................................................ 4 1.2 Performance Issues Not Covered in This Paper ............................................................................. 4 1.3 Just-in-Time Compilers and Java Virtual Machines....................................................................... 4 1.4 Environment and Tools Used in Code Examples............................................................................ 5 1.5 How Examples Were Timed ............................................................................................................ 5 1.6 Performance Analysis Tools ........................................................................................................... 6 1.7 Web Site .......................................................................................................................................... 7 2 Classes.................................................................................................................................................... 8 2.1 Default Constructors....................................................................................................................... 8 2.2 Constructor Hierarchies ................................................................................................................. 9 2.3 Class and Instance Initialization..................................................................................................... 9 2.4 Recycling Class Instances............................................................................................................. 10 3 Methods ............................................................................................................................................... 12 3.1 Inlining.......................................................................................................................................... 12 3.2 Final Methods............................................................................................................................... 13 3.3 Synchronized Methods .................................................................................................................. 13 3.4 Inner Classes................................................................................................................................. 14 4 Strings .................................................................................................................................................. 15 4.1 Strings are Immutable................................................................................................................... 15 4.2 Accumulating Strings Using char[] Arrays .................................................................................. 16 4.3 Using == and String.equals() to Compare Strings....................................................................... 17 4.4 Interning Strings ........................................................................................................................... 17 4.5 Obtaining the Length of a String................................................................................................... 18 4.6 Using toCharArray()..................................................................................................................... 19 4.7 Converting Strings to Numbers..................................................................................................... 20 5 Input and Output ................................................................................................................................ 22 5.1 Buffering ....................................................................................................................................... 22 5.2 BufferedReader ............................................................................................................................. 23 2 Thirty Ways to Improve the Performance of Your Java™ Programs 5.3 Formatting .................................................................................................................................... 24 5.4 Obtaining File Information........................................................................................................... 25 6 Libraries .............................................................................................................................................. 27 6.1 System.arraycopy() ....................................................................................................................... 27 6.2 Vector vs. ArrayList ...................................................................................................................... 28 6.3 Setting Initial Array Capacity....................................................................................................... 28 6.4 ArrayList vs. LinkedList ................................................................................................................ 29 6.5 Programming in Terms of Interfaces ............................................................................................ 31 7 Size ....................................................................................................................................................... 33 7.1 .class File Size............................................................................................................................... 33 7.2 Wrappers....................................................................................................................................... 33 7.3 Garbage Collection....................................................................................................................... 34 7.4 SoftReference ................................................................................................................................ 34 7.5 BitSet............................................................................................................................................. 35 7.6 Sparse Arrays................................................................................................................................ 35 3 Thirty Ways to Improve the Performance of Your Java™ Programs 1 Introduction This paper presents 30 ways to improve the performance of your Java™ applications. These techniques focus on Java language and library features. Performance is defined to include both speed and space issues, that is, how to make your programs run faster, while using less memory and disk space. The paper raises a variety of performance issues, and gives some hard numbers about how specific performance improvements work out. It should be noted up front that there’s no way to present totally definitive advice on performance, because various applications have different performance characteristics and bottlenecks, and because performance varies across different hardware, operating systems, and Java development tools such as compilers and virtual machines. The Java programming language is still evolving, and its performance continues to improve. The ultimate aim of the paper is to promote awareness of Java performance issues, so that you can make appropriate design and implementation choices for specific applications. 1.1 When to Worry About Performance Performance is not the only consideration in developing applications. Issues like code quality, maintainability, and readability are of equal or greater importance. The techniques found in the paper are not intended to be applied in isolation, simply because they exist. Some of the techniques are "tricky", leading to more obscure code, and should be applied only when necessary. For example, consider the use of the string concatenation operator (+). It’s natural to say: Strings=s1+s2+s3+"abc" + s4 + "def"; to concatenate a set of strings. Most of the time this is the right thing to do, though sometimes you will want to apply other approaches described below (§4.1), approaches that are more efficient at the expense of naturalness of expression. 1.2 Performance Issues Not Covered in This Paper This paper describes a set of techniques, rooted in the Java language and libraries. There are other areas of performance mentioned only in passing. The first of these is algorithm performance. If your application contains fundamentally slow algorithms in it, these techniques may not help you. For example, suppose that you use an N*N sort algorithm instead of a much more efficient N*log(N) one. It’s possible that such an algorithm will dominate the running time of your