Mark-and-Sweep Garbage Collection Algorithm in JavaGarbage collection algorithms, such as mark and sweep, run in the background to manage memory in programming languages like C++ and Java. When objects are created dynamically, they occupy memory in the heap. However, if we keep creating objects without freeing memory, it can lead to Out Of Memory errors. To prevent this, garbage collection automatically releases memory for objects no longer referenced or unreachable in the program. It alleviates the burden of memory management from the programmer, as the garbage collector deallocates memory for unreferenced objects, ensuring available space for new objects to be allocated. Mark and Sweep AlgorithmThe mark-and-sweep garbage collection algorithm is a simple and efficient algorithm used to reclaim memory no longer being used by an application. The algorithm works by first marking all objects that are still in use. Once all of the live objects have been marked, the garbage collector sweeps through the heap and reclaims all the unmarked objects. 1. Marking phase: When an object is created, the mark bit is set to 0(false). The marking phase is responsible for identifying all of the live objects in the heap. It is done by traversing the heap and marking all of the objects that are reachable from the roots of the heap. The heap's roots are the objects directly accessible to the application, such as local variables and object references. The root variable is a variable that refers to an object that is accessible from the local variable. We assume that there is only one root. Algorithm 2. Sweeping phase: The sweeping phase is responsible for reclaiming all unmarked objects in the heap. It is done by freeing the memory allocated to the unmarked objects. Algorithm: The mark-and-sweep algorithm is called a "tracing garbage collector" because it systematically traces and identifies all objects that can be reached directly or indirectly from the program's root references. Example:
Advantages of Mark and Sweep Algorithm
Disadvantages of Mark and Sweep Algorithm
The green squares in the image represent objects that are reachable from the roots. The white squares represent objects that are not reachable from the roots. Fragmentation is a problem that occurs when small, unused memory regions are scattered throughout the heap. It can happen when the garbage collector reclaims memory from no longer-needed objects. If the garbage collector does not compact the heap, these small, unused memory regions can prevent allocating new objects. Compaction is rearranging the objects in the heap so that the free memory regions are contiguous. It can be done by moving the objects around in the heap or copying them to a new location. Compaction can reduce fragmentation and improve the performance of the garbage collector. The garbage collector can quickly find free memory regions when the heap is compacted. In the example you provided, five free memory regions of sizes 1, 1, 2, 3, and 5 units exist. These free memory regions are fragmented, meaning that they are not contiguous. It prevents the allocation of an object that requires 10 units of memory. If the heap was compacted, the five free memory regions would be combined into a single free memory region of size 12 units. It would allow the allocation of the object that requires 10 memory units. Compaction can be time-consuming, so it is only sometimes performed by the garbage collector. However, compaction can be beneficial for applications that experience frequent fragmentation. Next TopicShadowing of Static Functions in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India