Re: A question about life cycle of an object.
- From: Barry Kelly <barry.j.kelly@xxxxxxxxx>
- Date: Thu, 24 Apr 2008 20:45:35 +0100
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: Xiuming
- Re: A question about life cycle of an object.
- From: Xiuming
- 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
- A question about life cycle of an object.
- Prev by Date: Re: Relationship between Application.Exit() and AppDomain
- Next by Date: Re: View windows forms control in a web page
- 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
|