1

Possible Duplicate:
Does setting Java objects to null do anything anymore?

Will dereferncing the unused object(assigning with null) increase performance in lengthy methods? If it increase, is there is any thumb rule for deciding when to assign null. since this requires an extra step in the code.

Community
  • 1
  • 1
Sujith
  • 1,127
  • 1
  • 13
  • 20
  • 1
    See [Does setting Java objects to null do anything anymore? ](http://stackoverflow.com/questions/850878/does-setting-java-objects-to-null-do-anything-anymore). Note that dereferencing is the wrong word. That means following a pointer/reference; in Java, it's basically using `.` on an object. – Matthew Flaschen Nov 02 '10 at 06:18
  • 1
    Generally assigning a local variable to null is not required. If your method is so long that it makes a difference, I would suggest breaking up your method into more manageable sub-methods and you should find there is no long any advantage. – Peter Lawrey Nov 02 '10 at 07:00
  • @Peter: Hey buddy, have a look to my answer. Is it any significantly different? ;) – Adeel Ansari Nov 02 '10 at 07:02
  • @Adeel, very similar to suggestion 1 and one hour earlier. ;) Not sure a profiler will help in this case, but you are right to suggest it because a profiler is likely to find other parts of the code make more difference to performance. – Peter Lawrey Nov 02 '10 at 20:27

2 Answers2

1

Loosely speaking, it doesn't or say very unlikely. This is how I will approach,

  1. Introduce new methods by splitting the long method, and name those appropriately
  2. Now check where exactly you have performance problem. You can use some profiler, but even logging start time and end time for each method will provide some idea
  3. Then fix that
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
1

It depends on the context. If you assign null value to an unused object then it will become eligible for garbage collected. But there is no guarantee that the memory will become free.