Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D custom list removal, garbage collector

Suppose I have a two-way linked list. I want to remove a sequence from it like this:

begin - element - element x element - element - element x element - end
                        \_________________________________/
-: reference
x: removed reference

I would like to know if I also should remove the references between the elements that are to be removed, or if the garbage collector handles it in a better way.

like image 868
weltensturm Avatar asked Dec 17 '25 23:12

weltensturm


2 Answers

the standard garbage collector uses a mark and sweep algorithm which can handle circular references so there is no need to null those (only the references from reachable memory to unreachable should be nulled)

like image 175
ratchet freak Avatar answered Dec 19 '25 15:12

ratchet freak


You do not need to remove those references. The garbage collector can trivially determine that the three elements that you are removing are no longer reachable, so there is no need to scan them for possible pointers. In other words, there is no performance gain in doing so.

To see why this is the case, consider the first algorithm presented in this blog post: http://xtzgzorex.wordpress.com/2012/10/11/demystifying-garbage-collectors/


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!