Sign in to follow this  
GekkoCube

Necessary to set resources to null before setting class to null?

Recommended Posts

GekkoCube    116
Suppose I have a class that has many allocations in it, say in the constructor, for say various objects and assets. Will setting the class instance to null free all memory? Or is it necessary to explicitly free (set to null) of members prior to the release of the class itself?

Share this post


Link to post
Share on other sites
Zahlman    1682
When the instance is garbage-collected, it will no longer be able to provide a reference to its resources, so setting the references to null is not necessary.

However, nullifying your reference to the class instance is no guarantee that the resources can be garbage collected; there may be other references to them in the system (like from other objects that you created). Similarly, it is not even a guarantee that your *class instance* can be garbage collected (although usually you can be pretty sure of exactly who has a reference to your own class instances, and nullify *all* of them).

Finally, note that for technical reasons, some resources need to be "cleaned up" rather than just letting the GC handle them. For example, files (streams) and some MMAPI objects like Players. By long-standing Java convention, the objects representing these resources will have a method called .close(), which is what you need to call before you try to GC the object.

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