0
votes

I am doing some practice test questions and one of them is as follows:

You are creating a class named Data that includes a dictionary object named _data. You need to allow the garbage collection process to collect the references of the _data object. How should you complete the relevant code?

public class Data
{
    <replace1>
    public Data(int count)
    {
        for (int i = 0; i < count; i++)
        {
            <replace2>
        }
    }
}

Here are the options:

static Dictionary<int, WeakReference> _data;
static Dictionary<int, Int32> _data;
_data.Add(i, new WeakReference(new Class(i*2), false));
_data.Add(i, (Int32)(i*2));

Since, basically, the question is "Should you use a WeakReference or a struct reference if you want the Garbage Collector to be able to collect your objects?" I thought the Int32 struct would be a better choice, since it references no object on the heap and thus the object can be garbage collected immediately, even though weak references do not keep an object alive if there are no strong references to it (as far as I understood it) I still thought the struct would be a better answer but the practice test says the WeakReference is the correct answer. Mind you this practice test has been wrong before even for the most obvious and simple matters.

Could you explain why, in this case, a WeakReference would be better suited than a struct if we want the GC to be able to collect our object?

1
Yeah I think it's certainly a case the test being wrong again. They should explain why they think it's necessary to GC AppDomain-wide static objects - Micky
Why couldn't they just ask a multiple choice question to see if you know what WeakReference is? That achieves the same result with less uncertainty. I don't like answering questions when it looks like they haven't been thought out, and I realize that they might have the answer to their own question wrong based on factors they haven't considered. Or all of the answers are wrong and you have to guess which one they thought was right, and why. And to really split hairs, neither dictionary is instantiated. - Scott Hannen
@ScottHannen, the dictionaries are static - J. Doe
@J.Doe, yes, but they're both null. We can understand the intent, but it just makes me wonder to what extent they thought through the details when they (whoever created the test) wrote the question. There's a lot of indications that they didn't, which would lead me to wonder what they did think through. - Scott Hannen

1 Answers

1
votes

To enable the GC to collect the objects you have to use WeakReference because it does not hold a strong reference to your objects. See: https://docs.microsoft.com/de-de/dotnet/api/system.weakreference

In your case the struct will be placed on the heap because you are storing it in a Dictionary which is obviously stored on the heap. Therefore a strong reference will be created and your struct doesn't get collected until you remove it from the dictionary.

You commonly use a WeakReference when you want to cache large objects but you don't need them to be accessible all the time. So the WeakReference allows the GC to collect the object when your program needs the storage elsewhere.