• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how many eligible for Garbage Collection

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

 
Guilherme Borges Lima
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup........Abimaran is correct.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!~
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can anyone explain me how 13 objects are created. I thought just 9 objects are created.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

Nice explanation.

Thanks.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bert Bates for your valuable information!
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ankit and everyone. The discussion really helped me .
 
Guilherme Borges Lima
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this topic is very interesting, It could be moved for "Java in General" forum.

 
reply
    Bookmark Topic Watch Topic
  • New Topic