This chapter introduces you to garbage collection, the process that Java uses for managing unused memory. Every object created in Java uses memory. Garbage collection ensures that when your program is finished using an object, the memory is freed. This decreases the chance of creating an unwanted memory leak in Java code, although memory leaks can still occur in some situations. It also greatly simplifies the design and implementation of code, as opposed to C and C++, in which programmers spend precious time manually programming memory management.
Although the Java specification doesn't require it, most implementations of the Java Virtual Machine (JVM) use a mark-sweep garbage collection system. In this system, objects become eligible for deletion as soon as the last reference to them drops, but they aren't actually deleted until free memory is exhausted. When the system determines that it needs memory, it deletes any object that it determines is no longer in use. This takes the deletion control out of the hands of the programmer. The object's finalizer is called to alert the programmer that an object is about to be deleted. The finalizer of the object is simply a method of an object that is called just before the object is deleted; the finalizer for an object isn't required and is often omitted.
The Java language provides some built-in routines for controlling garbage collection: the methods System.gc() and System.runFinalization(). System.gc() requests that garbage collection be run. System.runFinalizers() asks that finalizers be executed, but that memory not necessarily freed. We'll discuss the difference later in the chapter. We'll also talk about the new classes in java.lang.ref that you can use for more advanced memory management.
Although garbage collection simplifies the writing of Java code, it is not an excuse to become lazy. Garbage collection imposes some trade-offs, and you still have to make decisions so the system works efficiently. After you read this chapter, you'll be able to make those decisions with certainty.
Garbage collection is nothing new; it has been used in languages such as Lisp and Smalltalk for many years. When an application runs, it uses memory. Memory is one of the most basic computer resources and tends to be one of the most limited. The action of creating a new object is the biggest use of memory in a Java application. Because it is difficult to tell how many objects are going to be created when a program runs, it is difficult to tell how much memory a program will need.
Java manages memory in a structure called a heap. Every object that Java creates is allocated in the heap, which is created when the application begins and is managed automatically by the JVM. Java attempts to ensure that there is always enough memory in the heap to create a new object through a process called garbage collection.
The basic idea behind a garbage collection system is simple: if memory is allocated, it eventually has to be freed. There is only so much memory on a computer?even today's modern machines with huge amounts of memory have an upper limit. If a program repeatedly allocates memory without freeing it, the system will eventually run out of memory and the program will fail.
The problem with freeing memory is that it can be very difficult to determine when memory should be freed. It is always clear when memory is allocated; each object can be tracked down to the single new statement that created it. It is not as clear when an object is no longer being used. An object may be in use by many different parts of a program at once; determining which of these parts is the last one to use the object can be impossible to figure out before the program runs.
Consider a class that represents a company with employees. Each employee has a reference to the company. There are an unknown number of employees in the system. If all the employees for the company are freed, the company is no longer being used and should be freed. However, the company should not be freed until all the employees have been freed.
Download your Reports for Distributed Garbage Collection