Criteria for triggering garbage collection in .Net

Posted by Kennet Belenky on Stack Overflow See other posts from Stack Overflow or by Kennet Belenky
Published on 2010-04-19T21:47:52Z Indexed on 2010/04/19 21:53 UTC
Read the original article Hit count: 181

I've come across some curious behavior with regard to garbage collection in .Net.

The following program will throw an OutOfMemoryException very quickly (after less than a second on a 32-bit, 2GB machine). The Foo finalizer is never called.

class Foo
{
    static Dictionary<Guid, WeakReference> allFoos = new Dictionary<Guid, WeakReference>();

    Guid guid = Guid.NewGuid();
    byte[] buffer = new byte[1000000];

    static Random rand = new Random();
    public Foo()
    {
        // Uncomment the following line and the program will run forever.
        // rand.NextBytes(buffer);
        allFoos[guid] = new WeakReference(this);
    }

    ~Foo()
    {
        allFoos.Remove(guid);
    }

    static public void Main(string args[])
    {
        for (; ; )
        {
            new Foo();
        }
    }
}

If the rand.nextBytes line is uncommented, it will run ad infinitum, and the Foo finalizer is regularly invoked. Why is that?

My best guess is that in the former case, either the CLR or the Windows VMM is lazy about allocating physical memory. The buffer never gets written to, so the physical memory is never used. When the address space runs out, the system crashes. In the latter case, the system runs out of physical memory before it runs out of address space, the GC is triggered and the objects are collected.

However, here's the part I don't get. Assuming my theory is correct, why doesn't the GC trigger when the address space runs low? If my theory is incorrect, then what's the real explanation?

© Stack Overflow or respective owner

Related posts about garbage-collection

Related posts about clr