Using the “clear” method vs. New Object

前端 未结 5 1901
有刺的猬
有刺的猬 2020-12-05 17:13

In the .NET framework, many of the System.Collection classes have Clear methods on them. Is there a clear advantage on using this versus replacing the reference

相关标签:
5条回答
  • 2020-12-05 17:44

    New object causes memory problems if you are working on big datas and manipulating IEnumerable item frequently. Look at my example:

    class Lol
    {
        int i;
        string s;
        public Lol(int ti, string ts)
        {
            i = ti;
            s = ts;
        }
    }
    
    class Program
    {
        static List<Lol> lol = new List<Lol>();
    
        static void Main(string[] args)
        {
            for (int i = 0; i < 10000000; ++i)
                lol.Add(new Lol(i, i.ToString()));
    
            Stopwatch sw = new Stopwatch();
            sw.Start();
            ListCleaner();
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
    
            for (int i = 0; i < 10000000; ++i)
                lol.Add(new Lol(i, i.ToString()));
    
            Console.WriteLine("lol");
    
            ListCleaner();
    
            for (int i = 0; i < 10000000; ++i)
                lol.Add(new Lol(i, i.ToString()));
    
            Console.WriteLine("lol");
    
            ListCleaner();
    
            for (int i = 0; i < 10000000; ++i)
                lol.Add(new Lol(i, i.ToString()));
    
            Console.WriteLine("lol");
    
            ListCleaner();
    
            for (int i = 0; i < 10000000; ++i)
                lol.Add(new Lol(i, i.ToString()));
    
            Console.WriteLine("lol");
    
            ListCleaner();
    
            for (int i = 0; i < 10000000; ++i)
                lol.Add(new Lol(i, i.ToString()));
    
            Console.WriteLine("lol");
    
            ListCleaner();
    
            for (int i = 0; i < 10000000; ++i)
                lol.Add(new Lol(i, i.ToString()));
    
            Console.WriteLine("lol");
    
            ListCleaner();
    
            for (int i = 0; i < 10000000; ++i)
                lol.Add(new Lol(i, i.ToString()));
    
            Console.WriteLine("lol");
    
            ListCleaner();
    
            for (int i = 0; i < 10000000; ++i)
                lol.Add(new Lol(i, i.ToString()));
    
            Console.WriteLine("lol");
    
            ListCleaner();
        }
    
        static void ListCleaner()
        {
            //lol = new List<Lol>();
            lol.Clear();
        }
    }
    

    If you use this, you will not encounter any problem on run-time. But if you comment out Clear line in ListCleaner and uncomment New object line, you will have a memory problem.

    By the way "Console.WriteLine(sw.ElapsedMilliseconds);" (You can use ElapsedTicks because its value is dramatically difference than other one.) line tells you, Clear takes more time but saves you from memory problem.

    I noticed all these happen on Debugging code (even for release mode). There is no problem in running .exe file.

    0 讨论(0)
  • 2020-12-05 17:52

    That would depends maybe on how large your collection object is. I think when you say new it will create the original size of the object, clear would just clear the content but the size would still be the same.

    0 讨论(0)
  • 2020-12-05 18:02

    You'd want to use Clear if you have other references to the same object, and you want to keep them all pointing to the same object.

    For example maybe you have a worker queue where you store tasks to do. And in one or more threads you take work items out of this queue (of course you use locking to make sure you access the queue with at most one thread at a time). If at some point you want to empty the queue, then you can use Clear and all threads will still point to the same object.

    As seen here when you use Clear all items will be removed, and the Count will be 0, but the Capacity will remain unchanged. Usually the Capacity being unchanged is a good thing (for efficiency), but there could be some extreme case that you had a ton of items and you want that memory to be eventually freed.

    The MSDN link above also mentions that Clear is an O(n) operation. Whereas simply replacing the reference will be an O(1) operation and then eventually it will be garbage collected but possibly not right away. But replacing the reference also means that the memory that makes up the capacity will need to be re-allocated.

    0 讨论(0)
  • 2020-12-05 18:06

    Brian is correct but to be more specific, the Clear method removes all items from the current instance of the collection. Instantiating a new collection and assigning its reference to your variable will give you an entirely new instance altogether and may cause some unintended consequences depending on whether or not other people are holding a reference to the old instance. If another thread has a reference to that collection they will still hold a reference to the old collection even though you have created a new instance.

    0 讨论(0)
  • 2020-12-05 18:08

    replacing the reference will not release the collection right away it will need to wait for garbage collector to dispose the object,

    if you want to reutilize the same object collection then use .Clear() if not you will have the two objects in memory for some time.

    0 讨论(0)
提交回复
热议问题