[.net] Garbage Collection of Static ArrayList?

Started by
4 comments, last by Telamon 18 years, 7 months ago
I have a class, GameData with a private static ArrayList inside of it. When I start my game, I instantiate a GameData object and fill the arraylist with bitmaps. In other parts of my game I then use a static accessor function in GameData to spit out the bitmap for a requested image. This works fine. My question is this - do I have to worry about the static ArrayList being GCed while I am still using it? Or does the compiler allocate space for static objects are compile time (like C/C++)? Presumably there's a table of pointers to static objects somewhere in the code to prevent premature GC. However, if I'm wrong about this, this could become a hiesenbug later, so I thought I would check.

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

Advertisement
You don't have to worry about it being GC'ed. The array list should stay live as long as the assembly is loaded.
A brief description of how a tracing garbage collector works can be found here.

Static object references form part of the root set of references considered by the garbage collector (along with local variables on live stack frames), and as such they will never be collected.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
I dont think statics every get thrown away in GC
As SiCrane already mentioned, they will be collected when the assembly is unloaded. Which will only happen when the Application Domain is unloaded, which typically (unless you're using interdomain communication) only happens when the process is unloaded.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Ok that make sense. Thanks guys.

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

This topic is closed to new replies.

Advertisement