Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Garbage collection.." Watch "Garbage collection.." New topic
Author

Garbage collection..

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
please, let me know the correct answer when it is available for
garbage collection.
1) At what point is the object anObj available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: String anObj = "sample";
06:
07: anObj.trim();
08:
09: anObj = anObj.toUpperCase();
10:
11: anObj = null;
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) At line 5
b) At line 7
c) At line 8
d) At line 10
e) At line 12
2) At what point is the object anObj available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11
Thanks,
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
The question is worded incorrectly. Here is how it should be, IMO:
1) At what point is the object CREATED AT LINE 05 available for garbage collection.
<PRE>
01: public class Base{
02:
03: private void test() {
04:
05: String anObj = new String("sample");
06:
07: anObj.trim();
08:
09: anObj = anObj.toUpperCase();
10:
11: anObj = null;
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) At line 5
b) At line 7
c) At line 8
d) At line 10
e) At line 12
</PRE>
My answer is (d) at line 10. <CODE>anObj</CODE> is now denoting a different object from "sample". The trim() method doesn't do much in this case.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
2) At what point is the object CREATED IN LINE 06 available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = new String("sample");
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11

I say (e), after line 11. The object's scope is inside the braces of the <CODE>if</CODE> block. Inside the block, <CODE>locObj</CODE> is still denoting the object created at line 06.
gunjan
Ranch Hand

Joined: Jan 28, 2000
Posts: 33
Tony:
I got a recent mail from Prep-Talk list which brought up a very interesting point, that string literals are not garbage collected. Somehow I do not feel this right as all String literals will remain in the pool and consume memory. I feel string literals are pure objects and should be dealt the same way as other objects.
What do u feel.
Regards
Gunjan

Regards<BR>Gunjan
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I feel that for the exam, in the GC questions, objects will always be created with the <CODE>new</CODE> keyword to avoid the possibility of confusion. The exam does not go into the management of strings in the pool and just wants to test whether one has understood the basics of GC and not the details.
That's what I read from the people that have taken it anyway. I haven't.
Notice that for other types of questions with say, Strings, it is very possible to have string literals right and left. But I would expect that in GC, they would be <CODE>new</CODE>ly allocated objects.

[This message has been edited by Tony Alicea (edited February 07, 2000).]
Ajay
Greenhorn

Joined: Feb 07, 2000
Posts: 8
Tony,
How can you say that (In Question 1) that object is garbagr collected at line 10 and not at line 12?
Please expalin, I am very much confused
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Ajay:
<PRE>
05: String anObj = new String("sample");
06:
07: anObj.trim();
08:
09: anObj = anObj.toUpperCase();
10:
</PRE>
Statement 09 causes the reference <CODE>anObj</CODE> to refer to a new String object "SAMPLE", so <CODE>anObj</CODE> does not point to "sample" anymore so this last object is free for GC.

[This message has been edited by Tony Alicea (edited February 07, 2000).]
Badri Sarma
Ranch Hand

Joined: Apr 01, 2003
Posts: 72
Hi Tony,
In the question all are string literal, and literal objects exist for the life of the class, and are never independently garbage collected.
So i think we cannot determine when actually string literal object can be garbage collected.


Thanks<br />Badri
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Garbage collection..
 
Similar Threads
Can anyone help
Garbage Collection
GC
Garbage collect from Sarga.com Exam
Garbage Collection