Recycling objects to improve performance

Overview

In a previous article I stated that the reason the deserialization of objects was faster was due to using recycled objects. This is potentially surprising for two reasons, 1) the belief that creating objects is so fast these days, it doesn’t matter or is just as fast as recycling yourself, 2) None of the serialization libraries use recycling by default.

This article explores deserialization with and without recycling objects. How it not only is slower to create objects, but it slows down the rest of your program by pushing data out of your CPU caches.

While this talks about deserializaton, the same applies to parsing text or reading binary files, as the actions being performed are the same.

The test

In this test, I deserialize 1000 Price objects, but also time how long it takes to copy a block of data. The copy represents work which the application might have to perform after deserializing.

The test is timed one million times and those results sorted. The X-Axis shows the percentile timing. e.g. the 90% values is the 90% worst value (or 10% of values are higher).

As you can see, the deserialization take longer if it has to create objects as it goes, however sometimes it takes much much long. This is perhaps not so surprising as creating objects means doing more work and possibly being delayed by a GC. However, it is the increase in the time to copy a block of data which is surprising. This demonstrates that not only is the deserialization slower, but any work which needs the data cache is also slower as a result. (Which is just about anything you might do in a real application)
Performances tests rarely show you the impact on the rest of your application.

In more detail

Examining the higher percentile (longest times) you can see that the performance consistently bad if the deserialization has to wait for the GC.

And the performance of the copy increases significantly in the worst case.

The code

Recycling example code

Reference: Recycling objects to improve performance from our JCG partner Peter Lawrey at the Vanilla Java blog.

Related Articles :
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


× 8 = twenty four



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books