.NET Framework Bookmark and Share   
 index > Common Language Runtime > Garbage Collector Behaviour
 

Garbage Collector Behaviour

Hi,

I'm seeing some behaviour with the garbage collector that I didn't expect and I wonder if anyone can shed any light on it. Here is some sample code (modified slightly from the Microsoft website):

Namespace GCCollectInt_Example
    Class MyGCCollectClass
        Private maxGarbage As Long = 10000

        Public Shared Sub Main()

            Dim myGCCol As New MyGCCollectClass

            'Determine the maximum number of generations the system
            'garbage collector currently supports.
            Console.WriteLine("The highest generation is {0}", GC.MaxGeneration)

            myGCCol.MakeSomeGarbage()

            'Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol))

            'Determine the best available approximation of the number 
            'of bytes currently allocated in managed memory.
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(False))

            'Perform a collection of generation 0 only.
            GC.Collect(0)

            'Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol))

            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(False))

            'Perform a collection of all generations up to and including 2.
            GC.Collect(2)

            'Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol))
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(False))

            'Perform a collection of all generations up to and including 2.
            GC.Collect(2)

            'Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol))
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(False))


            Console.Read()

        End Sub


        Sub MakeSomeGarbage()
            Dim vt As Version

            Dim i As Integer
            For i = 0 To maxGarbage - 1
                'Create objects and release them to fill up memory
                'with unused objects.
                vt = New Version
            Next i
        End Sub
    End Class
End Namespace
And here is the output when it is run:

The highest generation is 2
Generation: 0
Total Memory: 1052156
Generation: 1
Total Memory: 203796
Generation: 2
Total Memory: 203496
Generation: 0
Total Memory: 195420


Can anyone explain to me why the object that was in Generation 2 goes back into Generation 0 after the second GC.Collect(2) statement?

If I do GC.Collect(0) followed by GC.Collect(1) followed by GC.Collect(2) followed by another GC.Collect(2) then the object stays in Generation 2.

Any help much appreciated!

Thanks,

Rob
Rob Miles
No repro, it stays in gen2. Tested on .NET 3.5 SP1, x86 and x64. An object moving from gen2 to gen0 makes no sense.

Hans Passant.
nobugz
Hi,

Well this is interesting, I've just tried to reproduce at home and I can't get it to reproduce either. Here I get:

The highest generation is 2
Generation: 1
Total Memory: 624112
Generation: 1
Total Memory: 312816
Generation: 2
Total Memory: 312056
Generation: 2
Total Memory: 303864

My home setup is VS2008 SP1, .NET 3.5 SP1, Vista x64.
My work setup is VS2008 SP1, .NET 3.5 SP1, XP x86

Any ideas what could be causing my work setup to behave so strangely? It does concern me slightly!!

Rob
Rob Miles
Hello

This is a very interesting case. I can reproduce the problem when the app is started by a managed debugger. In other words, if I run the app directly, or if Irun the app from windbg (native debugger), or if I enable unmanged code debugging in VS, the problem cannot be reproduced. I'm performing researches on it. I will update you as soon as possible.

Regards,
Jialiang Ge
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Jialiang Ge [MSFT]
Hi Jialiang,

That is interesting, I'm glad I'm not the only one who can see this behaviour!

I have just tested it and I can confirm that if I run the compiled application from outside of VS then I do not see this behaviour. On my home PC I could not reproduce the issue from within Visual Studio however.

Rob
Rob Miles

You can use google to search for other answers

Custom Search

More Threads

• System.ComponentModel.Win32Exception
• Matching PIA to COM dll/exe?
• Marshalling vs Serialization
• Deriving from Generic Base Classes
• Collection Modified Exception During Multi-Thread Session
• .NET Runtime 2.0 Error Reporting # 5000 EventType clr20r3
• Memory access (or pointer size) problem when marshalling a win32 method on a 32-bit system
• System.BadImageFormatException after SP1
• Slow interop with Word in c# application
• Adding CodeGroups to the CAS policy