wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes gc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "gc" Watch "gc" New topic
Author

gc

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi,
i got a doubt regarding garbage collector.In the finalize()
method can the object have a chance not to be garbage collected
using classname c1 = this;correct me where i am wrong?
Thanks!
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Yes, a finalizer can "resurrect" an obj by storing the this reference, as you did in the abv stmt.
This is described in the book by Flannigan, O'Reilly Pubs.
While doing so may not have any technical value,
one could do this. Also, you should note that the finalizer
is invoked only once.
Regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks!for the reply.I have read few forums here on gc.
But i am unclear regarding gc on string literals.Does
gc apply for String literals i.e does gc consider
the objects in pool as if created in heap and garbage
collect them???Or does gc doesnot perform
on string literals at all.Pl. clarify my doubt .
Thanks!
[This message has been edited by avn (edited July 30, 2000).]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

You should also have seen prior suggestions that
for SCJP purpose, disregard string literals.
In most mock exams from my experience, I have not
come across any misleading qstns regarding this issue
and found that the abv stmt is a reasonable assumption
to make, atleast I agreed with others and went ahead.
Most qstns I have come across go like this:
...
2. .....
3. String a = "abcd";
4. .....
:
:
9. a = null;
10. ...
at which line is the obj refered by 'a' eligible for GC
(assuming no other line sets the obj ref 'a' to null before
line 9).
Obviously its after line 9 or line 10 or
whatever is the choice given saying similar to the abv.
I am sure you agree with my answers....
I guess this is the concept we should know, IMO.
Regds.
- satya
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Hmm... I beg to differ here.
Actually the runtime constant pool is managed by the JVM and exists probably for a longer duration than the life of an object. Hence, it is desirable to keep the values in the pool alive till the time the class is actually unloaded by the JVM. So, the normal GC semantics that applies to new()-ed objects should not be assumed to apply to constants in the string pool.
Ajith
[This message has been edited by Ajith Kallambella (edited July 31, 2000).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Madhav and Ajith for the reply.But i arent
your views contradicting.Pl. tell me the actual concept
and also the concept which should be followed for the actual exam.???
Savithri Devaraj
Ranch Hand

Joined: Jun 26, 2000
Posts: 103
Originally posted by Ajith Kallambella:
Hmm... I beg to differ here.
Actually the runtime constant pool exists for a per-class, per-interface basis. Hence, it is desirable to keep the values in the pool alive till the time the class is actually unloaded by the JVM. So, the normal GC semantics that applies to [b]new()-ed
objects should not be assumed to apply to constants in the string pool.
Ajith[/B]

Ajith,
I agree with you - Only objects that are "new()" ed can be garbage collected, not literals in the string pool.
But, what do you mean by
"the runtime constant pool exists for a per-class, per-interface basis."??
Are string literals in String pool not seen across subclasses? I will have to try an example later.
Savithri
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Good point!!...well that's what the JVM spec says( Section 3.5.5 ). I too tried writing some test code, however the behavioiur is not apparent. Since my statement may be too confusing, I will remove that phrase from there till such time I can prove it!!.
Thanks for noticing
Ajith
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ajith - I think the key here is that there is more than one kind of "pool". There is a runtime constant pool for each class or interface. There is also a pool of interned strings - one intern pool for the whole JVM. If three classes A, B, and C all mention the literal "Ajith", then the heap will have a single instance of the String object Ajith, and there will be at least four references to it - one in each of the class runtime constant pools, and one in the interned String pool.


"I'm not back." - Bill Harding, Twister
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
Ajith: I didn�t read the article you post about garbage collector yet, but I know that the String pool behaves different than any kind of objects for gc, I read a lot of that in this forum. My question is:
For the exam, if you have a question like the one posted by Madhav, what do you have to answer?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Jim,
Thanks for the clarification. Apparently I became over-enthusiastic about quoting that statement from JVM spec, only to cause more here. I apologise for the mess I caused unintentionally
Marcela, I don't think the SCJP questions will be so ambiguous. You can expect questions on GC, however, they will mainly concentrate on objects created using the new() keyword. Happy?

Ajith
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: gc
 
Similar Threads
Garbage Collection
Garbage Collection- Islands of Isolation
passed SCJP 1.4 with 72 %
WA #1.....word association
Netbeans forces Garbage collection ,not request Garbage Collection