Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Garbage Collection and the Other Kind of Heap


A Java object is an instance of a class or an array. Every object instance in a Java program occupies some memory. The manner in which a Java object is represented in memory is left up to the implementor of the Java virtual machine and can vary from one implementation to another. However, in the typical Java virtual machine, a object data occupy contiguous memory locations.

The region of memory in which objects are allocated dynamically is often called a heap . In Chapter gif we consider heaps and heap-ordered trees in the context of priority queue implementations. Unfortunately, the only thing that the heaps of Chapter gif and the heap considered here have in common is the name. While it may be possible to use a heap (in the sense of Definition gif) to manage a region of memory, typical implementations do not. In this context the technical meaning of the term heap is closer to its dictionary definition--``a pile of many things.''

The amount of memory required to represent a Java object is determined by the number and the types of its fields. For example, fields of the primitive types, bool, char, byte, short, int, and float typically occupy a single, 32-bit word whereas long and double both require two words of storage. A field which refers to an object or to an interface typically requires only one word.

In addition to the memory required for the fields of an object, there is usually some fixed, constant amount of extra storage set aside in every object. This extra storage carries information used by the Java virtual machine to make sure that object is used correctly and to aid the process of garbage collection.

Every object in a Java program is created explicitly by invoking the new operator. Invoking the new operator causes the Java virtual machine to perform the following steps:

  1. An unused region of memory large enough to hold an instance of the desired class is found.
  2. All of the fields of the object are assigned their default initial values.
  3. The appropriate constructor is run to initialize the object instance.
  4. A reference to the newly created object is returned.

next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.