Java Reference
In-Depth Information
and machine resources; they are generally not useful during measurement of a program's ex-
ecution.
Heap Histograms
Reducing memory use is an important goal, but as with most performance topics, it helps to
target efforts to maximize the available benefits. Later in this chapter, I'll show an example
around lazily initializing a Calendar object. That will save 640 bytes in the heap, but if the
application only ever initializes one such object, there will not be a measurable difference in
performance. Analysis must be performed to know which kinds of objects are consuming
large amounts of memory.
The easiest way to do that is via a heap histogram. Histograms are a quick way to look at the
number of objects within an application without doing a full heap dump (since heap dumps
can take a while to analyze, and they consume a large amount of disk space). If a few partic-
ular object types are responsible for creating memory pressure in an application, a heap his-
togram is a quick way to find that.
Heap histograms can be obtained by using jcmd (here with process ID 8898):
% jcmd 8998 GC.class_histogram
8898:
num #instances #bytes class name
---------------------------------------------
1: 789087 31563480 java.math.BigDecimal
2: 237997 22617192 [C
3: 137371 20696640 <constMethodKlass>
4: 137371 18695208 <methodKlass>
5: 13456 15654944 <constantPoolKlass>
6: 13456 10331560 <instanceKlassKlass>
7: 37059 9238848 [B
8: 10621 8363392 <constantPoolCacheKlass>
In a heap histogram, Klass -related objects are often near the top; those are the metadata ob-
jects from loading the classes. It is also quite common to see character arrays ( [C ) and
String objects near the top, as these are the most commonly created Java objects. Byte ar-
rays ( [B ) and object arrays ( [Ljava.lang.Object; ) are also quite common, since classload-
ers store their data in those structures. (If you're unfamiliar with the syntax here, it comes
Search WWH ::




Custom Search