aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects are Garbage Collected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects are Garbage Collected" Watch "How many objects are Garbage Collected" New topic
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many objects are Garbage Collected