I’ve been looking into how garbage collector behaves in low memory environment. If I understand correctly, the GC uses CreateMemoryResourceNotification to subscribe to OS notification when it’s low on memory. It then calls, GCHeapUtilities::GetGCHeap()->GarbageCollect(0, TRUE);.
What I don’t understand is, why is it doing generation 0 garbage collection? I would expect that in low memory case, it would try to reclaim as much memory as possible, and thus run a generation 2 garbage collection.
I tried writing a test application that allocates around 512MB on managed heap (in chunks of 64kb, to avoid Large Object Heap allocation), and then releases all references to it. If I manually call GC.Collect(2), it releases everything I allocated. If I cause low memory situation, it does nothing. I have verified with debugger, that garbage collector gets called.