Results 1 to 5 of 5
Thread: Q about Garbage Collector
- 02-05-2010, 03:42 AM #1
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Q about Garbage Collector
Just a quick one, I wrote up a practice program that creates a binary search tree. The nodes are defined like this:
Removing the nodes from the tree is handled by skipping, like this:Java Code:private class Node { int data; Node parent, lchild, rchild; public Node(int d, Node p, Node l, Node r) { data = d; parent = p; lchild = l; rchild = r; } }
Now, the node I skipped is no longer referenced by any active object, but it still references it's parent and child/children. Will it be removed by the garbage collector or not?Java Code:Node n; //the node I want to remove, assume it's initialized and has a non-null left child int data = n.data; n = n.parent; if(data < n.data) //since this is a binary search tree, lesser values will alway be contained in the left subtree n.lchild = n.lchild.lchild; else n.rchild = n.rchild.lchild;
- 02-05-2010, 05:32 AM #2
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
You are right.
When an object is no longer referenced it becomes eligable for garbage collection.
It does not matter if the "to be garbage collected" object references another object.
However, exactly when the garbage collection process cleans up those eligable objects is up to the JVM.
You can suggest that garbage collection runs by System.gc(), but this is not gauranteed.
- 02-05-2010, 05:43 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Thanks a lot!
This program is only studying for an exam, but the question has been bugging me, it wouldn't be a very good habit to write memory leaks.
- 02-05-2010, 05:50 AM #4
Member
- Join Date
- Nov 2007
- Location
- New Zealand
- Posts
- 36
- Rep Power
- 0
If your studying for the SCJP exam then "A programmer's guide to Java certification: a comprehensive primer By Khalid Azim Mughal, Rolf W. Rasmussen" is an excellent book. Very consise.
Best of luck
- 02-05-2010, 05:57 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Similar Threads
-
Garbage Collector and finalize()
By arefeh in forum New To JavaReplies: 5Last Post: 01-09-2010, 09:04 PM -
Garbage collector and its impacts
By RadhaJayalakshmi in forum Advanced JavaReplies: 1Last Post: 07-23-2008, 11:56 AM -
Interacting with the Java Garbage Collector
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 08:04 PM -
How to use the garbaje collector
By Eric in forum Advanced JavaReplies: 2Last Post: 06-29-2007, 01:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks