Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove instance of a class from memory 2

Status
Not open for further replies.

kodr

Programmer
Dec 4, 2003
368
0
0
This is probably a real basic question, but I'm not even sure what to search for to find an answer.

For instance:

Code:
SimpleClass MyClass = new SimpleClass();

Once I was done with MyClass, and I wanted to free up the memory space, how would I go about that?
 
If there are no references to the object, then the object will be automatically removed from memory the next time that garbage collection is run. If you'd like to run garbage collection sooner, you can use System.gc(), but this isn't generally advisable- usually it's best to let java decide when to release memory.

If you are holding a reference to an object, but you want the object to be garbage collected even if you have that reference, you can wrap that reference with a WeakReference object.

liam morley
 
You would set the reference to null and let the Garbage Collector eat it when it gets around to it.
Code:
myClass = null; //Variables begin with lower case letters by convention

Obviously you'd have to ensure that no other variable holds a reference to your instance, otherwise it will stay alive regardless.


Tim
 
Ah, thanks to you both. I'm still new to Java, so this Garbage Collector is new to me. I've got some reading to do.
 
You should generally reduce the instantion of classes to the smallest scope necessary.

Setting references to null is nearly allways useless voodoo.
And don't confuse memory used by the JVM with memory available for the system.
When starting the JVM, a certain amount of memory is allocated from the system by the jvm, and you can't increase or decrease it at runtime.

don't visit my homepage:
 
You should generally reduce the instantion of classes to the smallest scope necessary.

I wish I'd said that.[smile]

Setting references to null is nearly always useless voodoo.

Yeah, I have to admit that personally I don't tend to use the null method very often and do rely on scoping to deal with instances no longer needed.

Managing no-longer-needed instances is better done by good design and using Stefan's rule of keeping them within the smallest scope possible. Just be aware that even if you create an instance in the smallest scope possible, if you copy the reference to something outside that scope, the instance will remain live beyond that scope. It's as Liam stated in his reply, objects are elligible for GC when there are no references to them.



Tim
 
Thanks for the info. I was asking because I was trying to learn about setting up a Linked List (first time I've been exposed to that.)

I got everything to work correctly, but I was concerned that on a large project, how would I ensure that a 'node' in the linked list was actually removed from memory.

Would removing the reference in the previous nodes next element be sufficient for GC to clean it up, if I don't reference it anywhere else? I'm not really sure how I could test for this. In a smaller project just for testing, this is not a major concern, but what about in a large scale production environment? Obviously, in that environment you'd probably be using some type of DB backend (MySQL, Oracle, etc..) But what if for performance you used Linked Lists for quick manipulation of data before accessing the hard drive, or network connection.

Am I making sense?
 
Ahh. You will have to be careful how you make use of the instances in your linked list. Keep any variables used to process nodes in the tightest scope you can.

(By the way, Java has an implementation of the Linked List at java.util.LinkedList)

Tim
 
Would removing the reference in the previous nodes next element be sufficient for GC to clean it up, if I don't reference it anywhere else?
Yes.
I'm not really sure how I could test for this.
Create a List.
Add elements, until you get a memory-exhaustion.

Modify your program:
Now add elements, and remove them again.
See if you get a memory-exhaustion.


don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top