J
Jason Callas
I was doing starting some experimenting with the GC and ran into the following odd result. I created an object and my used memory went up by almost 11k but when I cleared it and forced a collection the used memory only went down by 12 bytes.
Any ideas on what else is being created or not cleaned up?
Code ==>
Sub Main()
' The following VB.NET code shows how memory is allocated and deallocated during object creation and destruction.
Console.WriteLine("Memory used before object creation : " & GC.GetTotalMemory(True))
Dim obj1 As New Object
Console.WriteLine("Memory used after object 1 creation : " & GC.GetTotalMemory(True))
Dim obj2 As New Object
Console.WriteLine("Memory used after object 2 creation : " & GC.GetTotalMemory(True))
obj1 = Nothing
GC.Collect()
Console.WriteLine("Memory used after cleanup : " & GC.GetTotalMemory(True))
Console.ReadLine()
End Sub
Output ==>
Memory used before object creation : 8168
Memory used after object 1 creation : 19100
Memory used after object 2 creation : 19112
Memory used after cleanup : 19100
You can see from the increase between object 1 and 2 that a new variable of type Object only costs 12 bytes. You also see that the value drops by 12 when the object is destroyed and garbage collected.
Any ideas on what else is being created or not cleaned up?
Code ==>
Sub Main()
' The following VB.NET code shows how memory is allocated and deallocated during object creation and destruction.
Console.WriteLine("Memory used before object creation : " & GC.GetTotalMemory(True))
Dim obj1 As New Object
Console.WriteLine("Memory used after object 1 creation : " & GC.GetTotalMemory(True))
Dim obj2 As New Object
Console.WriteLine("Memory used after object 2 creation : " & GC.GetTotalMemory(True))
obj1 = Nothing
GC.Collect()
Console.WriteLine("Memory used after cleanup : " & GC.GetTotalMemory(True))
Console.ReadLine()
End Sub
Output ==>
Memory used before object creation : 8168
Memory used after object 1 creation : 19100
Memory used after object 2 creation : 19112
Memory used after cleanup : 19100
You can see from the increase between object 1 and 2 that a new variable of type Object only costs 12 bytes. You also see that the value drops by 12 when the object is destroyed and garbage collected.