wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many eligible for Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how many eligible for Garbage Collection" Watch "how many eligible for Garbage Collection" New topic
Author

how many eligible for Garbage Collection

saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Consider the following code (written by me):



At line 3 how many objects are eligible for garbage collection?
The answer is 1 or no objects.

well, s1 object is created at line 1 , and then at line 2 another object is created with upper case letters "APPLE" , so two objects are created but since we do not have any reference to this second object with upper case letters , it is eligible for GC. Anyone please confirm . Will we consider this object at line 2 as a newly created object or not ?? is it eligible for GC ?


SCJP 6.0 (86%) SCWCD 5 (94%)
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I think the answer should be 1 object as one of the string literals that are formed go to string pool and the other is lost due to no reference.

so yup, i think you are right.
but lets see what others have to say.


SCJP 1.6 96%
Guilherme Borges Lima
Greenhorn

Joined: Jun 04, 2008
Posts: 11
Yes, a new Object is created at line 2
There are only String literals in the String pool
s1.toUpperCase returns a String that is not in the String pool, because it is not a literal.
Yes, the Object that contains "APPLE" is eligible to GC.
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Thanks guys. One more thing to clarify:

when we call the toString method on Stringbuilder or string buffer (or any Wrapper class like Integer), then also an object is created without a reference. true or not ??

if that is true then consider the following codes written by me for garbage collection:

Here also the string object created at line 2 is eligible for GC.

So , what about the following code ??



here , sine system.out.println invokes toString method , a string abject is created at line 2 , which is eligible for garbage collection. true ??
Guilherme Borges Lima
Greenhorn

Joined: Jun 04, 2008
Posts: 11
Yes, in both cases toString() returns a String that is not referred by any variable
Both codes create a String that is eligible for GC.
But if you do the following:

then the String is not eligible for GC, because it is still referred to by s.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Guilherme Borges Lima wrote:
...... s1.toUpperCase returns a String that is not in the String pool, because it is not a literal.
Yes, the Object that contains "APPLE" is eligible to GC.


Yea, That APPLE is eligible GC. It also is in the String Literal Pool(obviously it is a String literal??), since it doesn't have any reference in the stack, that's why it is eligible for GC. Correct?

What about Guilherme Borges Lima's? Please confirm?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Guilherme Borges Lima
Greenhorn

Joined: Jun 04, 2008
Posts: 11
Yea, That APPLE is eligible GC. It also is in the String Literal Pool(obviously it is a String literal??), since it doesn't have any reference in the stack, that's why it is eligible for GC. Correct?

No, it's not a literal.

It Returns false since the Object created using s1.toString() is only on the heap, not in the String Literal Pool
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Yea, It's correct, that both a and b aren't equal(==). But I'm pretty sure that every Strings you created, first JVM checks whether it is in the String Literal Pool, If it is there, then that is another story. If it isn't, then JVM first creates a String Literal in the String Literal Pool. But the variable may or mayn't refer it. Another point that String Literal is also an Object(which is in the String Literal Pool).

For Example, can you tell me how many objects are created in the below program?



I think there are 13 Objects.. Please somebody confirm it? Thanks in Advanced!
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
yup........Abimaran is correct.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hi, saima kanwal.. If I help you...


In this code, there are 3 Objects. apple(s1), APPLE(s2) are in String Literal Pool. When you invoke toString() method, there will a Object created in the heap, not in the String Literal Pool(Since APPLE is already in the String Literal Pool, JVM don't need to create a String Literal Pool Object.). That's why
s2 == s1.toUpperCase() returns false

.
If s2 is not there, Then JVM creates a String Literal Pool Object (APPLE) and create a object in the heap? I don't sure beyond the story..... Somebody help!~
KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46


Can anyone explain me how 13 objects are created. I thought just 9 objects are created.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

KrishnaPrasad raghavan wrote:

Can anyone explain me how 13 objects are created. I thought just 9 objects are created.


I've to draw the diagram. Wait Please, Otherwise anybody help him without diagrams Please....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

The discussion is interesting, but there will be no question in SCJP which will ask you how many String objects are eligible for GC. The String constant pool makes things confusing so GC questions are never asked about Strings...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

KrishnaPrasad raghavan wrote:Can anyone explain me how 13 objects are created. I thought just 9 objects are created.

The 13 objects that are created are as follows (wherever I've written (CP), it means that the object was created in the String constant pool)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks a lot Ankit Garg! I think, you won't help me. But you did it. Thanks again!

Ankit, It's ok, that they mayn't ask about it(String and GC). But can you give a good explanation for us about that question I asked lastly.???
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

KrishnaPrasad raghavan wrote:

Can anyone explain me how 13 objects are created. I thought just 9 objects are created.


Ankit Garg worte:

The 13 objects that are created are as follows (wherever I've written (CP), it means that the object was created in the String constant pool) ......///////


Hope KrishnaPrasad raghavan understood Ankit's. I needn't to draw any diagrams. Thanks a lot Ankit.....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Abimaran Kugathasan wrote:I think, you won't help me. But you did it.

What made you think that I'll not help you??
But can you give a good explanation for us about that question I asked lastly.???

There are too many questions in this thread and I can't figure out which question you are talking about. That's why we encourage people to Use One Thread Per Question. Can you please ask the question again, or point me to the exact post you are talking about, otherwise anyone else who can figure out what question you are trying to ask might help you out...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I made a mistake previously.... OK. Let's get out from that....



In this code, there are 3 Objects. apple(s1), APPLE(s2) are in String Literal Pool. When you invoke toString() method, there will a Object created in the heap, not in the String Literal Pool(Since APPLE is already in the String Literal Pool, JVM don't need to create a String Literal Pool Object.). That's why
s2 == s1.toUpperCase() returns false

.
If s2 is not there, Then JVM creates a String Literal Pool Object (APPLE) and create a object in the heap? I don't sure beyond the story..... Somebody help!~ If needed, I can start n new Thread on this.....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Okay the question is not very much clear to me. But there are a few things
1. The toString method of a String object never creates a new String object, neither in the heap nor in the constant pool, it just returns the String object itself i.e. this.
2. any manipulations that you do on a String object, like toUpperCase or substring, doesn't create an object in the String constant pool. The String constant pool stores only String literals in our programs.

So when you called s1.toUpperCase(), a new String object is created in the Heap which has the value "APPLE", but this is a different object than the "APPLE" in the String constant pool which is referenced by s2, that's why the == returns false...
KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46
Hi Ankit,

Nice explanation.

Thanks.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8803
    
    5
I want to reiterate what Ankit said...

Since this is the SCJP forum...

It's an interesting topic, but...

THERE ARE NO GC QUESTIONS USING STRINGS ON THE REAL EXAM !!!

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Bert Bates for your valuable information!
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Thanks ankit and everyone. The discussion really helped me .
Guilherme Borges Lima
Greenhorn

Joined: Jun 04, 2008
Posts: 11
I think this topic is very interesting, It could be moved for "Java in General" forum.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how many eligible for Garbage Collection
 
Similar Threads
GC Doubt
GC doubts
HI please Help with. GC Kathy and Bert Book
Garbage for objects and strings ...
gc