Mark and Sweep
Table of contents
· What do you understand by Mark and Sweep?
· Mark and Sweep and its Description
o Mark Phase
o Sweep Phase
· Example considered to elucidate Mark and Sweep
· Pros and Cons of Mark and Sweep Algorithm
· Mark and Sweep (vs) Stop and Copy Algorithm
· Mark and Sweep vs Reference Counting
· Justification of Mark and sweep in C language and C#
· Applications and accomplishment of Mark and Sweep
· Clarification of Concurrent Mark Sweep
What do you get by Mark and Sweep?
In the language of computer programming, mark and sweep turn out to be an algorithm which is expended in garbage compilation. In the main in C++ and Java, we generate objects and a quantity of them may linger idly but the objects which ever were produced use to dwell in some load memory and if we call for to generate new-fangled objects, then, there may not be an adequate amount of memory that is, we may run out of breathing space and in such cases there is a great requirement to clear or get rid of those unused objects but doing it by hand by the programmer accepts a lot of time and effort. As a result, we have mark and sweep algorithm does bring together the garbage values.
Mark and Sweep and its Elaboration
If there lives a garbage collection algorithm, after that two fundamental operations that it should carry out are as follows. At the outset, it should locate all the inaccessible and fresh objects. Secondly, the algorithm should be talented enough to get better all the memory expanded by those inaccessible objects. Subsequently, both of the above-observed operations are executed by mark and sweep algorithm. Mark and sweep algorithm has two stages to execute garbage collection. They are 1. Mark phase and 2. Sweep phase. We are attending to talk about both the phases in detail as follows:
For every object produced, there is a trifle attributed to it called up as mark bit and mark bit for an object just produced will be position to zero (0). In the initial phase i.e., mark phase, the programmer adjusts the mark bit for all the objects that are found out to be approachable to 1. Therefore, as already observed that in mark phase, all the unreachable objects are set up and collide with, and to do so, we call for a reference to all the objects in attendance and then we can get to be familiar with if any exacting object is in application or not. We are in great need to carry out a graph traversal and we may make use of a profundity first search strategy for this. Look at the subsequent figure, which demonstrates both available objects expending arrows and unreachable objects which are not associated to the root.
One can very well make out that every object is looked at as a node in this traversal process and all the nodes which can be extended to be called and the traversal goes forward till it visits all the available nodes or objects.
We can very well believe the below algorithm for the operation that is knocked off at the mark phase.
//algorithm in mark phase//
void Mark(Object* pObj)
if (!Marked(pObj)) // Noted returns the marked flag from object header
MarkBit(pObj); // Chumps the flag in obj header
// Acquire list of references that the current object has
// and recursively mark them too
ObjectCollection children = pObj->GetChildren();
for(int i = 0; i < children.Count(); ++i)
Mark(children[i]); // recursively call mark
}// we have presumed that there is only one root variable in this case but if there subsists more than one root, we require to call Mark() for all the accessible root variables.
In the second phase, i.e., the sweep phase, the name itself suggests what it is about to do. In this phase, all the fresh or out-of-the-way objects are wiped out. Moreover, all the memory that has been engaged by these inaccessible objects is authorized and is all set to be apportioned to some other lately produced objects. Not only this, all the commemorated bits that are the position to false or zero are now authorized just after the traversal from the mound and the algorithm in sweep phase can be scripted as follows:
//Algorithm in Sweep phase//
Object *pHeap = pHeapStart;
while(pHeap < pHeapEnd)
Free(pHeap); // put it to the free object list
Example considered giving details for Mark and Sweep
Consequently, let us consider the following example wherein which the acting upon of mark and sweep algorithm is identified. Let us take for granted seven objects A, B, C, D, E, F, G and some of them are colligated to each other and some are not. Root R serves to be the pedestal object from which the traversal commences. Look at the following connection of objects.
On top of the figure of objects connection, to locate what objects are available, the traversal of objects commences from root R and afterward to A as there is an association. At this point, connection constitutes that there stays a reference sandwiched between those two objects. Accordingly, the traversal of objects happens until it arrives at all the nodes which are found connected in the most effective manner. Except, we hardly have no relationship between C and E, F or between any other object that is linked up to the root and E, F. That very well thinks of that the traversal of E, F does not come about as they are not got in touch. Thus, these two objects are looked at as not accessible even if they are got in touch themselves. So, all of these objects are swept away from the memory as per the algorithm and memory lodged in by these two objects that got exempted.
Merits and Demerits of Mark and Sweep Algorithm
· Mark and sweep algorithm can very well grip the case of cyclic references. Nothing like some algorithms of garbage collection, mark and sweep algorithm does wind up in an infinite loop even if cycles occur in traversal and that holds too much of the importance.
· Even, this algorithm does not experience added performance operating cost during the carrying out of this algorithm.
· While we run the garbage collecting algorithm, the most important program execution stops campaigning.
· After the implementation of the algorithm, all the unreachable objects are moved out from the memory and this consequences in allowing for the reachable objects stay well in the memory with distances in between the available objects i.e., they emerge to be disunited, Take a look at the below figure.
Moreover, all the white spaces in sandwiched between submitting that there rests unfilled space and is splitting the reachable objects which are presented by grey blocks apart.
Mark and Sweep (vs) Stop and Copy Algorithm
In this setting, we are attending well to take a look at two diverse garbage collecting algorithms and the difference rest in their functionality too. As we have previously talked about, mark and sweep accompany two phases in order to carry out garbage collection.
Stop and copy algorithm turns out to be an additional technique for the sake of assembling garbage values and this technique is also used to eliminate the fragmented memory which is demonstrated in the above figure. It is a must to understand that the main working principle of stop and copy algorithm is that it carves up the whole memory in use by objects into two halves. One half is acknowledged as the current half and the second half remains inoperative. Nothing like in mark and sweep algorithm, this technique does not enclose a liberated list to preserve or look for while getting rid of fragmentation. In stop and copy algorithm, memory is apportioned to new objects from the first half and when the memory in the first half is completely invaded, garbage collection is executed in the first half of memory.
Diagram evidencing stop and copy memory fragmentation elimination and understand writing the above paragraph and try to evaluate each sentence with this figure and you will acquire a clear idea of how stop and copy technique acts upon.
As of the mark and sweep, the collector got across the whole memory heap from the root node. After each node is spanned, the traversed node is reassigned to the second half of the heap which is found out to be idle. When the copy is completed, i.e., all the available nodes are now imitated into the idle heap, the designations of the two halves are changed over. The first half which is also known as the cut rent heap is interchanged as idle heap and the second heap is now modified to cut the rent heap. As a result, all the reachable nodes have been replicated but what about the unreachable nodes or objects? These inaccessible objects are left in the unoccupied heap at this point that means these are not simulated into the second half all along with reachable nodes.
Advantages of stop and copy:
· This technique productively eradicates fragmentation of memory.
Drawbacks of stop and copy:
The main disadvantage of this technique is that you are attending to waste half of the memory by separating it as a part of the technique.
Mark and Sweep vs Reference Counting
As we are by now conscious of mark and sweep let us obtain a glance at reference counting and how well-organized it is likened to mark and sweep has to be made out recognized. When objects are produced, in mark and sweep a traversal is executed to be familiar with all the reachable nodes and in reference counting continues a track on how many times an object has been cited and free-based on that count that it decides which object is inaccessible or unreachable. If number of references to an entity is zero, that intends that object is not reachable by any other object and is therefore unreachable.
Formal meaning of reference counting is that it is recognized to be a technique which entrepots the number of references or pointers or even does by a resource such as a disk space or a block of memory or any other resource. A reference counting algorithm may also pass on to a garbage collection algorithms which utilize such of reference counts. Indeed, if an object is unapproachable and is ruined, afterward, the reference count of all the objects that had looked up to put down object will be reduced by one. So, destructing one object ultimately contributes to a great number of objects getting dislodged.
Advantages of Reference Counting:
· In reference counting, objects are recalled as soon as they can no more are cited by other objects and are remembered in an incremental fashion. Every object has its lifetime undoubtedly determined in reference counting.
· This technique is followed up for the reason that it is one of the simplest in memory management.
Disadvantages of Reference Counting:
A prominent disadvantage in reference counting is that it can’t become aware of objects that include a cycle of reference. For example, if A and B are two objects that have reference to each other but they are not mentioned or found not in contact with the root node and other objects that are in contact with the root node. Thus, this technique is never going to provide the reference count to objects A and B as zero as they are being mentioned by each other.
Enlightenment of Mark and Sweep in C language and C#
In C programming, memory gets apportioned to objects by the function malloc(). So, to start on with composing a Garbage Collector, we first call for to inscribe a memory allocator i.e., malloc(). Every now and then, memory is apportioned energetically and this dynamically allocated memory lodges in heap.
So, Mark and Sweep in C language work as follows:
We call for to be familiar with two terms related to this Used and free list. A Used list is that whose memory is being at present used and free list is the total accessible space/memory which is idle. First of all, we need to effort to the way in for any memory location that we can obtain. Secondly, all the variables are stashed away at the memory location that you were previously talented enough to access.
Figuring out Vehicle Routing problem using Mark and Sweep
Vehicle Routing Problem expresses that “come across the optimal set of routes that top suit for a group of vehicles to transport its passengers to the fortune”. It is to some extent similar to the traveling salesman problem. Consequently, the chief objective of the vehicle routing problem is to reduce the total cost in arriving at the providence. As look at the following figure in which three optimal routes are to originate from the warehouse and to resolve a vehicle routing problem, we have a lot of approaches like ‘branch and bound’, ‘branch and cut’ etc.
In vehicle Routing Problem, best nodes out of all the possible nodes should be preferred for a single optimal route and the other nodes are not looked at to participate in constituting the optimal route.
Applications and execution of Mark and Sweep
Purposely, Mark and Sweep algorithm are habituated in programming languages in order to get rid of the glut space that is expended by objects that are not practical enough. It very well has in mind to discharge the space being invaded by objects that are no longer in use or being looked up. It is habituated in many software applications in order to open the accessible memory space period and perk up the presentation of the software application.
Mark and Sweep technique gets enforced in two phases. They are marked phase and sweep phase. Mark phase discovers all the unapproachable objects by traversing the complete tree of objects and then carves up all the inoperative objects. Sweep phase assists in erasing all these gathered up inaccessible objects from the memory and discharge the memory which can be used to assign for some other purposes.
Elucidation of Concurrent Mark-Sweep
Concurrent Mark Sweep collector is noted to be a garbage collector that is built up or planned in particular for those applications that search for to encompass fewer intermissions in garbage collection. A concurrent mark sweep collector is articulated to be generational and executes both minor and major garbage collections. CMS exercise a break-up garbage collector threads to outline for all reachable objects concurrently along with the carrying out of application threads and attempts hard to lessen the pause time too. During each and every cycle of garbage collection, the concurrent mark sweep collector interruptions the carrying out of all the application threads for a convinced period at the beginning and in the middle of the collection. There is a new term that we call for to be aware of that is concurrent mode stoppage which is a message established when you make use of a low pause collector in garbage collection. It means when the new generation is being occupied too fast and as a result, it is overrunning to the tenured generation of objects but in the meantime if the CMS could not clear out the upgraded generation in the background. This is predicted Concurrent mode failure.
And then, among both the pauses, the pause that happens in the middle of the collection inclines to be a more prospicient one and during these pauses a manifold number of threads are used to do the compilation process during this pause time. We have to keep in mind that the CMS collector at most of the times dispatches the sweeping of inaccessible objects without stopping the implementation of the application threads. If you appear at the following figure, you can watch the dissimilarity of pausing in serial parallel and concurrent garbage collection. The blue arrows symbolize the implementation of application and the arrows in light red represent the pauses.
If too much time is exhausted in garbage collection, then an error “Out of Memory” is bewildered by the CMS collector, that is, if more than 98 percent of time is dropped in only garbage collection and 2 percent in upturn of memory, then this error is demonstrated. This particular feature is in particular used in forestalling the applications running for overtime and these applications even may not build any growth owing to minute size of memory. In any case, if one wants to take away this feature, they can only add the option “UseGCOverheadLimit“ to the command line and the indicating of error “outofmemoryerror” will be out of action.
If you need more help in mark-and-sweep-homework-helpClick here