Which of these objects are eligible for garbage collection?

Both Random() instances and the WeakReference are eligible for collection: The first Random was not stored in a local, let alone a local that is later read The second Random was passed to a WeakReference so would be OK to collect anyway but the WeakReference itself is not held anywhere, so that too is eligible for collection None of the strings are (there are only 2 string instances here, not 4, even if every possible code path was reached): because they are literals in the c# code, they are interned once they exist.

Both Random() instances and the WeakReference are eligible for collection: The first Random was not stored in a local, let alone a local that is later read. The second Random was passed to a WeakReference, so would be OK to collect anyway, but the WeakReference itself is not held anywhere, so that too is eligible for collection. None of the strings are (there are only 2 string instances here, not 4, even if every possible code path was reached): because they are literals in the c# code, they are interned once they exist.

Yeah, obviously, the question was about the 'Random' objects, sorry, that I forgot to mention that. – Yippie-Kai-Yay 2 days ago Now, can one say that my answer is incorrect and is the right answer that both are? – Yippie-Kai-Yay 2 days ago Yes; clarified - see update – Marc Gravell?2 days ago 1 @Zarat the compiler won't invent locals without good reason; any part of the stack they are still in is opaque garbage and is treated as such – Marc Gravell?2 days ago 1 @MarcGravell - but that is an implementation detail.

And the compiler does switch to locals when the stack starts getting too deep. Also you can never be sure which stack values are treated as garbage, remember that the GC sees JITted machine code not IL. – Zarat 2 days ago.

It is true that this is implementation (and compiler) dependant. If all this is in the same method you cannot know which objects are still on the stack. Since objects on the stack are still referenced they wouldn't be collectible.

What the interviewer wanted you most likely to do is to check which objects are still reachable at the call of GC. Collect assuming a "perfect" implementation which discards everything as soon as possible.

This is important to know when coding unit tests involving GC behaviour, I'm speaking out of experience here. I explicitely made a difference between what the interviewer likely wanted to hear and what the technical reality is. – Zarat 2 days ago.

GC. Collect is like in Java equivalent to System.gc() It "recommends" to check for null values to delete them. You can't really depend on this feature since it's really automatic unlike C++.

Hope it helps!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions