20,741
edits
m (→Problem) |
m (→Problem) |
||
| Line 7: | Line 7: | ||
In other words, the time spent in the GC is proportial to the number of active objects (i.e. lines of code), even for objects which may not be running or active - just loaded. For additional background information, please see [[Improving Nasal]]. | In other words, the time spent in the GC is proportial to the number of active objects (i.e. lines of code), even for objects which may not be running or active - just loaded. For additional background information, please see [[Improving Nasal]]. | ||
= Mark/Sweep = | |||
When using a mark-and-sweep collector, unreachable objects are not immediately reclaimed. Instead, garbage is allowed to accumulate until all available memory has been exhausted. When that happens, the execution of the program is suspended temporarily while the mark-and-sweep algorithm collects all the garbage. Once all unreferenced objects have been reclaimed, the normal execution of the program can resume. | |||
Obviously, the main disadvantage of the mark-and-sweep approach is the fact that that normal program execution is suspended while the garbage collection algorithm runs. In particular, this can be a problem in a program that must satisfy real-time execution constraints (like a flight simulator). For example, an interactive application that uses mark-and-sweep garbage collection becomes unresponsive periodically. | |||
The mark-and-sweep algorithm is called a tracing garbage collector because is traces out the entire collection of objects that are directly or indirectly accessible by the program. The objects that a program can access directly are those objects which are referenced by local variables on the processor stack as well as by any static variables that refer to objects. In the context of garbage collection, these variables are called the roots . An object is indirectly accessible if it is referenced by a field in some other (directly or indirectly) accessible object. An accessible object is said to be live . Conversely, an object which is not live is garbage. | |||
The mark-and-sweep algorithm consists of two phases: In the first phase, it finds and marks all accessible objects. The first phase is called the mark phase. In the second phase, the garbage collection algorithm scans through the heap and reclaims all the unmarked objects. The second phase is called the sweep phase. | |||
= Objective = | = Objective = | ||