Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Kathy, Dan, Shishio, Valentine, get on the subject: Final Word on GC

 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please when you reply to this post, quote the questions, choices, and write your answer.
If you can quote anything from the JLS to prove your answer that would be great.
Thanks in advance...

1- Case of a string literal referenced by a member variable
"Hello World" created at (marker 1) is ilegible for garbage collection after line:
(marker A - marker C - Never)
2- Case of a string object created by the new operator and referenced by a member variable
"Hi World" created at (marker 2) is ilegible for garbage collection after line:
(marker A - marker C - Never)
3- Case of a string literal referenced by a local variable
"inside World" created at (marker 4) is ilegible for garbage collection after line:
(marker A - marker C - Never)
4- Case of a string object created by the new operator and referenced by a local variable
"another World" created at (marker 5) is ilegible for garbage collection after line:
(marker A - marker C - Never)
5- Case of any other object other than string referenced by a member variable
Integer(3) created at (marker 3) is ilegible for garbage collection after line:
(marker B - marker C - Never)
6- Case of any other object other than string referenced by a local variable
Integer(4) created at (marker 6) is ilegible for garbage collection after line:
(marker B - marker C - Never)
Another question would be, will static or final modifiers on the member variables OR the method affect the answers??
Thanks in advance
[ October 29, 2002: Message edited by: Alfred Kemety ]
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1- Case of a string literal referenced by a member variable
"Hello World" created at (marker 1) is ilegible for garbage collection after line:
(marker A - marker C - Never)
2- Case of a string object created by the new operator and referenced by a member variable
"Hi World" created at (marker 2) is ilegible for garbage collection after line:
(marker A - marker C - Never)
3- Case of a string literal referenced by a local variable
"inside World" created at (marker 4) is ilegible for garbage collection after line:
(marker A - marker C - Never)
4- Case of a string object created by the new operator and referenced by a local variable
"another World" created at (marker 5) is ilegible for garbage collection after line:
(marker A - marker C - Never)
5- Case of any other object other than string referenced by a member variable
Integer(3) created at (marker 3) is ilegible for garbage collection after line:
(marker B - marker C - Never)
6- Case of any other object other than string referenced by a local variable
Integer(4) created at (marker 6) is ilegible for garbage collection after line:
(marker B - marker C - Never)
Another question would be, will static or final modifiers on the member variables OR the method affect the answers??
Thanks in advance
[ October 29, 2002: Message edited by: Alfred Kemety ][/qb]<hr></blockquote>
1 - never
2 - marker A
3 - never
4 - never
5 - marker B
6 - never
[ October 29, 2002: Message edited by: Shishio San ]
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what's this thing about local variables beeing only eligible for GC after the method finish executing???
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alfred Kemety:
So what's this thing about local variables beeing only eligible for GC after the method finish executing???


If you don't set dString to null before the end of the method, i believe it will only be eligible for GC after the complete excution of the method.
[ October 29, 2002: Message edited by: Shishio San ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So what's this thing about local variables beeing only eligible for GC after the method finish executing???


A local variable can be a primitive or a reference to an object. These are NEVER garbage collected or said to be eligible to be garbage collected.
In the case of a reference to an object it is the object being referred to that may or may not be eligible for garbage collection.
If the reference to the object is the ONLY reference to the object, and that reference gets set to null, or gets changed to refer to another object, or goes out of scope then the object being referred to is eligible for garbage collection.
If the object being referred to is a string literal then garbage collection DOES NOT apply.
An object such as that formed by new String("a string literal"); is NOT a string literal.
Note that being eligible for garbage collection DOES NOT mean the object WILL be garbage collected.
-Barry
[ October 29, 2002: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alfred Kemety:
So what's this thing about local variables beeing only eligible for GC after the method finish executing???


Alfred,
Local variables can be eligible for garbage collection before the method runs to completion.

If the garbage collector runs the output of the program could be a series of four integers such as 0123. The ordering of the integers is not guaranteed.
Method K.m1 creates five objects of type J. Each instance has a name represented by an integer between 0 and 4 inclusive. If the garbage collector does not run then the program will produce no output. If the garbage collector does run then the output of the program could be a series of integers that are the names
of four of the five objects. As each new object is created its reference is assigned to the reference variable j. The previously referenced object then becomes eligible for garbage collection. The last object created, 4, is not available for garbage collection until method m1 runs to completion. The while loop in the synchronized block will never complete because J.notFinalized will never return zero.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:

An object such as that formed by new String("a string literal"); is NOT a string literal.
-Barry


I would like to add just one minor detail to the above statement.
The program statement new String("abc") results in the creation of two String objects. The first is the String literal "abc". The second is a new object that is a copy of the original String. The copy of the literal is not another literal.
The following would print "false".
String s1 = "abc";
String s2 = new String(s1);
System.out.print(s1 == s2);
 
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
Dan, that last addition of yours was what I was trying to imply, it's just I was to lazy to write it down
BUT I would like to argue with your use of the term "local variable". To me a local variable is one that exists only on the stack, the object that a local reference variable references exists in/on the heap. That's where garbage collection happens. The local reference variable (if set to null) is still on the stack until it goes out of scope; it's just not referencing anything, and is never garbage collected.
-Barry
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Dan, that last addition of yours was what I was trying to imply, it's just I was to lazy to write it down
BUT I would like to argue with your use of the term "local variable". To me a local variable is one that exists only on the stack, the object that a local reference variable references exists in/on the heap. That's where garbage collection happens. The local reference variable (if set to null) is still on the stack until it goes out of scope; it's just not referencing anything, and is never garbage collected.
-Barry


Yes, I agree completely. I didn't mean to imply anything different.
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gosh i was mistaken about local reference variables.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually this gets pretty tricky. I had a mock exam test question that relied on the local variable not being GCed until the method exited. This turned out to be true in early versions of the JDK but incorrect in later versions.
By testing with a class having a finalizer that wrote to System.out, we determined that local variables could be GCed at the point where it was impossible - due to the program logic - for the variable to be used again. I never did try disassembling the bytecode to see how this was accomplished.
Bill
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Brogden:
I had a mock exam test question that relied on the local variable not being GCed until the method exited. This turned out to be true in early versions of the JDK but incorrect in later versions.


Bill, I failed to thank you for mentioning that earlier in this other thread.
Thank you again.
Have you found a new publisher for your SCJP Exam book? Will you be doing a 1.4 update?
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, just my two cents (about what my stock is worth today) worth on "for the purpose of the exam".
You *will* need to know when an object becomes eligible for GC, and you will need to know the point at which that occurs. You will need to recognize the following scenarios as making an object eligible:
* the moment that the last reference to the object is set to null
* the moment that the last reference to the object is assigned a different object.
* the moment that the last reference to the object went out of scope.
BUT -- Big Point on that last one -- for the purpose of the exam, out-of-scope will be at the method-level, not something scoped smaller than the method itself.
You DO still need to know the exact rules about scoping, for other parts of the exam, but you will not be required to assess whether a reference becomes eligible or not -- based on scope -- for anything less than method-level scope.
And you of course do need to know the other rules... be certain you understand that "when an object has no more references" is not the full story. The implicit statement is really, "when an object has no more references *that are reachable from a live thread*".
So an object *can*, of course, still have references to it and yet be eligible for GC. And that you can have "islands of isolation" where two or more objects refer back to one another, but no live thread can reach either of them.
But you all knew that already. I just like to type
Hope that helps.
Also, I wouldn't stress too much on gc and the String pool.
And by the way, can somebody tell me what this graemlin above (the guy who sticks out his tongue) is for? I like it, but just do not want to use it inappropriately
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic