• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

one more on GC

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements about Java's garbage collection are true?
a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.
I know that b is an answer.
vrinda
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read in another discussion group that
calling the finalize() method of the super class
is also necessery/advisable when overriding it.
API says:
The finalize method of class Object performs no special action; it
simply returns normally. Subclasses of Object may override this
definition.
Does anybody know the real answer?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As of c, I am also confused. In fact a program below
class TestFinalize
{
protected void finalize()
{
System.out.println("JavaRanch");
}
public static void main(String[] args)
{
TestFinalize tf = new TestFinalize();
}
}
runs fine, no errors.
However, the java tutorial, last paragraph says that http://java.sun.com/docs/books/tutorial/java/javaOO/garbagecollection.html
"If you override finalize, then your implementation of the method
should call super.finalize as the last thing it does"
Regards
Gunjan
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answers to this question are:
a) Garbage collector can be explicitly called as follows: Runtime.getRuntime().gc()
b) and c)
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And many questions on the exam expect the answer that you cannot force garbage collection. There is a huge conflict here. I would have answered correctly as this was on a mock exam but am glad I did not get the question.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The confusing issue may be that you, at any time can request that the GC be run. However, your request may not be accommodated and the GC may in fact never run during the life of your application.
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's A, B and C.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The link doesn't work.....
Here's the one that does....
http://java.sun.com/docs/books/tutorial/java/data/garbagecollection.html
 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gunjan,
The reason for c) is that the finalize methods of the superclasses are not automtically called during object cleanup like they are during object construction. So unless you explicitly call super.finalize(), the superclasses' resources won't get cleaned up.
Eric
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The catch in the question
a) The garbage collector can be invoked explicitly using a Runtime object.
is that it does not say that the gc is foced but instead it says you explicitly invoke the gc. Hence its true.
 
I just had the craziest dream. This tiny ad was in it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic