Results 1 to 7 of 7

Thread: hashtable and memory

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    4

    hashtable and memory

    Hi all,

    I would like to know how to be sure that calling the clear method of my hashtable frees memory from the objects inside.
    For example I have:

    MyHashTable.Add(mykey, new MyObject(...))
    .....

    and finally

    MyHashTable.Clear(); //because I want to re-populate it again

    This code erases all MyObject from memory?
    If not, how can I do it?

    Thanks

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

    Re: hashtable and memory

    Why exactly do you want to do that? Is it for security reasons or for memory management?
    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
    New Member
    Join Date
    Feb 2007
    Posts
    4

    Re: hashtable and memory

    for memory management

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

    Re: hashtable and memory

    There's no point. The .NET Framework includes automatic memory management. All you need to do is make sure you call the Dispose method of all objects that support it and set any variables that may not lose scope for some time to Nothing. The Framework will take care of the rest in 99.999% of circumstances.
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    4

    Re: hashtable and memory

    Thanks.

    I'm tryng to make a loop like:

    foreach (object MyObject in MyHashTable.Values)
    {
    MyObject =null;
    }

    but obviously I get error because MyObject is only readable.

    Is it OK to set my objects of my hashtable explicitly to null (anyway there must be another way to do that because I get an error) or just calling MyHashTable.Clear() is the same?

    Thanks very much.

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

    Re: hashtable and memory

    If what you were trying to do would work it would do the same as Clear. A Hashtable contains items, each of which have a key and a value. Clear will remove all the items, so the Hashtable is empty. Conceptually, if what you are trying to do would work then it would simply remove the value for each key. The Hashtable would still contain just as many items and all the keys would be the same, but the value corresponding to each key would then be null.

    Having said that, even calling Clear doesn't change the amount of memory being utilised by more than a speck. All the objects that your Hashtable contained still exist, they just aren't in the Hashtable anymore. Like I said, the .NET Framework performs automatic memory management and if you try to mess with it there's every chance you'll make your app less efficient. Just follow the two simple rules I mentioned before and you're covered for the vast majority of situations. If you want your Hashtable emplty then clear it, but don't go worrying about the memory each object occupies.
    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    4

    Re: hashtable and memory

    Thanks, it's clear. No way to clear hashtable and free objects inside together. They'll be garbage-collected after calling hashtable.clear in an undefined time.

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