[Mono-dev] GC.Collect() CLR<>Mono difference.

Sunny sloncho at gmail.com
Thu Jan 8 12:45:04 EST 2009


On Thu, Jan 8, 2009 at 10:33 AM, Lucas Meijer <lucas at lucasmeijer.com> wrote:
> Here's another one.  (Unsure if these are considered ontopic. If not,
> please let me know)
>
> This small test program shows GC.Collect() + WeakReference working
> different on CLR
> than they do on Mono. In mono, the WeakReference is still alive after
> GC.Collect(),
> in the clr, the WeakReference is no longer active.
>
> using System;
>
> public class MonoTest2
> {
>    public static void Main()
>    {
>        var obj = new Version();
>        WeakReference reference = new WeakReference(obj);
>        obj = null;
>        GC.Collect();
>        Console.WriteLine("reference.IsAlive: " + reference.IsAlive);
>        //mono outputs true
>        //clr outputs false
>    }
> }
>
>
> If people think this is a a bug, I'll file a report.
>
> Bye, Lucas

According to this:
<http://msdn.microsoft.com/en-us/library/system.weakreference(VS.80).aspx>

IsAlive becomes false after the finalizer is done. It may be so, that
under mono your call to IsAlive is made before the finalizer is
called. Try with GC.WaitForPendingFinalizers() before the check.

-- 
Svetoslav Milenov (Sunny)

Even the most advanced equipment in the hands of the ignorant is just
a pile of scrap.


More information about the Mono-devel-list mailing list