[.net] Garbage Collection of Static ArrayList?
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement