1. Trang chủ
  2. » Công Nghệ Thông Tin

OReilly java performance may 2014 ISBN 1449358454

425 200 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 425
Dung lượng 11,65 MB

Nội dung

Java Performance: The Definitive Guide by Scott Oaks Copyright © 2014 Scott Oaks All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://my.safaribooksonline.com) For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com Editor: Meghan Blanchette Production Editor: Kristen Brown Copyeditor: Becca Freed Proofreader: Charles Roumeliotis April 2014: Indexer: Judith McConville Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Rebecca Demarest First Edition Revision History for the First Edition: 2014-04-09: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449358457 for release details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Java Performance: The Definitive Guide, the image of saiga antelopes, and related trade dress are trademarks of O’Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 978-1-449-35845-7 [LSI] Table of Contents Preface ix Introduction A Brief Outline Platforms and Conventions JVM Tuning Flags The Complete Performance Story Write Better Algorithms Write Less Code Oh Go Ahead, Prematurely Optimize Look Elsewhere: The Database Is Always the Bottleneck Optimize for the Common Case Summary 2 5 10 An Approach to Performance Testing 11 Test a Real Application Microbenchmarks Macrobenchmarks Mesobenchmarks Common Code Examples Understand Throughput, Batching, and Response Time Elapsed Time (Batch) Measurements Throughput Measurements Response Time Tests Understand Variability Test Early, Test Often Summary 11 11 16 18 20 24 24 25 26 29 33 36 A Java Performance Toolbox 37 Operating System Tools and Analysis 37 iii CPU Usage The CPU Run Queue Disk Usage Network Usage Java Monitoring Tools Basic VM Information Thread Information Class Information Live GC Analysis Heap Dump Postprocessing Profiling Tools Sampling Profilers Instrumented Profilers Blocking Methods and Thread Timelines Native Profilers Java Mission Control Java Flight Recorder Enabling JFR Selecting JFR Events Summary 38 41 43 44 46 47 50 51 51 51 51 52 54 55 57 59 60 66 70 72 Working with the JIT Compiler 73 Just-in-Time Compilers: An Overview Hot Spot Compilation Basic Tunings: Client or Server (or Both) Optimizing Startup Optimizing Batch Operations Optimizing Long-Running Applications Java and JIT Compiler Versions Intermediate Tunings for the Compiler Tuning the Code Cache Compilation Thresholds Inspecting the Compilation Process Advanced Compiler Tunings Compilation Threads Inlining Escape Analysis Deoptimization Not Entrant Code Deoptimizing Zombie Code Tiered Compilation Levels iv | Table of Contents 73 75 77 78 80 81 82 85 85 87 90 94 94 96 97 98 98 101 101 Summary 103 An Introduction to Garbage Collection 105 Garbage Collection Overview Generational Garbage Collectors GC Algorithms Choosing a GC Algorithm Basic GC Tuning Sizing the Heap Sizing the Generations Sizing Permgen and Metaspace Controlling Parallelism Adaptive Sizing GC Tools Summary 105 107 109 113 119 119 122 124 126 127 128 131 Garbage Collection Algorithms 133 Understanding the Throughput Collector Adaptive and Static Heap Size Tuning Understanding the CMS Collector Tuning to Solve Concurrent Mode Failures Tuning CMS for Permgen Incremental CMS Understanding the G1 Collector Tuning G1 Advanced Tunings Tenuring and Survivor Spaces Allocating Large Objects AggressiveHeap Full Control Over Heap Size Summary 133 136 140 145 148 149 150 157 159 159 163 171 173 174 Heap Memory Best Practices 177 Heap Analysis Heap Histograms Heap Dumps Out of Memory Errors Using Less Memory Reducing Object Size Lazy Initialization Immutable and Canonical Objects String Interning 177 178 179 184 188 188 191 196 198 Table of Contents | v Object Lifecycle Management Object Reuse Weak, Soft, and Other References Summary 202 202 208 221 Native Memory Best Practices 223 Footprint Measuring Footprint Minimizing Footprint Native NIO Buffers Native Memory Tracking JVM Tunings for the Operating System Large Pages Compressed oops Summary 223 224 225 226 227 230 230 234 236 Threading and Synchronization Performance 237 Thread Pools and ThreadPoolExecutors Setting the Maximum Number of Threads Setting the Minimum Number of Threads Thread Pool Task Sizes Sizing a ThreadPoolExecutor The ForkJoinPool Automatic Parallelization Thread Synchronization Costs of Synchronization Avoiding Synchronization False Sharing JVM Thread Tunings Tuning Thread Stack Sizes Biased Locking Lock Spinning Thread Priorities Monitoring Threads and Locks Thread Visibility Blocked Thread Visibility Summary 237 238 242 243 244 246 252 254 254 259 262 267 267 268 268 269 270 270 271 275 10 Java Enterprise Edition Performance 277 Basic Web Container Performance HTTP Session State Thread Pools vi | Table of Contents 277 280 283 Enterprise Java Session Beans Tuning EJB Pools Tuning EJB Caches Local and Remote Instances XML and JSON Processing Data Size An Overview of Parsing and Marshalling Choosing a Parser XML Validation Document Models Java Object Models Object Serialization Transient Fields Overriding Default Serialization Compressing Serialized Data Keeping Track of Duplicate Objects Java EE Networking APIs Sizing Data Transfers Summary 283 283 286 288 289 290 291 293 299 302 305 307 307 307 311 313 316 316 318 11 Database Performance Best Practices 321 JDBC JDBC Drivers Prepared Statements and Statement Pooling JDBC Connection Pools Transactions Result Set Processing JPA Transaction Handling Optimizing JPA Writes Optimizing JPA Reads JPA Caching JPA Read-Only Entities Summary 322 322 324 326 327 335 337 337 340 342 346 352 353 12 Java SE API Tips 355 Buffered I/O Classloading Random Numbers Java Native Interface Exceptions String Performance 355 358 362 364 366 370 Table of Contents | vii Logging Java Collections API Synchronized Versus Unsynchronized Collection Sizing Collections and Memory Efficiency AggressiveOpts Alternate Implementations Miscellaneous Flags Lambdas and Anonymous Classes Lambda and Anonymous Classloading Stream and Filter Performance Lazy Traversal Summary 371 373 373 375 376 378 378 379 379 381 382 383 385 A Summary of Tuning Flags 387 Index 397 viii | Table of Contents Flag What it does When to use it See also -XX:MaxDirectMemorySize=N Controls how much Consider setting this if you want to “Footprint” on native memory can limit the amount of direct memory page 223 be allocated via the a program can allocate Note that it is no longer necessary to set this allocateDir ect() method of flag to allocate more than 64 MB of the ByteBuffer direct memory class -XX:+UseLargePages Directs the JVM to If supported by the OS, this option “Large Pages” on allocate pages from will generally improve page 230 the operating performance system’s large page system, if applicable -XX:+LargePageSizeInBytes=N Directs the JVM to On large Solaris systems, increase “Large Pages” on allocate large pages this value (to, say, 256 MB) for best page 230 of the given size performance (Solaris only) -XX:+StringTableSize=N Sets the size of the hashtable the JVM uses to hold interned strings Increase this value if the application “String performs a significant amount of Interning” on string interning page 198 -XX:+UseCompressedOops Emulates 35-bit pointers for object references This is the default for heaps that are “Compressed less than 32 GB in size; there is never oops” on page an advantage to disabling it 234 -XX:+PrintTLAB Prints summary When using a JVM without support “Thread-local information about for JFR, use this to ensure that TLAB allocation TLABs in the GC log allocation is working efficiently buffers” on page 164 -XX:TLABSize=N Sets the size of the TLABs When the application is performing a lot of allocation outside of TLABs, use this value to increase the TLAB size -XX:-ResizeTLAB Disables resizing of TLABs Whenever TLABSize is specified, “Thread-local allocation make sure to disable this flag buffers” on page 164 “Thread-local allocation buffers” on page 164 Table A-8 Flags for thread handling Flag What it does When to use it See also -Xss Sets the size of the native stack for threads Particularly on 32-bit JVMs, decrease this size to make more memory available for other parts of the JVM “Tuning Thread Stack Sizes” on page 267 -XX:-BiasedLocking Disables the biased locking algorithm of the JVM Often helps performance of the thread pool “Biased Locking” on based applications page 268 Summary of Tuning Flags | 395 Table A-9 Miscellaneous JVM flags Flag What it does -XX:+AlwaysLockClassLoader Revert the Java parallel classloading scheme to the Java nonparallel scheme -XX:-StackTraceInThrowable Prevents the stack trace from being gathered whenever an exception is thrown When to use it See also On large systems loading classes “Classloading” on in only a single thread, startup page 358 performance may slightly benefit by using this flag On systems with very deep stacks where exceptions are frequently thrown (and where fixing the code to throw fewer exceptions is not a possibility) “Exceptions” on page 366 -XX:-RestrictContended Allows non-JDK code to use the @Contended annotation Set this if application code uses “The @Contended the @Contended annotation Annotation” on page to pad variables to prevent false 266 sharing -XX:-EnableContended Disables JDK code from using the @Contended annotation This should likely be left enabled “The @Contended Disabling may save a small Annotation” on page amount of space in certain JDK 266 classes -XX:+AggressiveOpts Enables certain optimizations for the JVM These optimizations are likely to become defaults in future releases You can test with this flag to “AggressiveOpts” on determine if it helps, but be page 378 aware that it may not work the same when JVM versions change Table A-10 Flags for Java Flight Recorder Flag What it does When to use it -XX:+FlightRecorder Enables Java Flight Recorder Enabling Flight Recorder is always “Java Flight recommended, as it has very little Recorder” on overhead unless an actual recording page 60 is happening (in which case, the overhead will vary depending on the features used, but still be relatively small) -XX:+FlightRecorderOptions Sets options for a default recording via the command line Control how a default recording can be made for the JVM -XX:+UnlockCommercialFeatures Allows the JVM to use If you have the appropriate license, commercial (nonopen-source) features 396 | Appendix A: Summary of Tuning Flags See also “Java Flight Recorder” on page 60 “Java Flight setting this flag is required to enable Recorder” on Java Flight Recorder page 60 About the Author Scott Oaks is an architect at Oracle Corporation, where he works on the performance of Oracle’s middleware software Prior to joining Oracle, he worked for years at Sun Microsystems, specializing in many disparate technologies from the SunOS kernel to network programming and RPCs to Windows systems and the OPEN LOOK Virtual Window Manager In 1996, Scott became a Java evangelist for Sun and in 2001 joined their Java Performance Group—which has been his primary focus ever since Scott also authored O’Reilly’s Java Security, Java Threads, JXTA in a Nutshell, and Jini in a Nut‐ shell titles Colophon The animals on the cover of Java Performance: The Definitive Guide are saiga antelopes (Saiga tatarica), commonly known as saigas Their most distinctive feature is an over‐ sized flexible nose, which hangs partially over their mouths The saigas’ noses help filter out dust in the summer, and heat up air in the winter before it goes to their lungs Saiga coats also adapt to both seasons: in the cold, it is thick and white, but turns a cinnamon color and thins out significantly in the summer The animals are around 2-3 feet tall at the shoulder, and weigh between 80-140 pounds Saigas have long thin legs, and are able to run up to 80 miles an hour to avoid predators Their habitat is in semi‐ desert steppes, where they spend their days grazing on various grasses and shrubs (in‐ cluding some that are poisonous to other animals) Saigas normally live in herds of 30-40 individuals, but gather in the thousands for mi‐ gration in the winter Rut begins in late November, after the herds move south During this time males don’t eat much and violently fight with each other, causing many to die from exhaustion The survivors collect and impregnate a harem of females, who will each give birth to one or two young in late April The saiga population is almost exclusively within Kazakhstan, with a small isolated subspecies in Mongolia Though they used to range all across the steppes of Central Asia, saigas have been severely overhunted in the last few decades and are designated as “critically endangered.” The collapse of the USSR led to uncontrolled hunting and a dramatic 97% decrease in the saiga herds Recovery has also been much more difficult because male saigas are the only sex with horns, a commodity much in demand in traditional Chinese medicine As a result, hunters have nearly wiped out male saigas and crippled the species’ ability to breed The cover image is from a loose plate, origin unknown The cover fonts are URW Type‐ writer and Guardian Sans The text font is Adobe Minion Pro; the heading font is Adobe Myriad Condensed; and the code font is Dalton Maag’s Ubuntu Mono

Ngày đăng: 12/05/2017, 09:57

TỪ KHÓA LIÊN QUAN