Win a copy of Rust Web Development this week in the Other Languages 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Garbage Collection?

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Q. How many objects are eligible for Garbage collection after line 13? (Assume there are no dependencies in the above code)

Ans. 0

Why the object created inside the argument of catch block is not eligible for GC?
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
throwing does not mean making it for garbage collection.

When exception occurs in try block, then control automatically jump to matching catch statement. In catch, it is going to be referenced by a reference variable e.

Naseem
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See after line 13, there is no object elgible for gc.

I think it sould be better if you say at line 13 not after line 13.


Originally posted by Swapnil:

Why the object created inside the argument of catch block is not eligible for GC?



After the completion of catch block, NullPointerException will be eligible for garbage collection because e is local variable.
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How many objects are eligible for GC after execution of line 11?

Hi Naseem: Sorry to bother u..But as long as I understand the answer to this question should be 1. But it's incorrect....the correct answer is 0.
Pls can u explain it....Thanks a ton!!

Regards
Swapnil
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case , no. of objects eligible for gc could be 0 or 1 depending upon whether exception occurs at line 7 or not.

If you pass num say "Swapnil", then there will be one object eligible for garbage collection which is NumberFormatException object.

But if you pass num say "1", some String which you can parse then number of object eligible for gc will be zero because there will be no exception in that case.


Regards


Naseem
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Naseem...


Regards
Swapnil
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic