Previous      Up      Previous     Course Home   e-mail

2 Garbage Collection

 2.1 Reference Counting
 2.2 Mark and Sweep
 2.3 Generation-Based Collectors
 2.4 Incremental Collection

Garbage   

Objects on the heap that can no longer be reached (in one or more hops) from any pointers in the activation stack (i.e., in local variables of active functions)or from any pointers in the static storage area (variables declared in C++ as "static")are called garbage.

Garbage Example   


Garbage Collection   

Determining when something on the heap has become garbage is sufficiently difficult that many programming languages take over this job for the programmer.

The runtime support system for these languages provides automatic garbage collection, a service that determines when an object on the heap has become garbage anf automatically scavenges (reclaims the storage of) such objects.

Java has GC   

In Java, for example, although Java and C++ look very similar, there is no "delete" operator.

Java programmers use lots of pointers1 , many more than the typical C++ programmer.

But Java programmers never worry about deleting anything. They just trust in the garbage collector to come along eventually and clean up the mess.

C++ Does Not   

Automatic garbage collection really can simplify a programmer’s life. Sadly, C++ does not support automatic garbage collection.

But how is this magic accomplished (and why doesn’t C++ support it)? That’s the subject of the remainder of this section.

 Previous      Up      Previous     Course Home   e-mail