I met some curious behavior regarding garbage collection in .Net.
The following program will quickly throw an OutOfMemoryException (in less than a second on a 2-bit 32-bit 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() {
If the string rand.nextBytes is uncommented, it will run indefinitely, and the Foo finalizer will be called regularly. Why is this?
My best guess is that in the first case, CLR or Windows VMM are lazy in allocating physical memory. The buffer is never written, so physical memory is never used. When the address space ends, the system crashes. In the latter case, the system runs out of physical memory, before it leaves the address space, the GC starts up and objects are collected.
However, here is the part that I am not getting. Assuming my theory is correct, why doesn't the GC start when the address space is running low? If my theory is wrong, then what is the true explanation?
Kennet belenky
source share