Re: Comparison<T, U> to Comparison<T>
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Tue, 03 Mar 2009 16:22:55 -0800
On Tue, 03 Mar 2009 15:43:20 -0800, Pavel Minaev <int19h@xxxxxxxxx> wrote:
[...]
Well, it turns out that the damn thing always prints "1 objects
alive", no matter how long you wait after the Collect call, and no
matter how many objects were created! Furthermore, I've printed the
index of that object, and it's always the last object in the array -
again, regardless of the size of the array.
Does anyone has any idea of what is going on here?
Sure. You're losing your mind. :)
Actually, I'm just kidding. As near as I can tell, the issue is that when you assign into the object[] as part of the argument to the WeakReference constructor, the compiler generates a hidden local variable. This local variable continutes to refer to the most recent object instantiated, preventing it from being collected.
You can see the generated local if you inspect the IL for your sample.
The really weird parts:
-- The compiler still generates this intermediate variable even for a "Release" build. I didn't play with the various compiler switches, but I would have thought that even the default "Release" config would suppress that. Seems like someone's relying a little too much on the JITter. :)
-- Speaking of the JITter, I'm very surprised that even the JIT-ted code doesn't mark the variable as unreachable past its last use. Considering that the fact that this optimization exists, and is the very reason for having GC.KeepAlive(), I'm a bit amazed that not having GC.KeepAlive() on the hidden local variable doesn't result in the local variable's reference being collected in spite of the variable's presence.
But, anyway...if you change the code so that you assign to your "srs" array element first, and then just use that element in the constructor for your WeakReference, everything works just as you think it should.
As for whether the code example sheds any light on the question of GC.Collect() being synchronous or not, I'd suggest it does show the method is synchronous. But, absent a more invasive inspection of the execution of the code (one showing exactly when each instruction in each thread of the process is executed), I'd admit that even your code example isn't proof of that.
Pete
.
- References:
- Comparison<T, U> to Comparison<T>
- From: jehugaleahsa@xxxxxxxxx
- Re: Comparison<T, U> to Comparison<T>
- From: Peter Duniho
- Re: Comparison<T, U> to Comparison<T>
- From: jehugaleahsa@xxxxxxxxx
- Re: Comparison<T, U> to Comparison<T>
- From: Pavel Minaev
- Re: Comparison<T, U> to Comparison<T>
- From: jehugaleahsa@xxxxxxxxx
- Re: Comparison<T, U> to Comparison<T>
- From: Peter Duniho
- Re: Comparison<T, U> to Comparison<T>
- From: Peter Duniho
- Re: Comparison<T, U> to Comparison<T>
- From: Pavel Minaev
- Re: Comparison<T, U> to Comparison<T>
- From: Peter Duniho
- Re: Comparison<T, U> to Comparison<T>
- From: Pavel Minaev
- Re: Comparison<T, U> to Comparison<T>
- From: Peter Duniho
- Re: Comparison<T, U> to Comparison<T>
- From: Pavel Minaev
- Comparison<T, U> to Comparison<T>
- Prev by Date: Re: Comparison<T, U> to Comparison<T>
- Next by Date: Re: SQLDateTime problem in c#
- Previous by thread: Re: Comparison<T, U> to Comparison<T>
- Next by thread: Re: Comparison<T, U> to Comparison<T>
- Index(es):
Relevant Pages
|
Loading