Garbage (computer Science) - Eliminating Garbage

Eliminating Garbage

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

  • Many operating systems will reclaim the memory and resources used by a process or program when it terminates. Simple or short-lived programs which are designed to run in such environments can exit and allow the operating system to perform any necessary reclamation.
  • In systems or programming languages with manual memory management, the programmer must explicitly arrange for memory to be deallocated when it is no longer used. C and C++ are two well-known languages which support this model.
  • Garbage collection uses various algorithms to automatically analyze the state of a program, identify garbage, and deallocate it without intervention by the programmer. Many modern programming languages such as Java and Haskell provide automated garbage collection. However, it is not a recent development, as it has also been used in older languages such as LISP.
  • There is ongoing research to type theoretic approaches (such as region inference) to identification and removal of garbage from a program. Note that no general type-theoretic solution to the problem has been developed.

An example of the automatic removal of semantic 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 "", 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.

Read more about this topic:  Garbage (computer Science)

Famous quotes containing the words eliminating and/or garbage:

    A favorite of outdoor alcoholics, connoisseurs and Fundamentalists, these pills turn water into wine. In 10 minutes the most fetid swamp scum in the forest can become modest red, elusive and light on first taste, yet playful—one might say a trifle impudent—on the afterbite. Saves pack space by eliminating need for bulky corkscrew, decanter and bottles. Store pills on their sides in a cool dark place.
    Alfred Gingold, U.S. humorist. Items From Our Catalogue, “Wine Pills,” Avon Books (1982)

    To be an editor, as I was.
    Then to lie here close by the river over the place
    Where the sewage flows from the village,
    And the empty cans and garbage are dumped,
    And abortions are hidden.
    Edgar Lee Masters (1869–1950)