Java performance programming, Part 1: Smart object-management saves the day

Learn how to reduce program overhead and improve performance by controlling object creation and garbage collection

1 2 3 Page 3
Page 3 of 3

With this approach we can have objects that are reusable and, from the user's standpoint at least, immutable. We can also hide any state information we don't want to make visible to the users of the class. It also gives somewhat better performance than allocation and recycling, even for these simple objects, and substantially improves performance when used for composite objects.

If the pooled objects are only used by a single thread, a further performance enhancement is possible. The use of synchronized for the getInstance() and freeInstance() methods can be eliminated in this case, providing up to several times the performance of allocation and recycling. However, this approach needs to be used with caution because of the resulting lack of thread safety in the pool.

Lifeguard on duty?

One complication of the free pool approach to object reuse: the code using the objects needs to return them to the pool when usage is complete. In some ways, this looks like a retreat to the C/C++ approach of explicit allocation and deallocation. Unlike in C/C++, though, we can pick the particular cases in which we want to implement this level of object management. In cases in which we're dealing with high-usage object types, a strategic withdrawal to explicit allocation and deallocation can yield a major benefit in terms of improved performance.

This type of free pool is also much more forgiving than C/C++. If there are some abnormal cases in which the objects never get returned to the pool, the only impact will be somewhat lower performance -- the objects will eventually be garbage collected when they're no longer in use, and we'll just need to allocate new ones for the pool at some point. In C/C++, objects which are not deallocated stay around as long as the program executes, causing the memory leaks that can plague C/C++ programs.

Object pools are also often used for managing limited or costly resources, such as database connections. A number of past JavaWorld articles have covered this use in detail, and are linked in the Resources section below. Pools of this type are generally not so forgiving: if the resource is not properly freed, it may not be available for reuse without restarting the JVM.

Conclusion

In this article we've examined some of the issues surrounding object management in Java and provided techniques that can substantially reduce the volume of object creation and garbage collection in your programs. The timing results given in the article demonstrate the performance improvements possible with these techniques. These improvements come at the cost of a small increase in code complexity, but, in cases in which your programs are generating large numbers of objects, this increased complexity can have a big performance payoff.

As with all performance enhancement techniques, these need to be applied judiciously. Most of the code in any given application is not going to be used often enough for object creation to be a significant concern. Where it does become important is in code shown by design requirements or execution profiling to be heavily used. If performance is a concern, you need to look at the object-creation load this heavily used code generates and structure it to minimize the resulting object churn.

In future articles in this series, we'll look at other issues that can limit the performance of Java applications. The next article will take a look at the drawbacks of using type casts and illustrate coding techniques to reduce the amount of casting in your code. As a side benefit, this will also provide some useful utility classes for working with primitive types instead of the corresponding wrapper objects.

Dennis Sosnoski is a software professional with over 25 years experience in developing for a wide range of platforms and application areas. An early adopter of C++, he was just as quick to convert to using Java when it became available, and for the last three years it's been his preferred development platform. Dennis is the president and senior consultant of Sosnoski Software Solutions Inc., a Java consulting and contract software development firm based "In the Land of Redmond, where the Shadows lie." He's also a big fan of sci-fi and fantasy books.

Learn more about this topic

  • Benchmarks
  • Recent JavaWorld articles covering object pools for resource management

Related:
1 2 3 Page 3
Page 3 of 3