Mark-and-sweep garbage collection

Source: Internet
Author: User

This section presentsMark-and-sweepGarbage collection algorithm. The mark-and-sweep algorithm was the first garbage collection algorithm to be developed that
Is able to reclaim cyclic data structures. Variations of the Mark-and-sweep
Algorithm continue to be among the most commonly used garbage collection techniques.

When using Mark-and-sweep, incluobjects are not reclaimed immediately. Instead, garbage is allowed to accumulate until all available memory has been exhausted. When that happens, the execution
Is sushortded 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.

The mark-and-sweep algorithm is calledTracingGarbage Collector because isTraces outThe 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 calledRoots.
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 beLive. Conversely, an object which is notLiveIs 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 calledMarkPhase. In the second phase, the garbage collection Algorithm
Scans through the heap and reclaims all the unmarked objects. The second phase is calledSweepPhase. The algorithm can be expressed as follows:

for each root variable r    mark (r);sweep ();

In order to distinguish the live objects from garbage, we record the state of an object in each object. That is, we add a specialBooleanField to each object called, say,Marked. By default, all
Objects are unmarked when they are created. Thus,MarkedField is initiallyFalse.

An objectPAnd all the objects indirectly accessible fromPCan be marked by using the following recursiveMarkMethod:

void mark (Object p)

if (!p.marked)

p.marked = true; for each Object q referenced by p mark (q);

Notice that this recursiveMarkAlgorithm does nothing when it encounters an object that has already been marked. Consequently, the algorithm is
Guaranteed to terminate. And it terminates only when all accessible objects have been marked.

In its second phase, the Mark-and-sweep algorithm scans through all the objects in the heap, in order to locate all the unmarked objects. the storage allocated to the unmarked objects is reclaimed during the scan.
At the same time,MarkedField on every live object is set backFalseIn preparation for the next invocation of the Mark-and-sweep garbage collection algorithm:

void sweep ()

for each Object p in the heap

if (p.marked) p.marked = false else heap.release (p);

Figure extends strates the operation
Of the Mark-and-sweep garbage collection algorithm. Figure (a) shows the conditions
Before garbage collection begins. In this example, there is a single root variable. Figure (B)
Shows the effect ofMarkPhase of the algorithm. At this point, all live objects have been marked. Finally, figure (c)
Shows the objects left afterSweepPhase has been completed. Only live objects remain in memory andMarkedFields have all been setFalseAgain.

 
Figure:Mark-and-sweep garbage collection.

Because the Mark-and-sweep garbage collection algorithm traces out the set of objects accessible from the roots, it is able to correctly identify and collect garbage even in the presence of reference cycles. This
Is the main advantage of mark-and-sweep over the reference counting technique presented in the preceding section. A secondary benefit of the Mark-and-sweep approach is that the normal manipulations of reference variables incurs no overhead.

The main disadvantage of the Mark-and-sweep approach is the fact that normal program execution is suincluded while the garbage collection algorithm runs. In particle, this can be a problem in a program that
Interacts with a human user or that must satisfy real-time execution constraints. For example, an interactive application that uses mark-and-sweep garbage collection becomes unresponsive periodically.

Original address:
<Link>

The download address of the book:
<Link>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.