Home All Groups Group Topic Archive Search About

garbage collection for hastable.

Author
13 Apr 2006 11:13 AM
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

Author
13 Apr 2006 1:31 PM
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.

Show quote
"Praveen" <praveen@newsgroup.nospam> wrote in message
news:%23wsWVtuXGHA.196@TK2MSFTNGP04.phx.gbl...
>
> 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
>
>
>
>
>
>
Author
13 Apr 2006 1:40 PM
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?



Show quote
"Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message
news:etgsG6vXGHA.3972@TK2MSFTNGP04.phx.gbl...
>
> 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.
>
> "Praveen" <praveen@newsgroup.nospam> wrote in message
> news:%23wsWVtuXGHA.196@TK2MSFTNGP04.phx.gbl...
>>
>> 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
>>
>>
>>
>>
>>
>>
>
>
>
Author
13 Apr 2006 5:47 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
13 Apr 2006 6:19 PM
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



Show quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:eW%23qiJyXGHA.4920@TK2MSFTNGP02.phx.gbl...
> >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
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
Author
13 Apr 2006 7:32 PM
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.

Show quote
"John Davis" <johndavi***@hotmail.com> wrote in message
news:efpuLbyXGHA.3660@TK2MSFTNGP04.phx.gbl...
>
> 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
>
>
>
> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
> news:eW%23qiJyXGHA.4920@TK2MSFTNGP02.phx.gbl...
> > >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
> >
> > --
> > Mattias Sjögren [C# MVP]  mattias @ mvps.org
> > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> > Please reply only to the newsgroup.
>
>
>

AddThis Social Bookmark Button