Results 1 to 3 of 3

Thread: [RESOLVED] [3.0/LINQ] List<> Clear() method

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Resolved [RESOLVED] [3.0/LINQ] List<> Clear() method

    I have a generic List<> that I am adding instances of a class to like this:

    Code:
    myList.Add(new myClass());
    At such time I wish to clear the list, I call the Clear() method and it removes the items in the List<>, but I am a bit unsure of how memory is affected at this point.

    MSDN says of the Clear() method:
    Count is set to 0, and references to other objects from elements of the collection are also released.
    I am taking this to mean that apart from clearing the list, the instances of the classes added to the list are destroyed, and will now be garbage collected.

    Am I right?

    (The class does not contain anything that needs Dispose() calling on it, it is all managed types and code.)


    Thanks to anyone that can cure my crisis of confidence!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [3.0/LINQ] List<> Clear() method

    Quote Originally Posted by Andy_P
    I am taking this to mean that apart from clearing the list, the instances of the classes added to the list are destroyed, and will now be garbage collected.

    Am I right?
    Not quite. Object destruction and garbage collection are not the same thing. In .NET, destruction means disposal, i.e. release of managed and unmanaged resources. Garbage collection means the system reclaiming the memory occupied by the object.

    If your objects have no dispose method then they don't need to be destroyed. Objects that do must be destroyed before they can be garbage collected. Well-written code will dispose an object that needs it as soon as that object is no longer needed. It will also make sure that references to objects that are no longer needed are removed to allow for garbage collection. Removing a reference means a variable either losing scope or being explicitly set to null.

    In the case of your List it's pretty simple. Consider a plain old every day variable:
    CSharp Code:
    1. string str = "Hello World";
    As long as the 'str' variable is in scope and refers to that string object the system cannot reclaim the memory it occupies. As soon as the variable loses scope or you assign null or another string to that variable, assuming there are no other variables that also refer to that same string object, the string object becomes eligible for garbage collection. Exactly when that happens is at the discretion of the system.

    Now, consider an array. An array is basically just a bunch of variables grouped together, so everything I said about the simple string variable previously is also true of each element of a string array, because each of them is basically a string variable.

    Now consider a List<string>. A List is just a wrapper for an array. Calling Clear on a List simply sets each element of the internal array to null. We already know what that means, don't we? Each object that was previously assigned to an element of that array will then become eligible for garbage collection assuming that there are no more references to that object stil in scope elsewhere.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [3.0/LINQ] List<> Clear() method

    Aha! Understanding that the Clear() method makes every item in the list 'null' makes things a lot clearer.




    Thanks for the help.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width