This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I would like to know whether the way I am removing all the objects from the linkedlist is correct or not? (may be efficient or not) This is my code for Doubly LinkedList (not java.util.LinkedList ) Ok here the code is :
if I just set the head and tail to null, what happens to the remaining nodes... because each node is referencing to next node.. and the next node has reference to the prev node. So I think they won't be garbage collected, am I right? Do I have to scan through the LinkedList and set all the nodes to null? Thanks, Vanitha. [ March 05, 2002: Message edited by: Vanitha Sugumaran ]
The nodes still reference each other so they won't get GC'd. What you need is to break the links in one direction from head to tail (or vice versa). You really don't need to break the links both ways but you could if you wanted to.
When you get this, the former tail node (N4) will become eligible for GC. When it is GC'd a chain reaction will be set off with each previous node becoming eligible for GC in turn. HTH, Junilu [ March 05, 2002: Message edited by: Junilu Lacar ]
What you have is a set of cyclic references - they refer to each other, but nothing outside the cycle refers to them. In this type of situation, all the objects can indeed be garbage collected. The JVM checks explicitly to see if there is any way the objects are reachable from variables held by any (living) thread. If no threads havee access to any of the nodes, it doesn't matter if the nodes have access to each other - they aren't reachable. Any objects not reachable can be garbage collected. I believe that some (early) JVM implementations may not have been able to successfully GC cyclic references, but it certainly should be expected nowadays. To test this, create a class with a finalize() method which prints some sort of message. Put objects of this class in your LinkedList, and use removeAll(). Wait a minute, and see if you get messages indicating that the objects are being GC'ed. Let me us know if they're not. Incidentally, it might be a good idea to rename your LinkedList, to avoid conflict with the java.util.LinkedList. Unless this is just a learning assignment and you never plan to actually use this class... [ March 05, 2002: Message edited by: Jim Yingst ]
Thanks a lot for your replies Junilu and Jim. Junilu, your diagram clearly represented my doubt.Thanks for it and also for your link. I tried like this. TestElement class
But it doesn't print anything... Am I doing anything wrong in finalize() method? I understand we can't expect finalize() will be called as soon as we remove all the elements. But how can we make sure of it? By the way, this is only for my own practice, how ever I will change the class name. Thanks, Vanitha. [ March 05, 2002: Message edited by: Vanitha Sugumaran ]