• 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

yet another question about GC... does Exceptions get g. collected?

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there!
Look at this code:

Let's say this function compiles without error and it's invoked with the num parameter "abd".
The Integer.parseInt(num) function call will create an NumberFormatException object and throw it. Then we catch and associate it to a reference named "ignore", that goes out of scope right after catch statement.
Does this object (NumberFormatException) get eligible for garbage collection when the method returns?
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess so.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bengt. I use the finalize method and System.gc to see if an object will get garbage collected. I know System.gc is only a request and not a guarantee, but it seems to cooperate for me.
This example shows that finalize is called. So the exception object is eligible for garbage collection after the method returns.

You can also try some other interesting experiments. Put the System.gc() after the try statement. Put the System.gc() after the try statement and add e = null; in the catch clause.
[ October 17, 2003: Message edited by: Marlene Miller ]
 
bengt hammarlund
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I asked that question because I saw it in a mock exam, that had multiple choice questions and I responded that ONE object was eligible for garbage collection when the method returns. But the correct answer was ZERO.
Am I right, wrong or this particular piece of code is actually a bad example - because there is no garantee of the actual number of objects that are eligible for garbage collection when the method RETURNS?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. One object, the NumberFormatException object, is eligible for garbage collection AFTER the method returns.
After a NumberFormatException is thrown and caught, an Integer object holding the value �1 is created and returned. That object is not eligible for garbage collection. Do you suppose the author of the example was thinking only about the Integer object and forgot about the NumberFormatException object?
 
bengt hammarlund
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am SHURE about it! This question is from Whizlabs 5.2.0 practice exam 1, but I don't think I should put the question of a paid exam here in the saloon. I made a few changes of course (the name of the variables!).
I hope there's no problem.
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the exception object "e", eligible only AFTER the method returns?
I feel it has scope only within the catch block, so outside of it, it should be eligible.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, e is out of scope outside of the catch block.
(The scope of a parameter of an exception handler that is declared in a catch clause of a try statement is the entire block associated with the catch. JLS 14.19)
Whether an object is reachable when the reference variable goes out of scope is a topic of much debate on this forum.
The GC source I am relying on is Appendix A The Truth About Garbage Collection from the book Java Platform Performance, whose authors are engineers of the Java Performance Team at Sun.
I am going to apply Section A.3.3 of JPP quoting as much as possible, so that I do not get it wrong.
When a reference variable declared in a block goes out scope, it might seem that the variable would be pulled off the stack and the associated object would become unreachable. After all, outside of the block, there is no syntax defined that would allow the program to access the object again. However, an efficient implementation of the JVM is unlikely to zero the reference when it goes out of scope. The object continues to be strongly referenced, at least until the method returns.
Some people say an optimizing JVM will null a stack reference variable when the variable goes out of scope. The authors of the Java Platform Performance say just the opposite. An efficient JVM will not null the stack reference variable when the variable goes out of scope.
[ October 19, 2003: Message edited by: Marlene Miller ]
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene. That was a very helpful post.
 
bengt hammarlund
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene! But anyway, one should agree that this is a not quite good exemple of code for GC objective. The best way to go in this kind of question is IGNORING the objects that are out of scope? Like exceptions, etc?
Thanks for any opinion.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My best guess is we will not see GC questions on the exam about objects referenced by variables that are out of scope. However we will see such questions on mock exams and the correct answer will vary.
I suppose if I had to choose, I would count them, because the reference has not been set to null or reassigned at the source code level and the method has not returned. As far as I know, the object is still reachable from a GC root.
(In your spare time, you might search this forum for anything written by Kathy Sierra (member number 37766). She has given us many insights about what to expect on the exam.)
[ October 21, 2003: Message edited by: Marlene Miller ]
 
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic