*** Welcome to piglix ***

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.

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.

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

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.


...
Wikipedia

...