+ 21

Which algorithm is used by garbage collector to remove the unused variables or object from memory?

10th Dec 2017, 6:19 AM
Abhivarshini Maddala
Abhivarshini Maddala - avatar
4 Answers
+ 17
Python uses the reference count algorithm to remove an object. The process is called garbage collection In summary, an object's reference count increases whenever it is assigned a new name or place x = 'abc' # reference count of 'abc' increased y = x # again reference count increased And, it decreases when, it is deleted with del, or it goes out of scope y = 'pqr' # reference count of 'abc' decreased del x # again reference count decreased Whenever, reference count reaches zero, python deletes the object automatically
10th Dec 2017, 6:43 AM
#RahulVerma
#RahulVerma - avatar
+ 9
To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a Mark and sweep algorithm.As you might intuit, it’s a straightforward, two-step process: The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive. All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects. Garbage collection is intended to remove the cause for classic memory leaks: unreachable-but-not-deleted objects in memory. However, this works only for memory leaks in the original sense. It’s possible to have unused objects that are still reachable by an application because the developer simply forgot to dereference them. Such objects cannot be garbage-collected. Even worse, such a logical memory leak cannot be detected by any software Even the best analysis software can only highlight suspicious objects. 
11th Dec 2017, 3:37 AM
Prabhat Thakur
Prabhat Thakur - avatar
+ 2
The very short version though is that  Garbage collectors determine which memory to retain and which to reclaim, using reachability through chains of pointers. _Garbage collector uses many algorithms but the most commonly used algorithm is mark and sweep. Garbage collection has two components: locating “live” objects and moving them so that there are no gaps. Locating live objects can be done by having each handle (pointer to the object) include a reference count. A non-zero reference count means that the object is still live. Alternatively, a depth first search algorithm can be used to locate live objects. There are also two basic methods of moving an object. One is called “mark and sweep” where all the live objects are located and their addresses sorted so that when they are moved, one object doesn’t inadvertently overwrite another. The other method is called “copying” garbage collection where a new block of memory is allocated for the objects and each live object is copied into the new area. This method is faster since each live object can be moved immediately without reference to any other object. However, it is wasteful of memory. The JVM tends to use a combination of both methods. Objects tend to have either a short life or a long life. New objects are placed in a “young generation” of memory where copying collection is employed. As the young generation fills up, “surviving” objects get placed into the “old generation” where mark and sweep is employed (infrequently).
23rd Dec 2017, 3:36 PM
Bits!
- 14
What is the automatic process by which unnecessary objects are deleted to free memory?
7th Apr 2020, 2:50 PM
Yeswanth Kota
Yeswanth Kota - avatar