Java Reference
In-Depth Information
Chapter 7. Heap Memory Best
Practices
Chapters 5 and 6 discussed the details of how to tune the garbage collector so that it has as
little effect on a program as possible. Tuning the garbage collector is important, but often
better performance gains can be made by utilizing better programming practices. This
chapter discusses some of the best-practice approaches to using heap memory in Java.
There are two conflicting goals here. The first general rule is to create objects sparingly and
to discard them as quickly as possible. Using less memory is the best way to improve the ef-
ficiency of the garbage collector. On the other hand, frequently re-creating some kinds of ob-
jects can lead to worse overall performance (even if GC performance improves). If those ob-
jects are instead reused, programs can see substantial performance gains. There are a variety
of ways in which objects can be reused, including thread-local variables, special object refer-
ences, and object pools. Reusing objects means they will be long-lived and impact the
garbage collector, but when they are reused judiciously, overall performance will improve.
This chapter discusses both approaches and the trade-offs between them. First, though, we'll
look into tools for understanding what is happening inside the heap.
Heap Analysis
GC logs and the tools discussed in Chapter 5 are great at understanding the impact GC has
on an application, but for additional visibility, we must look into the heap itself. The tools
discussed in this section provide insight into the objects that the application is currently us-
ing.
Most of the time, these tools operate only on live objects in the heap—objects that will be re-
claimed during the next full GC cycle are not included in the tools' output. In some cases,
tools accomplish that by forcing a full GC, so the application behavior can be affected after
the tool is used. In other cases, the tools walk through the heap and report live data without
freeing objects along the way. In either case, though, the tools require some amount of time
Search WWH ::




Custom Search