Re: A question about life cycle of an object.
- From: Xiuming <blkwht2008@xxxxxxxxx>
- Date: Sat, 26 Apr 2008 00:13:39 -0700 (PDT)
BTW, I missed some information in my post.
When I tested with a hashtable object, I called the Clear() method of
the hashtable instance at the end of Test1() method.
After the Clear() call, references to those string object should be
counted down to 0 I think, and all those string objects should stay in
gen0 and should be collected for a while. But when I check the memory
used by the process after running for 1 hour, it still remains the
same.
On Apr 25, 3:45 am, Barry Kelly <barry.j.ke...@xxxxxxxxx> wrote:
Xiuming wrote:
Before starting this discussion, I have already searched for
informations about how GC works, I create a instance of StringBuilder
class is just for an example.
In fact, I create a instance of Hashtable class and add 10000000
string objects, the same thing happens.
Why?
A hashtable with live references to 10,000,000 strings is just as much a
large object graph as a string builder. The situation is no different.
The GC can't collect any of it while the hashtable is rooted. Since lots
and lots of string allocations were needed to add the objects to the
table, a few GCs probably happened in the meantime. But because nothing
could get collected (because everything was kept alive in the hash
table), the memory for the strings and the hash table would have moved
from gen0 to gen2 (I presume you know what these are if you've read
about CLR GC).
Thus, once the hash table is no longer rooted (e.g. nulled out, or the
variable goes out of scope), the GC won't collect it immediately. It'll
have to wait until a gen2 collection for it to get freed up. That won't
happen until both gen0 and gen1 collections occur, which may require
many intermediate allocations, depending on the sizes of gen0 and gen1
(which are based on historical collection rates).
-- Barry
--http://barrkel.blogspot.com/
.
- Follow-Ups:
- Re: A question about life cycle of an object.
- From: Barry Kelly
- Re: A question about life cycle of an object.
- References:
- A question about life cycle of an object.
- From: Xiuming
- Re: A question about life cycle of an object.
- From: Barry Kelly
- Re: A question about life cycle of an object.
- From: Xiuming
- Re: A question about life cycle of an object.
- From: Barry Kelly
- A question about life cycle of an object.
- Prev by Date: Re: A question about life cycle of an object.
- Next by Date: Re: A question about life cycle of an object.
- Previous by thread: Re: A question about life cycle of an object.
- Next by thread: Re: A question about life cycle of an object.
- Index(es):
Relevant Pages
|