Java Reference
In-Depth Information
The implementations are a little more detailed, but the performance of GC is dominated by
these basic operations: finding unused objects, making their memory available, and compact-
ing the heap. Different collectors take different approaches to these operations, which is why
they yield different performance characteristics.
It is simpler to perform these operations if no application threads are running while the
garbage collector is running. Java programs are typically heavily multithreaded, and the
garbage collector itself often runs multiple threads. This discussion considers two logical
groups of threads: those performing application logic, and those performing GC. When GC
tracks object references, or moves objects around in memory, it must make sure that applica-
tion threads are not using those objects. This is particularly true when GC moves objects
around: the memory location of the object changes during that operation, and hence no ap-
plication threads can be accessing the object.
The pauses when all application threads are stopped are called stop-the-world pauses . These
pauses generally have the greatest impact on the performance of an application, and minim-
izing those pauses is the key consideration when tuning GC.
Generational Garbage Collectors
Though the details differ somewhat, all garbage collectors work by splitting the heap into
different generations. These are called the old (or tenured) generation, and the young genera-
tion. The young generation is further divided into sections known as eden and the survivor
spaces (though sometimes, eden is incorrectly used to refer to the entire young generation).
The rationale for having separate generations is that many objects are used for a very short
period of time. Take, for example, the loop in the stock price calculation where it sums the
square of the difference of price from the average price (part of the calculation of standard
deviation):
sum = new
new BigDecimal ( 0 );
for
for ( StockPrice sp : prices . values ()) {
BigDecimal diff = sp . getClosingPrice (). subtract ( averagePrice );
diff = diff . multiply ( diff );
sum = sum . add ( diff );
}
Like many Java classes, the BigDecimal class is immutable: the object represents a particu-
lar number and cannot be changed. When arithmetic is performed on the object, a new object
is created (and often, the previous object with the previous value is then discarded). When
Search WWH ::




Custom Search