Java Reference
In-Depth Information
The IBM VM uses three machine-words for the object header. All header fields hold the following information:
size + flags : This field is the first machine-word of the object header. This field contains
the size of the object and flags to indicate different states of the object. Because the size of the
object is limited and all objects start at 8 bytes boundary, some of the bits are used to store
flags indicating different states of the object.
mptr : This field is the second machine-word in the object header. It can hold one of the two
pieces of information depending on if the object is an array or not.
It holds a pointer to the method block, if the object is not an array. The method block
has reference to class block, which can provide more information about the Java class to
which the object belongs.
If the object is an array, this field holds the length of the array.
locknflags : This field is the third machine-word in the object header. It is used to hold
information about the object locking and some flags. A 1-bit flag is used to indicate whether
the object is an array. If this bit is set, mptr contains the length of the array. Another 1-bit flag
is used to indicate whether the object was hashed and moved. Note that objects are moved
during some kind of garbage collection. Since the hash code of an object is its address in the
memory and is supposed to be the same, the garbage collector that moves objects around
during garbage collection uses this flag to preserve the hash code, if it was computed and used
before the garbage collection.
in Java, all objects are created on heap. Java uses the new operator to allocate memory for an object on heap.
an array's length is not a part of its class definition. it is defined at runtime. it is stored in the object header. You will not
find the length instance variable in the array's class definition when you perform introspection on an array's class.
Tip
Java does not provide any direct means to compute the size of an object. You should not write a Java program that
depends on the size of the objects anyway. The size of primitive types, for example, int , long , double , etc. is fixed for
all JVM implementations. The layout and size of an object depends on the JVM implementation. Therefore, any code
that depends on the size of objects may work on one platform and not on others.
Garbage Collection in Java
The garbage collector is part of the Java platform. It runs in the background in a low priority thread. It automatically
reclaims objects. However, before it reclaims objects, it makes sure that the running program in its current state will
never use them again. This way, it ensures that the program will not have any dangling references. An object that
cannot be used in the future by the running program is known as a dead object or garbage . An object that can be used
in the future by the running program is known as a live object .
There are many algorithms to determine whether an object is live or dead. One of the simplest, but not very
efficient, algorithms is based on reference counting, which stores the count of references that refer to an object. When
an object's reference is assigned to a reference variable, the reference count is incremented by 1. When a reference
variable no longer refers to an object, the reference count is decremented by 1. When the reference count for an object
 
 
Search WWH ::




Custom Search