Memory Usage

  • Thread starter Thread starter Jason Callas
  • Start date Start date
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.
 
Jason Callas said:
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?

I suspect the first call to Console.WriteLine is causing a load of
extra memory to be allocated. Try putting:

Console.WriteLine ("Startup")

before the current first Console.WriteLine and see if that makes a
difference.

Note that the object obj1 refers to is actually eligible for GC
immediately in release mode, as it's never read.
 
Hi, Jason

you won't get any sensible results using this sample IMO. Problem is that
apart from GC you might get some influence from JIT during runs. Depends on
settings and mode of compilation and execution. As Jon pointed out optimizer
can remove even some code from execution path.
Additionally, you might found out that console methods allocate more memory
than Object and use more resources. And last one, GetTotalMemory is
providing you with figure, which is "A number that is the best available
approximation of the number of bytes currently allocated in managed memory"
as MSDN says. Even if you force garbage collection GC is not guaranteed to
collect all inaccessible memory.

If you want to get real data and understanding, not approximations, you must
use profiler and performance counters for relevant objects.

HTH
Alex

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.
 
Thanks for replying.

After the post I did some more testing. After mixing and matching
Console.WriteLine statements and creating new variables and stuff it seems
that it is the call the GC.GetTotalMemory that actually is causing the large
jump. (I did find that Console.WriteLine seems to use up about 4k
sometimes.) Not sure what the memory method is doing but it must be
creating stuff internally. No biggie.

I ended up putting the following code

Console.WriteLine(">> Ignore this line = " & GC.GetTotalMemory(True) & " <<"
& vbCrLf)

with brief comments at the beginning of my sample code. (I am writing a
whitepaper (with sample code) for internal company use.)

Thanks.

- Jason
 
Jason,

that's why I recommend to use profiler and not such benchmarks. I missed the
elephant, which would be very prominent in profiler statistics on heap and
calls - GetTotalMemory :-)

HTH
Alex
 
How about allocating a million of objects instead. This way the error of overhead may be smaller and you can get more accurate result.
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.
 
Back
Top