| Author |
How many objects are Garbage Collected
|
rengarajan vaikuntam
Ranch Hand
Joined: Oct 04, 2004
Posts: 37
|
|
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
Joined: Sep 09, 2004
Posts: 132
|
|
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.
|
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
|
 |
rengarajan vaikuntam
Ranch Hand
Joined: Oct 04, 2004
Posts: 37
|
|
Nitin so can we say five objects are garbage collected.
|
 |
Veena Pointi
Ranch Hand
Joined: Jun 20, 2002
Posts: 442
|
|
|
Yeah I guess 5 objects. lhs,"ma","ba","co","de"
|
SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
|
 |
Nitin Bhagwat
Ranch Hand
Joined: Sep 09, 2004
Posts: 132
|
|
|
Rengarajan, you are right !
|
 |
Atul Chandran
Greenhorn
Joined: Oct 24, 2004
Posts: 22
|
|
|
What about the Iterator reference it.What will it refer to initially and after the while loop??
|
 |
Vidyavathi saravanan
Ranch Hand
Joined: Sep 24, 2004
Posts: 34
|
|
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?
|
 |
 |
|
|
subject: How many objects are Garbage Collected
|
|
|