Sign in to follow this  
Telamon

[.net] Garbage Collection of Static ArrayList?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
You don't have to worry about it being GC'ed. The array list should stay live as long as the assembly is loaded.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this