Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Criteria for triggering garbage collection in .Net

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
{
    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);
    }

    ~Foo()
    {
        // This finalizer is never called unless the rand.NextBytes
        // line in the constructor is uncommented.
    }

    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?

like image 969
Kennet Belenky Avatar asked Apr 19 '10 21:04

Kennet Belenky


People also ask

What triggers garbage collection?

When a JVM runs out of space in the storage heap and is unable to allocate any more objects (an allocation failure), a garbage collection is triggered. The Garbage Collector cleans up objects in the storage heap that are no longer being referenced by applications and frees some of the space.

What are the methods used in .NET framework for garbage collection?

. NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

How does garbage collection happen in C#?

The garbage collector (GC) manages the allocation and release of memory. The garbage collector serves as an automatic memory manager. You do not need to know how to allocate and release memory or manage the lifetime of the objects that use that memory.

When and how garbage collector is invoked?

When the JVM doesn't have necessary memory space to run, the garbage collector will run and delete unnecessary objects to free up memory. Unnecessary objects are the objects which have no other references (address) pointing to them.


1 Answers

The code runs at a stable 18MB on my machine, with or without that line (XP SP3 x86, .Net 3.5 SP1, dual core).

Likely what is happening on your machine is that when the line is commented, the program spends most of its time allocating, and manages to allocate too much memory before the garbage collector thread has a chance to deallocate it. When you uncomment that line, the program spends much less time allocating, and thus can't allocate too much before the GC thread runs.

Try replacing the commented line with Thread.Sleep(0); if it doesn't crash, I'm probably correct.


Just as a side note, you shouldn't ever rely on the finalizer - it is not guaranteed to be called immediately when the object is GC'ed, or even at all. Instead, in real code implement the IDisposable interface, and use a finalizer only if it is extremely important that Dispose() be called, even if the programmer forgot it (eg. releasing shared network/file resources, etc.)

like image 173
BlueRaja - Danny Pflughoeft Avatar answered Sep 30 '22 12:09

BlueRaja - Danny Pflughoeft