-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
54 lines (43 loc) · 1.24 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Example program that demonstrates how the finalizer queue can be blocked by a long running finalizer causing a memory leak
// Try to run the program with the finalizer commented out and see how the memory usage doesn't grow
var exit = false;
while (!exit)
{
GeneratePeople();
Console.WriteLine($"Memory used: {GC.GetTotalMemory(false):N0} bytes");
Console.WriteLine("Leak more memory (y/n)");
var answer = Console.ReadKey();
exit = answer.Key != ConsoleKey.Y;
Console.WriteLine();
}
Console.WriteLine("Bye!");
return;
void GeneratePeople()
{
var people = new List<Person>();
for (var i = 0; i < 10_000; i++)
{
people.Add(new Person(
id: i,
firstName: $"John #{i}",
lastName: "Doe"));
}
}
internal sealed class Person
{
internal Person(int id, string firstName, string lastName)
{
Id = id;
FirstName = firstName;
LastName = lastName;
}
public int Id { get; }
public string FirstName { get; }
public string LastName { get; }
private char[] Details { get; } = new char[10_000];
~Person()
{
Console.WriteLine($"Finalizing person {Id}");
Thread.Sleep(5000); // Simulate a long operation
}
}