I am setting some quiz questions. There have been some questions raised about the following question. I am not sure if this is the right place but I would really appreciate any comment. Mostly I would like to know if the question is the right to evaluate someone's understanding of LOH.
Suppose a Dictionary type collection contains 1000 objects and takes 100kb of memory and is short-lived. Which one is true?
Suppose a Dictionary type collection
contains 1000 objects and takes 100kb of memory
If you mean Dictionary<TKey, TValue> it's not the object itself that goes on the LOH, nor holds the objects directly in the dictionary as the instance itself would not exceed the threshold for LOH promotion; it's the underlying array that goes on the LOH. Even if this is not the Dictionary<TKey, TValue> it is extremely rare to see an object that is not an array/string going on LOH.
The object is assigned to GEN0 heap
Goes on the LOH by default (a.k.a. Gen3, but it rarely is referred as Gen3 because it has a proper name), but you could change the LOH threshold to be greater than 100KiB.
The object can be efficiently garbage collected
Define "Efficiently"? The GC should do its best to collect them as efficiently as possible 馃檪. It's somewhat guessable that higher generation collections would be expensive than lower generations but they still are efficient because different strategies are employed to maximise performance for the kind of objects they hold.
The object is compacted
None of the collection in any generation compacts the object itself. Maybe you meant the heap? Even if so, you could allow LOH to be compacted.
This type object can potentially shrink usable memory over time
The object itself never changes in size. The underlying array instance would never change in size as well; While the Dictionary itself can allocate a new smaller array by the user calling then copy the contents to reduce memory footprint by the user calling TrimExcess this does not happen automatically. (At this point it also is not a GC/LOH question anymore)
I'm not sure if this type of question is appropriate for this repo, honestly. It likely is better suited for other .NET community like the official Gitter or StackOverflow 馃檪
(By the way, this is why I don't like this "true or false" type of quiz/questions; the questions are often too vague to be answered in binary answer.)
thank you @Gnbrkm41 for providing such great answers. I'll close this now.
Most helpful comment
If you mean
Dictionary<TKey, TValue>it's not the object itself that goes on the LOH, nor holds the objects directly in the dictionary as the instance itself would not exceed the threshold for LOH promotion; it's the underlying array that goes on the LOH. Even if this is not theDictionary<TKey, TValue>it is extremely rare to see an object that is not an array/string going on LOH.Goes on the LOH by default (a.k.a. Gen3, but it rarely is referred as Gen3 because it has a proper name), but you could change the LOH threshold to be greater than 100KiB.
Define "Efficiently"? The GC should do its best to collect them as efficiently as possible 馃檪. It's somewhat guessable that higher generation collections would be expensive than lower generations but they still are efficient because different strategies are employed to maximise performance for the kind of objects they hold.
None of the collection in any generation compacts the object itself. Maybe you meant the heap? Even if so, you could allow LOH to be compacted.
The object itself never changes in size. The underlying array instance would never change in size as well; While the Dictionary itself can allocate a new smaller array by the user calling then copy the contents to reduce memory footprint by the user calling
TrimExcessthis does not happen automatically. (At this point it also is not a GC/LOH question anymore)I'm not sure if this type of question is appropriate for this repo, honestly. It likely is better suited for other .NET community like the official Gitter or StackOverflow 馃檪
(By the way, this is why I don't like this "true or false" type of quiz/questions; the questions are often too vague to be answered in binary answer.)