none
Memory Size of Collection RRS feed

  • Question

  • What to read the memory size in terms of KB, holding data in a collection.

    PurchasingSupplierMaterialCollection = new List<ETLPurchasingSupplierMaterialGridModel>(gridDataList.OrderBy(c => c.SupplierCode));

    I would like to find out size (KB) of PurchasingSupplierMaterialCollection.


    Ajay

    Thursday, August 2, 2018 10:36 AM

Answers

  • Evaluate the next estimation:

    long bytes;
     
    using( var ms = new MemoryStream() )
    {
       var bf = new BinaryFormatter();
       bf.Serialize( ms, new List<ETLPurchasingSupplierMaterialGridModel>( 0 ) );
       bytes = ms.Position;
       ms.Position = 0;
       bf.Serialize( ms, PurchasingSupplierMaterialCollection );
       bytes = ms.Position - bytes;
    }
     
    long KB = (long)( bytes / 1024.0 + 0.5 );

    Also add an attribute to your class:

    [Serializable]
    class ETLPurchasingSupplierMaterialGridModel { . . . }


    It is also possible to use a null stream.





    • Edited by Viorel_MVP Thursday, August 2, 2018 4:42 PM
    • Proposed as answer by Stanly Fan Friday, August 3, 2018 3:07 AM
    • Marked as answer by AjayKumar_MIND Tuesday, August 7, 2018 4:59 AM
    Thursday, August 2, 2018 4:38 PM

All replies

  • Evaluate the next estimation:

    long bytes;
     
    using( var ms = new MemoryStream() )
    {
       var bf = new BinaryFormatter();
       bf.Serialize( ms, new List<ETLPurchasingSupplierMaterialGridModel>( 0 ) );
       bytes = ms.Position;
       ms.Position = 0;
       bf.Serialize( ms, PurchasingSupplierMaterialCollection );
       bytes = ms.Position - bytes;
    }
     
    long KB = (long)( bytes / 1024.0 + 0.5 );

    Also add an attribute to your class:

    [Serializable]
    class ETLPurchasingSupplierMaterialGridModel { . . . }


    It is also possible to use a null stream.





    • Edited by Viorel_MVP Thursday, August 2, 2018 4:42 PM
    • Proposed as answer by Stanly Fan Friday, August 3, 2018 3:07 AM
    • Marked as answer by AjayKumar_MIND Tuesday, August 7, 2018 4:59 AM
    Thursday, August 2, 2018 4:38 PM
  • Hi AjayKumar_MIND,

    Here is another method which use System.GC.GetTotalMemory to calculate the memory size of collection.

    static void Main(string[] args)
    {          
                long StopBytes = 0;
              
                List<string> list;
                long StartBytes = System.GC.GetTotalMemory(true);
               
    
                list = new List<string>() { "aaa", "bbb", "ccc" };
                StopBytes = System.GC.GetTotalMemory(true);
                
    
                GC.KeepAlive(list); // This ensure a reference to object keeps object in memory
                Console.WriteLine("Size is " + ((long)(StopBytes - StartBytes)).ToString());
           
                Console.ReadKey();
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 3, 2018 3:31 AM
    Moderator