Garbage (computer science)

Garbage, in the context of computer science, refers to objects, data, or other regions of the memory of a computer system (or other system resources), which will not be used in any future computation by the system, or by a program running on it. As computer systems all have finite amounts of memory, it is frequently necessary to deallocate garbage and return it to the heap, or memory pool, so the underlying memory can be reused.

Classification

Garbage is generally classified into two types: semantic garbage that is any object or data never accessed by a running program for any combination of program inputs, and syntactic garbage that refers to objects or data within a program's memory space but unreachable from the program's root set. Objects and/or data which are not garbage are said to be live.

Casually stated, syntactic garbage is data that cannot be reached, while semantic garbage is data that will not be reached. More precisely, syntactic garbage is data that is unreachable due to the reference graph (there is no path to it), which can be determined by many algorithms, as discussed in tracing garbage collector and only requires analyzing the data, not the code. Semantic garbage is data that will not be accessed, either because it is unreachable (hence also syntactic garbage), or reachable but will not be accessed; this latter requires analysis of the code, and is in general an undecidable problem.

Syntactic garbage is a (usually strict) subset of semantic garbage as it is entirely possible for an object to hold a reference to another object without the latter object being used.

Example

In the following simple stack implementation in Java, elements popped from the stack become semantic garbage once there are no outside references to them:[lower-alpha 1]

public class Stack {
  private Object[] elements;
  private int size;

  public Stack(int capacity) {
    elements = new Object[capacity];
  }
  public void push(Object e) {
    elements[size++] = e;
  }
  public Object pop() {
    return elements[size--];
  }
}

This is because there is still a reference to the object from elements, but the object will never be accessed again through this reference, since elements is private to the class and the pop method only returns references to elements it has not already popped (once size is decremented, that element will never be accessed again by this class). However, this requires analysis of the code of the class, which is undecidable in general.

If a later push call re-grows the stack to the previous size, overwriting this last reference, then the object will become syntactic garbage, since it is unreachable, and will be eligible for garbage collection.

Automatic garbage collection

An example of the automatic collection of syntactic garbage, by reference counting garbage collection, can be produced using the Python command-line interpreter:

>>> class Foo(object):
...     'This is an empty testing class.'
...     pass
... 
>>> bar = Foo()
>>> bar
<__main__.Foo object at 0x54f30>
>>> del bar

In this session, an object is created, its location in the memory is displayed, and the only reference to the object is then destroyed—there is no way to ever use the object again from this point on, as there are no references to it. This becomes apparent when we try to access the original reference:

>>> bar
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'bar' is not defined

As it is impossible to refer to the object, it has become useless: the object is garbage. Since Python uses garbage collection, it automatically deallocates the memory that was used for the object so that it may be used again:

>>> class Bar(object):
...     'This is another testing class.'
...     pass
... 
>>> baz = Bar()
>>> baz
<__main__.Bar object at 0x54f30>

Note that the Bar instance now resides at the memory location 0x54f30; at the same place as where our previous object, the Foo instance, was located. Since the Foo instance was destroyed, freeing up the memory used to contain it, the interpreter creates the Bar object at the same memory location as before, making good use of the available resources.

Effects

Garbage consumes heap memory, and thus one wishes to collect it (to minimize memory use, and allow faster memory allocation and prevent out-of-memory errors by reducing heap fragmentation and memory use).

However, collecting garbage takes time, and if done manually, requires coding overhead. Further, collecting garbage destroys objects and thus can cause calls to finalizers, executing potentially arbitrary code at an arbitrary point in the program's execution. Incorrect garbage collection (deallocating memory that is not garbage), primarily due to errors in manual garbage collection (rather than errors in garbage collectors), results in memory safety violations (often security holes) due to use of dangling pointers.

Syntactic garbage can be collected automatically, and garbage collectors have been extensively studied and developed. Semantic garbage cannot be automatically collected in general, and thus cause memory leaks even in garbage-collected languages. Detecting and eliminating semantic garbage is typically done using a specialized debugging tool called a heap profiler, which allows one to see what objects are live and how they are reachable, enabling one to remove the unintended reference.

Eliminating garbage

The problem of managing the deallocation of garbage is a well-known one in computer science. Several approaches are taken:

Notes

  1. ↑ Simplified from Effective Java Item 6 by omitting resizing and explicit exceptions.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.