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 March 2011
JProfiler
|
Get rid of your performance problems and memory leaks!
|
JProfiler
|
Get rid of your performance problems and memory leaks!
|
|
|
Back to newsletter 124 contents
http://kirk.blog-city.com/why_do_i_have_this_long_gc_pause.htm
Why do I have this long GC pause? (Page last updated February 2011, Added 2011-03-29, Author Kirk Pepperdine, Publisher kodewerk). Tips:
- Before a young generation collection starts, the VM must estimate the memory consumption of tenured at the end of the GC - if the estimated value exceeds 70% then it is obliged to collect tenured space.
- If both survivor spaces are empty after a collection, then all the objects have been either removed or promoted to the old generation. The latter suggests that the survivor spaces are too small.
- If the heap becomes fragmented enough to require a compaction, this will be a stop-the-world GC even for the concurrent collector (which actually defers the task to another collector, probably the parallel old).
- The old generation garbage collector takes time (mostly) proportional to the number of live objects, not dead objects.
- Heap tuning is about creating the conditions where most objects die in the young generational space.
- Having a lower initiating occupancy threshold gives the allocators and collector more space in which to work through fragmentation.
- Useful GC reporting flags include -Xloggc:gc.log, -XX:+PrintGCDetails, -XX:+PrintTenuringDistribution, -XX:+PrintHeapAtGC, -XX:+PrintGCApplictionStoppedTime.
http://www.javaspecialists.eu/archive/Issue190.html
Automatically Unlocking with Java 7 (Page last updated February 2011, Added 2011-03-29, Author Dr. Heinz M. Kabutz, Publisher The Java Specialists' Newsletter). Tips:
- If you open a file, it is easy to forget to close it. Java 7 introduced a try-with-resources statement which automatically closes any AutoCloseable resources referenced in the try statement.
- The AutoCloseable interface is implemented by 553 classes in the JDK.
- try-with-resource works well with objects that are constructed and then closed immediately after the block; it will not work with resources that need to be kept alive beyond the block.
- An AutoCloseable wrapper around a Lock will enable that lock to be auto-closed using the try-with-resources statement. the result is very readable and fully robust:
try (lock(lock)) {...}
(the lock is automatically released when the block is exited)
http://www.oracle.com/technetwork/articles/java/briangoetzchief-188795.html
Looking Ahead to Java SE 7 and 8 with Brian Goetz (Page last updated January 2011, Added 2011-03-29, Author Janice J. Heiss, Publisher Oracle). Tips:
- Code that is straightforward, clean, and follows the most obvious object-oriented principles is often fast because compiler developers focus their efforts on the most common code patterns.
- Code execution speed is becoming less and less of an issue as servers and computers increase their speed.
- The most common excuse for writing dirty code is because of the perceived performance benefits, but unfortunately most code atrocities committed in the name of performance aren't even faster and are, therefore, frequently counterproductive.
- With concurrency, developers should realize that they have to be careful, think hard, and have someone review their code.
- Developers can sidestep the complexity of concurrency by limiting shared mutable state in their programs. If you don't have shared state, you don't have to coordinate access to that state.
- Where concurrency is involved, immutable state is better than mutable state - seek to increase immutability and decrease sharing.
- In Java 8 we have extension methods - these let you add methods to interfaces that specify a reference to a default implementation, so in the event a class implementing that interface does not provide an implementation of that method, the default is used.
- Java 8 will support closures which allows, for example, Collections to have a forEach method that can execute in parallel. Without this, developers are limited to the for loop, which is inherently serial, so doesn't support multicore platforms well.
- If I were building a new language today, there's no way I would consider writing a native compiler -- I'd much rather target the JVM, and get portability, garbage collection, high-quality compilation, concurrency control, tools, serviceability, and lots of other features for free.
http://ajax.sys-con.com/node/1742629
Behind the Scenes of Serialization in Java (Page last updated March 2011, Added 2011-03-29, Author Alois Reitbauer, Publisher AjaxWorld). Tips:
- Choosing the right serialization strategy however is central for achieving good performance and scalability.
- Serialization problems can affect CPU, memory, network load and response times.
- Sending only the data with no metadata is the most efficient way of sending data, as there is no overhead involved. This can only be de-serialized if you know the exact serialization method.
- Standard Java serialization incudes metadata that allows an object to be rebuilt at the destination. Though this has a high initial metadata overhead, the metadata is cached for future messaging, so is much cheaper for multiple messages.
- Implementing the Java Externalizable interface allows much more control over what is serialized, providing for potential improvements in performance and size though giving a higher maintainance cost.
- Hessian is an alternative to Java serialization which uses a binary protocol which is relatively compact.
- JSON is a text-based serialization representation with a higher overhead than many other serialization approaches, but has the advantage that it is more lightweight than XML and it has good support for describing metadata.
- XML is the standard serialization format for exchanging data in heterogeneous systems - and it has out-of-the-box support for data validation. The amount of metadata, however, can become really high. Data is in text format by default but CDATA tags enables you to send binary data.
- Fast InfoSet is a lightweight version of XML which reduces unnecessary overhead and redundancies in data. This leads to smaller data set and better serialization and deserialization performance. With JAX-WS 2.0 you can enable Fast InfoSet serialization by using the @FastInfoset annotation.
- Data prefixed with its length allows faster and more efficient parsing.
http://java.dzone.com/articles/three-common-application
Three Common Application Performance Challenges for Developers (Page last updated December 2010, Added 2011-03-29, Author Bhaskar Sunkara, Carlos Hoces, Publisher JavaLobby). Tips:
- Memory leaks happen when the developer didn?t remove all references to an object.
- Use heap dumps and/or profilers to diagnose memory leaks.
- A heap dump allows you to see which object is holding a reference to the collections.
- Memory profilers let you analyze your heap - they have live data and show what is creating the objects.
- One of the most effective ways to isolate and address memory leaks is through snapshotting a transaction and analysing the resulting code paths.
- A common problem with applications is badly performing SQL from fields not being indexed, and from too much data being fetched.
- Performance considerations should always include the means and ways of accessing and storing data.
- ORM can be a significant weight on an application?s performance - make sure you understand what?s going on under the hood.
- Using "synchronized" can prevents thread from updating the same object at the same time, so preventing data inconsistencies.
- Synchronization forces concurrent processing back into sequential execution.
- The most common sources for leaking memory seem to come from two bad practices: forgetting to remove listeners no longer needed, and forgetting to close opened resources.
- If database access performance needs improving, you may need to directly use JDBC methods.
- The need for synchronization is not related to performance, but to data process requirements.
http://redstack.wordpress.com/2011/01/06/visualising-garbage-collection-in-the-jvm/
Visualising Garbage Collection in the JVM (Page last updated January 2011, Added 2011-03-29, Author Mark Nelson, Publisher Mark Nelson). Tips:
- The permanent generation is used only by the JVM itself to keep data. You cannot directly place any data in the permanent generation.
- The heap is the main area of JVM memory, divided into the ?Old Generation? and the ?New Generation.? which in turn is divided into ?Eden? and two ?Survivor? spaces.
- -Xms sets the initial/minimum heap size
- -Xmx sets the maximum heap size
- -XX:NewRatio sets the size of the old generation in ratio to the size of the new generation
- -XX:+PrintGCTimeStamps, -XX:+PrintGCDetails and -Xloggc:gc.log cause the JVM to print out additional information about garbage collection into a file call gc.log
- jvisualvm is a useful tool to view what is happening inside a JVM.
- New objects are created in Eden. When Eden is full, a young generation GC occurs, and Eden is emptied - dead objects are cleared and live objects are moved to Survivor space or the old generation if there isn't enough space in the Survivor space.
- When the old generation gets full, an old generation garbage collection removes all dead objects from it to free up space.
- When there is no more space left in the heap, your application will crash with an OutOfMemoryException.
Jack Shirazi
Back to newsletter 124 contents
Last Updated: 2024-11-29
Copyright © 2000-2024 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.
URL: http://www.JavaPerformanceTuning.com/news/newtips124.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us