aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection during exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection during exception" Watch "Garbage collection during exception" New topic
Author

Garbage collection during exception

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
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.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

IS it a statement? or question?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

you should try this when asking question.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

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

Joined: Jun 23, 2010
Posts: 432

thanks for this post sumit ,i didn't knew this thing
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
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.


OCPJP6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Garbage collection during exception