• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

garbage collection

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

In the following program1: Pls explain me which object is available for garbage collection 'n' how

String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2

Ans: object available for garbage collection are 1.


In the following program2: Pls explain me which object is available for garbage collection 'n' how

public class GBS
{
public static void main(String args[])
{
String one = "String1";
String two = new String("String2");

one = two = null;
}
}

Ans: object available for garbage collection are 1.

Note: I read SCJP TIP online on Strings by corey Mc Glone.

String Literals are not eligible for garbage collec
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there,
In program1. The Object orignally referenced by string1 is available for garbage collegetion during the command statemend "string1 = null". Because this statement makes the reference "string1" refer to null. Therefor "cutting of" its connection to the Object String "Test". And since the String Object "Test" has no reference connected to it, it is eligible for garbage collection.

W/ regards to program2. I think it should be 2 objects. But I haven't read Corey's article about this.
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!Doyle Matt

Thanks for the response. but i 've doubt...

Here's the link for corey's journal on strings

http://www.javaranch.com/journal/200409/Journal200409.jsp#a1


He says, String literals always have a reference to them from the String Literal Pool. That means they always have a reference to them and so , therefore ,not eligible for garbage collection.

so in the program 2 only 1 object is eligible for garbage collection i.e 'String two' which is created at run time.

because String literals always have a reference to them from the String
Literal Pool. Though String one is set to null , the Stringobject "something" has reference in String Literal pool

According to this in the program 1 though String String 1 = null; The String object 'Test' has reference in String Literal pooL.

So In program 1 i feel there is no object eligible for garbage collection.

is it right or wrong !!!


 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now look at the decompiled class file:



Lines 0: and 8: in main are loading the same string constant, even after a garbage collection may have taken place.
[ September 30, 2004: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way jayasree/maggie your displayed name still does not conform to JavaRanch's naming policy.

Please use two names: <first name> <family name>.

Thankyou
-Barry
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Barry Gaunt

Thanks for your reponse.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And thank you jaya for changing your name
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

plzzz respond...

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, let me try to explain how this works. Keep in mind, though, that garbage collection of String objects is not on the exam. Therefore, this discussion is informative only and won't be required for the exam.

Here's the first program mentioned:



So, when this code finishes execution, how many objects are available for garbage collection? The answer is 0. No objects are eligible for garbage collection. The reason for this is that every String literal has a reference stored in the String Literal Pool. As that reference is never cleared, each object always has an active reference to it and is not eligible for garbage collection. If you don't understand the String Literal Pool, I'd suggest reading (or re-reading) my article, Strings, Literally.

Now, let's take a look at the second program:



In this situation, there is an object available for garbage collection when the code complete, but there are 3 String objects, is all. The literals, "String1" and "String2", are referenced from the String Literal Pool - these objects will never be available for garbage collection.

However, by using the keyword "new" in the second line, you create a third String object which is equivalent to a String already created and referenced from the String Literal Pool. Note that, while these two String objects are equivalent (contain the same value), they are not the same object. They are two distinct String objects.

As this new object has no reference to it from the String Literal Pool, when the last line executes, setting the reference variable "two" to null, there is no longer a reference to that String and it is eligible for garbage collection.

I hope that helps,
Corey
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I now see that part of my post up there got lost when it was edited.

I said that I agreed with you that the first example did not make any strings available for garbage collection. The decompiled code example I gave shows that is the case.
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Thanks to Corey McGlone, your journal on Strings helped me a lot to understand Strings concept.


Thanks to both of you once again...Barry Gaunt 'n' Corey McGlone.

Now i'm confident with Strings Gc.



[ September 30, 2004: Message edited by: jaya merugu ]
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Folks

i'm stuck with this...would you explain this program.

How the objects r available to gc in this program.

class ADemo {
private String name;
private ADemo otherA;
public ADemo(String name) {this.name = name; System.out.println("received name"+name);}

public void other(ADemo otherA) {this.otherA = otherA; System.out.println("other()"+otherA);}

public ADemo other() { return otherA;}
public String toString() {return name;}
protected void finalize() {System.out.println(name);}
}

public class BDemo{

public static void m1() {
ADemo a1 = new ADemo("A1"), a2 = new ADemo("A2"), a3 = new ADemo("A3"), a0 = a3;
a1.other(a2); a2.other(a3); a3.other(a1); // a0.other(a1);
for(int i = 0; i<4; i++){System.out.println(a0 = a0.other());
}
}
public static void main(String[] args) {m1(); System.gc();
}
}

OutPut: A1A2A3A1A1A2A3


Thank You
Jaya

----

 
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
Jaya,

There's a whole lot of garbage collection going on in that program. Take a read through this and try to answer it on your own. If you're still confused or want someone to verify your answer, let us know what you came up with.
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi! corey,

i read your journal..that was pretty good. i understood.

but in the above program i'm not getting where the references r set to null.

i'm confused. could u pls explain me what exactly is happening in th
'other()' method.

pls make it clear step by step.

Thank you.
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pls respond..

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variables do not have to be set to null. You have to find where objects are no longer referenced. This can happen when a reference is changed to refer to another object, or when a reference variable goes out of scope.

Try a few simpler programs before trying to analysing this one. Most people will not have the time to spare to do this for you.
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Barry,

okay I'll try out simple programs first.

Thanks for your advice.
 
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
Jaya,

Please be patient. What you have asked for is neither simple to understand or quick to complete. Also, heed Barry's advice - if this is too difficult, start with something simpler and build up to something more difficult. Biting off more than you can chew is a sure way to get discouraged.

I'll try to get you started and I'll leave it up to you to figure out the rest. I'm going to use the same strategy with this application as I demonstrate in my blog - drawing pictures.

We'll start by looking at what objects we have after executing the first line of the m1 method:



Note that, each time we invoke the constructor for an object of type ADemo, we execute this line:



This causes a new String object to be created (based on the concatenation) which is immediately eligible for garbage collection because there is no active reference to it once the println method completes. So, by creating 3 objects, we have created 3 Strings that are eligible for garbage collection.

Now that we have this initial picture, let's execute the next line of code and see what happens.



See if you can take it from there. Now that you can see how the references are set up, I hope you can follow on to your conclusion.

In this program, there is very little that is available for garbage collection - only the 3 String objects created during the construction of the 3 objects. Nothing else is available for garbage collection, ever.

I hope that helps,
Corey
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Corey

Thanks a lot. U 've done a g8t job for me.

I'll look into it...

Thank you once again.
 
reply
    Bookmark Topic Watch Topic
  • New Topic