Java Performance Tuning
Java(TM) - see bottom of page
Our valued sponsors who help make this site possible
JProfiler: Get rid of your performance problems and memory leaks!
Training online: Threading Essentials course
Tips November 2010
Get rid of your performance problems and memory leaks!
Get rid of your performance problems and memory leaks!
Back to newsletter 120 contents
Things I learnt tracking a billion events in 24 hours (Page last updated September 2010, Added 2010-11-29, Author Ben Lowry, Publisher playtomic). Tips:
- Have a priority connection capability to your servers so that if they become overloaded you can still connect to administer them otherwise you can locked out when the server is overloaded.
- Pre-process anything that doesn't need to be processed during user-initiated or other high load or latency sensitive activity.
- If an application suddenly becomes popular, it may need to handle ten times the volume of requests and throughput - can yours?
- You need to have your application alert you when it exceeds currently defined thresholds (and obviously you need to determine which thresholds need alerting on) so that you can take corrective action before the exceeded threshold turns into a serious problem or a catastrophic failure.
- Your load balancing solution should include the option of denying/dropping service to avoid your servers getting swamped.
- Segment your services resource usage so that if one service gets overloaded it doesn't steal all the resources from the other services.
Introducing NIO.2 (JSR 203) Part 5: Watch Service and Change Notification (Page last updated August 2010, Added 2010-11-29, Author Masoud Kalali, Publisher kalili). Tips:
- Java 7 provides a file system watching service that uses an efficient underlying operating system notification mechanism where available (which is most modern operating systems, otherwise the JVM falls back to a less efficient polling mechinism) to allow the application to efficiently find out about changes to files and directories.
- From Java 7 use
Watcher ws=java.nio.file.FileSystems.getDefault().newWatchService() (or equivalent) to register Path (file system path) objects using Path.register(Watcher, ...) to get a list of paths that have changed, and to appropriately handle different kinds of file changes.
Java Concurrency Part 6 : Atomic Variables (Page last updated September 2010, Added 2010-11-29, Author Baptiste Wicht, Publisher baptiste-wicht). Tips:
- Unsynchronized instance variables that are read and updated concurrently can easily have updates incorrectly overwritten by updates from other threads.
- Synchronize access and updates to instance variables if they will be read or updated from multiple threads.
- The synchronize keyword allows mutiple threads to safely access and update shared data structures, but in a pessimistic way, i.e. the variable is locked on a per thread basis until the thread releases it; other threads trying to acquire the lock are blocked until the lock holding thread releases the lock.
- Non-blocking algorithms are more scalable and better performing for mostly-read variables (which is the overwhelming majority of cases).
- Non-blocking algorithms typically use optimistic techniques, detecting collisions between threads and typically retrying the operation.
- Non-blocking optimistic operations on data variables are available through using AtomicInteger, AtomicLong, AtomicBoolean, AtomicReference.
Unveiling the java.lang.Out OfMemoryError And dissecting Java heap dumps (Page last updated May 2010, Added 2010-11-29, Author Jinwoo Hwang, Publisher ). Tips:
- A java.lang.OutOfMemoryError (OOME) is thrown when the JVM cannot allocate an object due to memory constraints. There are six different types of memory areas in the JVM: 1. Program Counter Register; 2. Java Virtual Machine Stack; 3. Heap; 4. Method Area; 5. Runtime Constant Pool; 6. Native Memory Stack. All but the first of these can cause an OOME.
- Different OutOfMemoryErrors are thrown depending on what caused the error and which memory space is unable to provide the required space. The error message usually identfies which memory space has caused the error.
- "OutOfMemoryError: PermGen space" implies the perm gen space is too small (stores class objects and interned strings); this may be caused by loading too many classes (usually class versions, or generated classes), or interned strings, or the space may be too small (use -XX:MaxPermSize=... to set to a larger value).
- "OutOfMemoryError: unable to create new native thread" typically happens when you are using very many threads and you have encountered the limitations of the process on that particular machine (possibly at that time). You might want to reduce the stack size of threads to work around this, or examine how further operating system memory resources can be made available to the JVM.
- "OutOfMemoryError: requested NNN bytes for MMMM. Out of swap space?" Typically occurs when the process has grown very big and you have encountered the limitations of the process on that particular machine (possibly at that time). You might want to reduce the number of objects in the system to work around this, or examine how further operating system memory resources can be made available to the JVM.
- "OutOfMemoryError: Java heap space" this usually indicates you need to change the maximum size of the heap (-Xmx) or reduce the number of objects being held on to by the application. Can be accopanied by a heap dump if the JVM has been so configured (e.g. with -XX:+HeapDumpOnOutOfMemoryError set).
- To diagnose an OutOfMemoryError: first examine the error message to determine what caused the error and which memory space was unable to allocate more memory; then determine whether the error can be eliminated woth a configuration change (e.g. larger heap or perm space setting or stack sizes), in some cases it may help you to examine the garbage collection statistics to identify this; finally if this cannot be solved by a configuration change then either the application memory usage needs to be examined (with the help of a heap dump if possible) or the application needs to be spread across multiple JVMs possibly across multiple machines.
Swing's event-dispatching thread (Page last updated October 2010, Added 2010-11-29, Author Jeff Friesen, Publisher javajeff). Tips:
- The event-dispatching thread is the thread that executes all drawing and event-handling code in a Swing application.
- Because Swing is a single-threaded graphical user interface (GUI) toolkit, you must create an application's Swing user interface on the event-dispatching thread.
- Even the initial call to create Swing components should be processed on the event-dispatching thread - typically this means putting the Swing component initialization code into a Runnable object and passing that to EventQueue.invokeLater(Runnable).
- Never perform a lengthy activity on the event-dispatching thread as this will make the user interface unresponsive - the user will not even be able to close the application's main window.
6 Ways to Kill Your Servers - Learning How to Scale the Hard Way (Page last updated August 2010, Added 2010-11-29, Author Steffen Konerow, Publisher highscalability). Tips:
- If you don't properly load test before deploying your production system, you will be doing your load testing in production.
- Putting disk intensive applications (even frequently updated logfiles) on a non-local disk is likely to be a major bottleneck.
- Most third-party products' out-of-the-box configuration is not suitable for production, they are usually configured for ease of installation and development. Ensure that you configure your products appropriately for your production system.
- The database tends to be a bottleneck if your system hasn't been built with database limitations and strengths in mind.
- Caching is the single most important performance optimization on most systems.
- Monitor your system including the filesystem - running out of inodes (file pointers) or disk space means you run out of the ability to create new files.
- Maintaining a website is tending to the next performance bottleneck, ad infinitum. There will always be another that needs fixing as the site grows.
- There appears to be no substitute for experience.
Back to newsletter 120 contents
Last Updated: 2019-04-29
Copyright © 2000-2019 Fasterj.com. All Rights Reserved.
All trademarks and registered trademarks appearing on JavaPerformanceTuning.com are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. JavaPerformanceTuning.com is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us