Java Reference
In-Depth Information
a
A
c
B
d
e
C
f
g
h
Freelist
Figure12.16 Example of LISP list variables, including the system freelist.
point of view, that every so often the system must halt while it performs garbage
collection. For example, garbage collection is noticeable in the Emacs text edi-
tor, which is normally implemented in LISP. Occasionally the user must wait for a
moment while the memory management system performs garbage collection.
The Java programming language also makes use of garbage collection. As in
LISP, it is common practice in Java to allocate dynamic memory as needed, and to
later drop all references to that memory. The garbage collector is responsible for
reclaiming such unused space as necessary. This might require extra time when
running the program, but it makes life considerably easier for the programmer. In
contrast, many large applications written in C ++ (even commonly used commercial
software) contain memory leaks that will in time cause the program to fail.
Several algorithms have been used for garbage collection. One is the reference
count algorithm. Here, every dynamically allocated memory block includes space
for a count field. Whenever a pointer is directed to a memory block, the reference
count is increased. Whenever a pointer is directed away from a memory block,
the reference count is decreased. If the count ever becomes zero, then the memory
block is considered garbage and is immediately placed in free store. This approach
has the advantage that it does not require an explicit garbage collection phase, be-
cause information is put in free store immediately when it becomes garbage.
Reference counts are used by the UNIX file system. Files can have multiple
names, called links. The file system keeps a count of the number of links to each
file. Whenever a file is “deleted,” in actuality its link field is simply reduced by
one. If there is another link to the file, then no space is recovered by the file system.
When the number of links goes to zero, the file's space becomes available for reuse.
Search WWH ::




Custom Search