• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Creation & destruction

 
Shankar Narayana
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to do some memory management in my application so I have added a cleanUp method in all of my classes. The cleanUp method sets all the variables as well as other object instances used in that class to null.

The cleanUp method is called for each class after its use is over and the class reference itself is set to null.

In this way I hope the garbage collector would clear off all the unwanted resources from the memory.

My question here is do I need the cleanUp method or if I just set the class reference variable to null after its use would remove all the variables and instances from it?

Kindly let me know if you people are not clear of what i am trying to ask.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't definitively say, but I would guess assigning all of your object references to null doesn't buy you that much. When the variable goes out of scope it is eligible for the garbage collector anyway, and from what i have heard that algorithm is pretty efficient.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cleanUp method sets all the variables as well as other object instances used in that class to null.

Are you talking about class (static) variables here, or instance variables?
If an instance of a class goes out of scope and becomes GC collectable, all of the instance's references go out of scope, just as if you had used a cleanUp method to set them to null, but faster.
Bill
 
Shankar Narayana
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am talking about the instance variables, I am providing a sample code to get a clear understanding of what i am doing.



So do you think the method is really useful for the application. I am not clear at one point is that in class B cleanUp() method if I donot call the
cleanUp() of class A and just assign the reference to null, will the GC remove the String s1 from class A and free the memory ?


Thanks
Shankar
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The aa.cleanUp is totally unneeded. When there are no more references to the object "aa" there will also be no references to objects that can only be reached through aa.
You need to go read about Java GC - an object will be gc-ed if there is no way that a live Thread can "reach" it.
Bill
 
Shankar Narayana
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks William,

I will definetely go through and read about java GC to get a better understanding.


shankar
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!