none
What goes where in memory generation RRS feed

  • Question

  • Hi,

           Please consider sample below,

            private int i =0 ;
            private static int j =0 ;
            private string txt = "test";
            private int count { get; set; }
            private List<string> numbers = new List<string>();
            private static List<string> values = new List<string>();
            public void Print()
            {
                List<string> datas = new List<string>();
                var gen = new Genration();
                Console.WriteLine("static Generation I int: " + GC.GetGeneration(j));
                Console.WriteLine("Generation txt : " + GC.GetGeneration(txt));
                Console.WriteLine("Generation numbers : " + GC.GetGeneration(numbers));
                Console.WriteLine("static Generation values: " + GC.GetGeneration(values));
                Console.WriteLine("static Generation data: " + GC.GetGeneration(datas));
                Console.WriteLine("Generation gen: " + GC.GetGeneration(gen));
            }
        }

    The output is 

    static Generation I int: 0
    Generation txt : 2
    Generation numbers : 2
    static Generation values: 2
    static Generation data: 2
    Generation gen: 0



     How can the other objects move to generation 2, I this because of the delay, Then will Thread.Sleep(int.MaxValue) will also produce the same result.

    Regards,

    Shijith

    Thursday, July 4, 2013 6:38 AM

Answers

  • Your question (and example) is confusing.

    "The output is ..."

    If I take your code and put it in a console application I don't get the same result, all objects are in generation 0. If you end up with objects in gen 2 then it means that you have done a lot of additional allocations that you're not showing in your example.

    You should also note that getting the generation of a int (or any other value type) is pointless. The value will be boxed and its generation will always show up as 0 because it was just allocated.

    "How can the other objects move to generation 2, I this because of the delay, Then will Thread.Sleep(int.MaxValue) will also produce the same result."

    What delay? What Thread.Sleep? There's nothing like this in your code and anyway it doesn't have much to do with GC.

    • Marked as answer by Shijith M.C Saturday, July 6, 2013 2:07 PM
    Thursday, July 4, 2013 6:53 AM
    Moderator

All replies

  • Your question (and example) is confusing.

    "The output is ..."

    If I take your code and put it in a console application I don't get the same result, all objects are in generation 0. If you end up with objects in gen 2 then it means that you have done a lot of additional allocations that you're not showing in your example.

    You should also note that getting the generation of a int (or any other value type) is pointless. The value will be boxed and its generation will always show up as 0 because it was just allocated.

    "How can the other objects move to generation 2, I this because of the delay, Then will Thread.Sleep(int.MaxValue) will also produce the same result."

    What delay? What Thread.Sleep? There's nothing like this in your code and anyway it doesn't have much to do with GC.

    • Marked as answer by Shijith M.C Saturday, July 6, 2013 2:07 PM
    Thursday, July 4, 2013 6:53 AM
    Moderator
  • Hi,

           Thanks for your reply,This my calling code, I have not done any additional allocations, I'm usining i7 processor with 8 GB ram. Can other applications affect where the memory is allocated.

    Regards,

    Shijith 

                

    my code has only this more

    namespace ConsoleApplication1
    {
        class Program
        {
            public static void Main(string[] args)
            {
                try
                {
                    var gen = new Genration();
                    gen.Print();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.ReadKey();
            }
    
            public static void GetGarbageCollectionStatus()
            {
                bool found = false;
                bool Completed = false;
    
                while (true)
                {
                    found = GC.WaitForFullGCApproach(1) == GCNotificationStatus.Succeeded;
                    if(found) break;
                }
                if(found)
                {
                    while(true)
                    {
                        Completed = GC.WaitForFullGCComplete() == GCNotificationStatus.Succeeded;
                        if(found)break; 
                    }
                }
    
                Console.WriteLine("WaitForFullGCApproach : {0}", found);
                Console.WriteLine("WaitForFullGCComplete : {0}", Completed);
            }
        }
    }
    

    • Edited by Shijith M.C Thursday, July 4, 2013 8:16 AM for clarity
    Thursday, July 4, 2013 7:21 AM
  • "Can other applications affect where the memory is allocated."

    No.

    Even when I run this code in 64 bit mode I still don't get the same output. You must be doing something else that's not shown in the code.

    Thursday, July 4, 2013 7:48 AM
    Moderator