|
-
Feb 15th, 2007, 01:06 PM
#1
Thread Starter
New Member
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
-
Feb 15th, 2007, 06:34 PM
#2
Re: hashtable and memory
Why exactly do you want to do that? Is it for security reasons or for memory management?
-
Feb 19th, 2007, 06:29 AM
#3
Thread Starter
New Member
-
Feb 19th, 2007, 05:05 PM
#4
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.
-
Feb 20th, 2007, 04:17 AM
#5
Thread Starter
New Member
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.
-
Feb 20th, 2007, 04:30 AM
#6
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.
-
Feb 20th, 2007, 04:50 AM
#7
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|