Java Reference
In-Depth Information
The reference type here is the reference to any kind of Java object—instances of classes or
arrays. That space is the storage only for the reference itself. The size of an object that con-
tains references to other objects varies depending on whether we want to consider the shal-
low, deep, or retained size of the object, but that size also includes some invisible object
header fields. For a regular object, the size of the header fields is 8 bytes on a 32-bit JVM
and 16 bytes on a 64-bit JVM (regardless of heap size). For an array, the size of the header
fields is 16 bytes on a 32-bit JVM or a 64-bit JVM with a heap of less than 32 GB, and 24
bytes otherwise.
For example, consider these class definitions:
public
public class
class A {
private
private int
int i ;
}
public
public class
class B {
private
private int
int i ;
private
private Locale l = Locale . US ;
}
public
public class
class C {
private
private int
int i ;
private
private ConcurrentHashMap chm = new
new ConcurrentHashMap ();
}
The actual sizes of a single instance of these objects (on a 64-bit Java 7 JVM with a heap size
of less than 32 GB) is given in Table 7-2 .
Table 7-2. Sizes of simple objects
Shallow size Deep size Retained size
A 16
16
16
B 24
216
24
C 24
200
200
In class B , defining the Locale reference adds 8 bytes to the object size, but at least in that
example, the actual Locale object is shared among other classes. If the Locale object is nev-
Search WWH ::




Custom Search