• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

GC Question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When is the String "item" eligible for garbage collection?
public class Basket{
public String method() {
String item = new String("Apple");
String pack[] = new String[1];
pack[0]=item; // 1
item=null; // 2
return item; // 3
} // 4
}
a. Just After Line 1
b. Just After Line 2
c. Just After Line 3
d. Just After Line 4
I thought that the answer would be line 3 but I don�t know when �pack� really falls out of scope: line 3 or 4?
Any Thoughts
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable pack won't fall out of scope until the method ends. Therefore, you have an active reference to the object in question until the method ends, which would be line 4.
Corey
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think item is ready for gc right after lin2 since it is set to null.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"yuelan"
Welcome to the JavaRanch! Please adjust your displayed name to meet the JavaRanch Naming Policy.
You can change it here.
Thanks, and again welcome to the JavaRanch!!
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Just because, it is referencing to null, does not mean it will be eligible for GC. "item" can have any value of type string, in this case it is null. Morever GC does not depend on the value your are referring, instead it depends on whether is active in the live threads ( i mean, whether it is referenced in live thread)
Praveen.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yuelan,

pack[0]=item; // 1


After this line is executed, the object referenced by item is also referenced by pack[0], therefore is still not eligible for gc.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read that String variables are not GC. If that is right?
In that case the only suitable answer is after line 4. So does that mean that Stinrgs are GC once they are out of scope but not when they are not refernced.
Please Clarify
Thanks
 
Robert Holmes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate all of your input!
Rob
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepali,
String literals are not normally garbage collected. But this point isn't going to be tested in the exam.
The g. collection of the other String objects may be.


So does that mean that Stinrgs are GC once they are out of scope but not when they are not refernced.


No it doesn't. The point is that after the method completes the local variables created within are certainly destroyed. If some of them are not referenced anymore and the g.c. is running they may be g.c.ed before the method completes.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
The point is that after the method completes the local variables created within are certainly destroyed.


Be careful here. They are not certainly destroyed. They may be eligible for garbage collection but when, if ever, they are garbage collected can not be known for sure.
As far as String literals are concerned, just remember that String literals are never garbage collected, nor are they ever eligible for garbage collection.
Corey
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok in the frame for a method, primitive types are destroyed and reference types are g.ced
String literals can be g.c.ed, with some effort, as this post shows
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heh, I got more questions than answers from that post (not to mention a splitting headache)
Is the class loader concept part of the exam?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not at all. String literals g. collection is not either. Forget about them regarding the exam.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
Ok in the frame for a method, primitive types are destroyed and reference types are g.ced
String literals can be g.c.ed, with some effort, as this post shows


Jose,
That post does not show garbage collection of String literals. Rather, normal String objects are created by concatenating String literals to something else.
There are a number of misconceptions in this post. I'll try to hit a few of them.


Originally posted by yuelan:

I think item is ready for gc right after lin2 since it is set to null.


That's not true because there is another reference to that object through the variable pack[0]. Only after all references to an object have been destroyed will the object be eligible for garbage collection.


Originally posted by praveen cheruvu:

Just because, it is referencing to null, does not mean it will be eligible for GC. "item" can have any value of type string, in this case it is null. Morever GC does not depend on the value your are referring, instead it depends on whether is active in the live threads ( i mean, whether it is referenced in live thread)


Huh? I don't even understand all of that. Remember, variable don't undergo garbage collection. Rather, the objects that are referenced by variables are garbage collected. If a variable references null, it refers to no object. This doesn't mean a whole lot. But, if no variables refer to an object, that object is then eligible for garbage collection. That's the key point to remember.


Originally posted by Deepali Pate:

I have read that String variables are not GC. If that is right?


First of all, remember that you don't garbage collect variables. You garbage collect objects. With that in mind, String objects are created on the heap and are subject to garbage collection rules just like any other object. However, it is important to note that String literals (note the distinction from a normal String object) always have a reference to them from the String constants table. As they always have a reference to them, they will never be garbage collected. A normal String object (created with new String("...")) can be garbage collected as no reference is created to it from the constant table.
I hope that helps,
Corey
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would be wrong to say that all literals and objects are GCed when the program ends?
Francisco
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Francisco A Guimaraes:
Would be wrong to say that all literals and objects are GCed when the program ends?
Francisco


The garbage collection thread is really a daemon thread. Therefore, when all user threads exit (your program), the garbage collection thread dies with the JVM. Therefore, some objects might never be garbage collected. It is then up to the underlying OS to reclaim that memory space.
Corey
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from Corey


That post does not show garbage collection of String literals. Rather, normal String objects are created by concatenating String literals to something else.


Please read my classes Test and Test2 at the END of that post.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic