It's not a secret anymore!*
The moose likes Beginning Java and the fly likes LinkedList -- removeAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "LinkedList -- removeAll()" Watch "LinkedList -- removeAll()" New topic
Author

LinkedList -- removeAll()

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
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 ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

You start out with this:

After setting head=null and tail=null

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 ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Doh!
Jim is absolutely right. My bad. Got so carried away the ASCII diagramming that I forgot about that check :roll:
Junilu
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Here's a link you might find useful: http://perso.club-internet.fr/thedech/java/exp/ch05_04.htm
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LinkedList -- removeAll()