D
D
Denis Bredun2020-07-31 20:38:15
C++ / C#
Denis Bredun, 2020-07-31 20:38:15

Do I need to implement the "Mark and Sweep" mechanism myself, or is it already in the garbage collector?

Let's say we have code:

class Program
{
 public static void Main()
 {
   Element el1 = new Element();
   Element el2 = new Element(){OtherElement = el1};
   el1.OtherElement = el2;
 }
}
class Element
{
 public Element OtherElement {get; set;}
}

Here we need the "Mark and Sweep" mechanism to remove el1 and el2. But I don't know if it's already in the garbage collector or if I need to implement it myself. Why did this question even come up? The garbage collector works on the basis of links: there is a link to the object - we do not touch the object, no - we free the memory. But is the garbage collector adapted to circular references, or do you have to write your own "Mark and Sweep" mechanism?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2020-07-31
@Luffy1

No need to write anything yourself, everything is already there from the box.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question