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: Concurrency, Threading, GC, Advanced Java and more ...
Tips January 2010
Get rid of your performance problems and memory leaks!
Get rid of your performance problems and memory leaks!
Back to newsletter 110 contents
A Note on the Thread (Un)safety of Format Classes (Page last updated January 2010, Added 2010-01-31, Author Jeremy Manson, Publisher jeremymanson). Tips:
- The java.text.Format classes (Format, MessageFormat, NumberFormat, DecimalFormat, ChoiceFormat, DateFormat, SimpleDateFormat) are not thread-safe and cannot be shared amongst threads.
- Do not make a java.text.Format instance (Format, MessageFormat, NumberFormat, DecimalFormat, ChoiceFormat, DateFormat, SimpleDateFormat) static as this is likely to lead to corruption from incorrect multi-threaded use.
- To use a java.text.Format instance thread-safely, you either have to create an expensive Format object (Format, MessageFormat, NumberFormat, DecimalFormat, ChoiceFormat, DateFormat, SimpleDateFormat) every time you invoke the method, or use a ThreadLocal instance holding a Format object per thread or be very careful to correctly synchronize around the formatting (but this is quite inefficient) or use a thread-safe time library (like Joda-time).
The Missing Piece in the Virtualization Stack (Page last updated January 2010, Added 2010-01-31, Author Nati Shalom, Publisher natishalom). Tips:
- Most of us spend most of our time maintaining our code, plumbing it to other services within our organizations and continuously maintaining and tuning it.
- Each demand for additional scaling forces us to go through a complete cycle of tuning, design and in some cases, through a complete product selection phase to meet the demand.
- Our main real-life challenge is not how to make our infrastructure more efficient but how to make our business more efficient - adding a new machine requires manual reconfiguring, testing, measurement, probable retuning, and even possibly a redesign!
- The Virtualization pattern consists of: Breaking physical resources into smaller logical units; Decoupling the application from the physical resources; and providing an abstraction that makes all the small units look like one big unit
- The Virtualization pattern needs to be applied across the whole application lifecycle to properly gain the full efficiency of virtualization.
- Use the Virtualization pattern by interposing an abstraction layer that decouples the application from the resources and dynamically provides virtualization resources to the application.
- Resource sharing and pooling across multiple instances provides an efficient use of resources as long as no instance requires the full capacity of the resources.
Java profiling with DTrace (Page last updated January 2010, Added 2010-01-31, Author Wayne Adams, Publisher wayne-adams). Tips:
- You can use dtrace on any operating system by installing VirtualBox then running OpenSolaris on VirtualBox.
- [Article details using DTrace probes in a HotSpot JVM.]
- Monitoring thread start and stop events using dtrace would use hotspot$1:::thread-start and hotspot$1:::thread-stop probes [example provided].
- Monitoring GC events using dtrace would use hotspot$1:::gc-begin and hotspot$1:::gc-end and hotspot$1:::mem-pool-gc-begin and hotspot$1:::mem-pool-gc-end probes [example provided].
- dtrace hotspot probes vary in overhead they add to an application depending on the frequency of the probe event: gc and thread probes have low overhead (because they are rare events); method-entry and method-exit probes have a high overhead even if enabled but not being used! The -XX:+ExtendedDTraceProbes flag needs to be set to enable expensive probes.
Overview of Performance Tuning Tools in Oracle Fusion Middleware (Page last updated January 2010, Added 2010-01-31, Author Olaf Heimburger, Publisher Oracle). Tips:
- Most memory leaks derive from over-looked references or poorly implemented homegrown caches.
- If you do not use SoftReferences or WeakReferences, you cannot leverage the GC to automatically shrink your cache.
- In a multi-tier application data can and will cross process boundaries - this takes time as serialization and de-serialization is slower than keeping the data inside of the process.
- Choosing externalization over serialization can dramatically improve performance.
- Once you need to call something remotely, you definitely lose time. Saving network calls in either numbers or size will save time.
- The database is almost always the fastest piece in the architecture. On the other hand, missing indexes can decrease performance dramatically. A well tuned database and correctly used SQL statements play well together in a optimal architecture.
- Dynamically created objects start living in Eden Space. During garbage collection they move to the Survivor Space and to the Old Generation until they are un-used and collected by the GC. The PermGenSpace stores static data like classes, constant values, and static variables.
- When the Old Generation reaches a constant size of 70-80% of the available space, one can assume a memory leak. A mark of 90% is serious and should be investigated immediately.
- A highly saturated memory influences the runtime, as GC will be called more often and needs longer cycles to find areas that can be collected.
- VisualVM and JVisualVM show JVM state including threads, thread dumps (there is a Thread Dump Analyzer plugin), Class Loader Time, Compile Time (for the JIT), GC Time, Memory Samples (analyzable with the Heap Dump Analyzer).
- JRockit Console displays an overview of the current state of the JVM with gauges for Used Java Heap, Used Physical Memory, CPU Usage, and Live Set as well as graphs for Memory or Processor usage with tabs for Memory, Threads, Runtime, Exception Count, and Memory Profile.
- Oracle JRockit Runtime Analyzer gives a snapshot of the application including slow methods, memory usage, all existing objects, possible optimizations, blocking threads and a Memory Leak Detector.
Data scaling at Shopzilla volumes (Page last updated January 2010, Added 2010-01-31, Author Rob Roland, Publisher Shopzilla). Tips:
- Shopzilla chose the Oracle Coherence route to scale large amounts of data, fast (from alternatives such as Cassandra, memcached, Hadoop).
- Out of each colocation, Shopzilla needs to serve over 120 million documents with near-immediate response time. To achieve this the search engine issues requests for documents in batches in parallel, and the document server responds to each batch within 2.5 - 3.5 milliseconds, with the total document requests for a single search taking no more than 25 milliseconds, including parsing by the search engine.
- Physical architectures to achieve millisecond document serving response are multiple 4-core servers for Coherence grids - Coherence auto-recovers across the grid from individual machine crashes, with some performance penalty but no overall loss of service.
Java Profiling: Under the Covers (Page last updated December 2009, Added 2010-01-31, Author Wayne
Adams, Publisher JavaLobby). Tips:
- java.lang.instrument provides a mechanism to to modify bytecode as it is being loaded.
- [Article describes creating a simple java.lang.instrument agent.]
- Instrumenting specific code or all code is possible using a bytecode-injection library (this article uses Javassist).
- [Article describes using a simple java.lang.instrument agent to transform some classes.]
- The com.sun.tools.attach package and the com.sun.tools.attach.VirtualMachine class allow you to attach an agent to an alredy running application.
- [Article describes using com.sun.tools.attach.VirtualMachine to attach an agent to a Java program.]
- The java.lang.instrument.Instrumentation class has a getObjectSize() method (implementation-dependent) which gives you an approximate size of an object - typically its shallow size.
Minimize Garbage Generation: GC is your Friend, not your Servant (Page last updated January 2010, Added 2010-01-31, Author David Holmes, Publisher Sun). Tips:
- Garbage collection is cheap but not free, the costs are sufficient that you should keep that in mind - especially on real-time systems
- Don't append to String objects - Strings are immutable so each append creates a new String.
- Convert StringBuilder objects (or StringBuffer objects) to String objects only when you need the actual String.
- Size StringBuilder (or StringBuffer) objects initially so that dynamic expansion of their internal character buffers is avoided.
Back to newsletter 110 contents
Last Updated: 2022-11-29
Copyright © 2000-2022 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