File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Same old garbage collection problem!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Same old garbage collection problem!!!" Watch "Same old garbage collection problem!!!" New topic
Author

Same old garbage collection problem!!!

Geeta Gune
Greenhorn

Joined: Mar 19, 2002
Posts: 17

In the above code how many objects will be created? And how many shall be ready for garbage collection?
Also,i want to know 2 things:
1) Will it make a difference if i initialize the String s variable in the loop?
2) Will it make a difference if instead of the string i define an int?
I am very confused with these basic stuff.
Pls help.
Thanx
Geeta
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Basically, String literals are not garbage collected.
The String referred to by s is created once before the loop. No other object is created after that.
Please refer to the following discussions which handle the same topic:
http://www.coderanch.com/t/239961/java-programmer-SCJP/certification/GC-String-object
http://www.coderanch.com/t/236271/java-programmer-SCJP/certification/String-Pool-Redux
http://www.coderanch.com/t/366619/java/java/sting-pool
[ November 29, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
The code you wrote will have one String object created. IF this code was part of a method, and the method does NOT reference this object to a member variable, then supposedly there is one object elligibel for GC. BUT since this object is a string literal, the GC system doesn't garbage collected for optimization purposes.
If you make the initialization in the loop then there should be more object created with every loop but AGAIN since this is a String literal object, there will NOT be any new objects created because of the compiler optimization. UNLESS you use the new operator, there will not be new objects created during the loop itirations.
You can NOT garbage collect a primitive, GC system only works on objects.
but if you meant to creat an Object other than a String, like Integer object then yes it makes a difference since this time to initialize will use the new operator and will creat an object with each itiration - that's if the initialization is inside the loop -.
String s = "String"; //creats one String object
String s = new String("String"); // creats two String objects
HTH


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Same old garbage collection problem!!!
 
Similar Threads
String Garbage Collection Doubt
How many objects are eligible for garbage collection
doubt in garbage collection
MIndQ #36
garbage collector