According to Fundamentals of garbage collection, all threads except the one that triggered the garbage collection are suspended during garbage collection. Since the finalizers are called during the garbage collection process, I would expect the thread to be suspended until all the finalizers are executed. Thus, I would expect the following code to just "take longer" to complete. Instead, it throws a System.OutOfMemoryException
. Could someone elaborate on why this happens?
class Program
{
class Person
{
long[] personArray = new long[1000000];
~Person()
{
Thread.Sleep(1);
}
}
static void Main(string[] args)
{
for (long i = 0; i < 100000000000; i++)
{
Person p = new Person();
}
}
}
Wouldn't the creation of new Person
objects on the heap also be suspended while the finalizer is being executed?
Code taken from Exam Ref 70-483 Programming in C# (MCSD)