garbage collection for hastable.

P

Praveen

Hello all,

I am writing a class to cache xml and dataset objects that would be required
frequently by my application. I also have a time to check the last access of
the obect... If the object is not used for some time I would like to remove
it from the cashe to free memmory. I am using hastable to store these
objects.

What I observe is that even if I remove the object from the hastable the
memmory is not freed even if I wait a long time (more than 10 mins) to for
the Garbage collection cycle to happen.

To make sure there was an issue with the garbage collection I wrote a test
application.

I declared a class variable for Hashtable
in one function I am filling the hastable like below

for(int i = 0 ; i < 10000; i++)
{
HTT.Add(Guid.NewGuid().ToString(),new string('d',40000));
}

In another function I am calling HTT.Clear() to delete the items

during this time I am observing the memmory useage in the taskmanager. The
memmory useage rises as expected in the first function call but does not
come down after the second function is called.

I waited enough for the GC cycle more thant 10 mins... after the HTT.Clear
function call if I call CG.Colloect(); then memmory was coming down. I did
not want to do that in my app.

please suggest.

Thanks in advance,
Praveen
 
M

Michael D. Ober

Don't worry about the Garbage Collector. It will run when it needs to. If
you are really trying to reduce your application memory footprint, you need
to call the working set size APIs directly.

Mike Ober.
 
J

John Davis

Bear in mind that the Garbage Collector doesn't run after a certain amount
of time has passed but when a request for memory for a new object can't be
satisfied. But like Mike says you shouldn't really need to worry about what
the Garbage Collector is doing unless you are a writing a very time
sensitive program.



John



PS have you considered storing weekReferences to the objects in the
HashTable?
 
M

Mattias Sjögren

Bear in mind that the Garbage Collector doesn't run after a certain amount
of time has passed but when a request for memory for a new object can't be
satisfied.

Yes it does, you don't have to run out of memory for GC to kick in.


Mattias
 
J

John Davis

Hi Mattias



It is my understanding that when you allocate a new object it is allocated
on the managed heap for the necessary size, and the NextPtr is moved along
to the end of the memory just allocated. Then that this continues until
watermark levels are reached. If one of these levels is reached then the GC
looks to reclaim the memory from Generation 0, if it can't find any here it
looks in Generation 1 and if it still can't find it in Generation 2. So
with my current understanding it takes an allocation of a new object for
this to kick in. Please let me know at which point I have misunderstood.



Cheers



John
 
M

Michael D. Ober

John,

The GC will also kick in when the application goes idle and there has been
any memory allocations or finalize requests since the last GC cycle.

Mike Ober.

John Davis said:
Hi Mattias



It is my understanding that when you allocate a new object it is allocated
on the managed heap for the necessary size, and the NextPtr is moved along
to the end of the memory just allocated. Then that this continues until
watermark levels are reached. If one of these levels is reached then the GC
looks to reclaim the memory from Generation 0, if it can't find any here it
looks in Generation 1 and if it still can't find it in Generation 2. So
with my current understanding it takes an allocation of a new object for
this to kick in. Please let me know at which point I have misunderstood.



Cheers



John
 

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