0

Should we reference Objects to null in finally block while Objects are in local scope of method? What are the performance issues resolved by doing so?

code:

Map<String, Map<String, String>> getData(Map<String, String> params) {
    StringBuilder query = new StringBuilder(); // Construct query using it
    ResultSet rs = null;
    try {
        rs = DataUtil.getData(query);
        while(rs.next) {
            // store data in return Map
        }
    } catch(ConnectionException ce){
        ce.printStackTrace();
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        rs = null;
        query = null;
    }
}

Here, freeing up rs and query seems to fulfill no purpose as they are automatically valid for garbage collection after method execution. Any help would do great.

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
  • 2
    The scope of your objects is local... I dont think it makes any difference... They will be ready for GC as soon as you exit from the block... – TheLostMind Jan 17 '14 at 09:46
  • 6
    It will make your code harder to read with no positive difference in performance. So... – Kayaman Jan 17 '14 at 09:46
  • 1
    You are right. Setting locally declared reference variables to null in finally doesn't add anything to performance or help garbage collection AFAIK – Karthik Kalyanasundaram Jan 17 '14 at 09:46
  • 2
    not necessary to require to set null but require to close you resources like close connection,statement,resultSet – Darshan Patel Jan 17 '14 at 09:46
  • 1
    @DarshanPatel - Good point.. But thats a totally different context.. Such connections will not be local objects.. So, they have to be closed.. and closing of connections occurs asynchronously... – TheLostMind Jan 17 '14 at 09:48
  • right @TheLostMind.Context is local scope. If some Object is in local scope, why use finally to nullify these objects. – Vinay Prajapati Jan 17 '14 at 10:30

4 Answers4

1

No we should not. It makes the code a lot harder to read and in most cases (for example your example) it does absolutely nothing, as GC will start after the scope anyways.

In general we should let Java handle its Garbage Collection and should not try to make it more efficient by writing strange code.

LionC
  • 3,106
  • 1
  • 22
  • 31
1

There is no need to do so. There is no benefit at all. Local variables are not reachable once the execution gets out of the method. GC will be done on all objects which are not reachable. This is no where related to performance issues.

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
1

No don't do this because it doesn't mean anything when object is out of scope. All the java objects deallocation will be handled by Garbage Collector automatically when there is no owner or reference to that object in heap.

Finally is very handy because it'll get executed whether there occurs an exception or not in your code. Use finally when you want to perform some important resource management work like closing an opened database connection etc. HTH

iamyogish
  • 2,372
  • 2
  • 23
  • 40
1

There are edge cases where this can matter quite a lot: it can make the difference between a running program and an OutOfMemoryException. Consider this code:

long[] hugeAry = new long[Integer.MAX_VALUE]; // barely fits on the heap
... work on hugeAry;
hugeAry = null;
int[] intHugeAry = new int[Integer.MAX_VALUE];
... work on this one

Commenting out hugeAry = null will easily cause an OOME. The cases can get even more bizarre than this—check out this related topic on SO.

But, don't get me wrong: in 99.9% of cases, nulling out locals (in finally or otherwise) does not matter.

Community
  • 1
  • 1
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436