Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection during exception

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a method completes, all the local object defined in the method become eligible for garbage collection. In case an exception occurs when does the local object become eligible for garbage collection.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IS it a statement? or question?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should try this when asking question.
 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the Exception comes to JVM then garbage collector will not run because when the exception comes to jvm, it will terminate the current program and remove all the stuff from the heap.so,it does not matter whether the objects are eligible or not.

and if the exception is caught by some catch statement or in other method,then it will not affect anything because every object is on the heap and nothing is removed due to this.

Exception occurs just because of the code which jvm can't execute.so, it wouldn't affect the objects created in the heap.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for this post sumit ,i didn't knew this thing
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the question. When an object becomes out of scope, it becomes eligible for GC. What has that to do with exceptions?? Even if you catch the exception, and the reference variable was declared inside the try block, that variable will be out of scope and thus the object it references will be eligible for GC...
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit has made a good point.
An Object is a candidate for GC when it's unreachable, or not accessible by a live thread.
So, it shouldn't differ in case of Exceptions.
But JVM might have a different way to unallocating memory in case of Exception - in order to improve performance.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But JVM might have a different way to unallocating memory in case of Exception - in order to improve performance.

Maybe, but for the SCJP exam we are concerned with only when objects become eligible for GC not how they are actually garbage collected...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic