Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects are Garbage Collected

 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is:

import java.util.*;
public class Collectionss {
public static void main(String as[]) {
LinkedList lhs = new LinkedList();
Collectionss c = new Collectionss();
Collectionss c1 = new Collectionss();
lhs.add(c1);
lhs.add(c);
lhs.add("ma");
lhs.add("ba");
lhs.add("co");
lhs.add("de");
//lhs=null;
Iterator it = lhs.iterator();
//it.remove("delhi");
while(it.hasNext()){
System.out.println("CITY IS"+it.next());
}
lhs=null;
}
}

How many objects are garbage collected in this code after the line
lhs=null;
Could anyone explain me.
Thanks.
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, only object "LinkedList" is garbage collected. Even after lhs=null, c and c1 are still pointing to Collectionss objects. In Java strings are also objects so you can say strings "ma", "ba", "co" and "de" are also garbage collected!

Hope it helps.
 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitin
so can we say five objects are garbage collected.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I guess 5 objects. lhs,"ma","ba","co","de"
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rengarajan, you are right !
 
Atul Chandran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the Iterator reference it.What will it refer to initially and after the while loop??
 
Vidyavathi saravanan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt.

The String Objects are created in ' String pool'. (ma,ba,co,de).
Whether they are eligible for GC.? Or they are neglected by GC?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic