Hashtable, memory leak?

D

Derrick

I'm seeing what looks like a leak with Hashtable, but guessing it is
something I'm doing -

void func1()
{
Hashtable hash = getHash();

//Loop thru assorted data, check to see if data exists as key in
hashtable.

hash.Clear();
hash = null;
}

Hashtable getHash()
{
//populate and return new hashtable...
}

The memory consumption (found with GC.GetTotalMemory(false) ) keeps on
rising after each call to func1(), even though I've cleared the hash. Is
there something else I need to do? If I don't create and check the hash
keyset, memory consumption stays very low.

And, I am using the hash simply as a fast reference comparison,
if(hash.containskey("key I'm looking for")) kindof logic, all I really want
is the keyset but couldn't find a way to do that. The hash will sometimes
be loaded with *lots* of keys. I tried arraylist and the contains method,
but that was way to slow.

Thanks in advance!

Derrick
 
M

Marina

Memory would grow, as every time you are allocating and populating a new
hashtable object. If you do this repeatedly, then memory will keep growing.

When memory gets high enough, the GC should kick in, and clean up all those
hashtables that are no longer being referenced by anything.

Also, you don't need to set the hashtable variable to 'null'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top