Re: Memory Usage

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: AlexS (salexru2000NO_at_SPAMsympaticoPLEASE.ca)
Date: 06/25/04


Date: Fri, 25 Jun 2004 13:13:01 -0400

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

"Jason Callas" <JayCallas@hotmail.com> wrote in message
news:e5KQ0FtWEHA.3944@tk2msftngp13.phx.gbl...
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.



Relevant Pages

  • Re: SBCL performance on OS X
    ... > program that just gobbles up memory until the Lisp process dies. ... between the speed of SBCL on an x86 and on a powerpc. ... garbage collection time is proportional to ... If you want to allocate memory ...
    (comp.lang.lisp)
  • Re: heap vs stack
    ... But I read about this statement about C++ object "The lifetime ... In C it is necessary to call mallocto allocate memory and freeto ... This gives the illusion of garbage collection in C++. ...
    (comp.lang.c)
  • Re: Memory allocation limits
    ... 700MB available does not mean it's all in one huge available chunk. ... When trying to allocate a huge amount of memory, ... And if it is not able right now, it will be able after garbage collection finishes. ...
    (microsoft.public.dotnet.framework)
  • Re: Memory Usage
    ... Not sure what the memory method is doing but it must be ... with brief comments at the beginning of my sample code. ... you might found out that console methods allocate more ... Even if you force garbage collection GC is not guaranteed to ...
    (microsoft.public.dotnet.general)
  • Re: Garbage Collection bug?
    ... I am not allocating memory until I get an OutOfMemoryException (in my ... real app that is - I do in the simple test app for question #2) ... The memory allocation behavior seems OK to me (allocate a lot to gain speed ...
    (microsoft.public.dotnet.framework.compactframework)