aspose file tools*
The moose likes Java in General and the fly likes Final modifier and Java performance tuning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Final modifier and Java performance tuning" Watch "Final modifier and Java performance tuning" New topic
Author

Final modifier and Java performance tuning

Kevin Hermann
Greenhorn

Joined: Aug 12, 2010
Posts: 7
Hi,

At my company a number of developers have encountered websites claiming that by applying the final modifier to local variables (primarily Objects) it actually helps improve the performance in terms of speed for a Java Web Application. This is strange to me because this is not the purpose of applying the final modifier to local variables. The final modifier should ONLY be used if a calling client has an opportunity to modify what object a specific object reference variable is referring to and this capability is not desired.

We are using WebSphere Portal 6.1, WebSphere Application Server 7.0, EJB 3, and JAX-RPC/JAX-WS web services for a portal.

Does anyone have any special insight into this matter?


SCJP 6, SCWCD 5
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't really see how applying final to a local would increase performance; maybe to a class member, but local? I'd have to see the compiled code differences (then any JITted differences) to be sold on the idea.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39814
    
  28
Welcome to the Ranch
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

It's amazing what you can find on the internet, isn't it?

Personally I would classify such an optimization in the same category as aerodynamic paint for race-cars. It's all very well but you wouldn't bother with aerodynamic paint for a short-haul delivery truck.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Kevin, can you point out any of these web pages your co-workers have found? I agree with the others that it seems extremely unlikely that this "optimization" will help anyone at all - at least in terms of performance. But I'm curious to find out where the idea comes from.
Hari haran Ravi
Greenhorn

Joined: Aug 11, 2010
Posts: 26
Hi Kevin. Well I actually disagree with your point. Sorry. But the actual concept is something like this.

Final objects are very expensive interms of performance. Because, if you declare something as final, it means that it will remain throughout the entire lifetime of the program.. Whereas, if you consider the normal non-final objects, if they do not have a purpose, they are taken care of the garbage collector (GC). But in the case of a final object, whether you use it or not, the GC won't consider it. This will result in performance loss due to memory management, as a large number of memory has to be managed because you are saying that you are going to declare local variables as final variables.

Even the Sun Microsystems' documentation reports that the usage of final objects should be highly cared about as they are directly linked to the program's performance.

Hope this information is usefull.

Regards
Hari
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Hari: no, this is not remotely true. For starters there is no such thing as a final object - only final variables. And it makes a big difference whether we're talking about local variables, instance variables, or static variables. A static final variable referring to an object will probably cause that object to never be garbage collected. Which might cause problems, but remember, it's just one object. Probably not a problem unless that object is a large collection containing many other objects. A final instance variable may cause the referenced object to never be collected. But very often, it doesn't. And a final local variable - which is what we're talking about in this thread - will never prevent an object from ever being GC'ed. Unless the method is in an infinite loop, I suppose. Which is pretty rare.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

But in the case of a final object, whether you use it or not, the GC won't consider it.


Said object would be eligible for GC after the execution comes out of scope.

I have never heard of the 'final' keyword improving performance. I dont see any reason why it should.

If the claim were to be backed by a valid technical explanation, that would make me change my mind. But I doubt someone can provide one.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Deepak Bala wrote:I have never heard of the 'final' keyword improving performance. I dont see any reason why it should.

For a method it could, because it would mean there'd be no further method resolution. For a local, I'm still skeptical, but if the compiler knows the value will never be assigned it might be possible to do a micro-optimization of some sort.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39814
    
  28
David Newton wrote: . . . For a local, I'm still skeptical, but if the compiler knows the value will never be assigned . . .
If you have a local variable never assigned, you should delete that local variable anyway.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Re-assigned.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39814
    
  28
If it's re-assigned, then sorry; I must have misunderstood the previous post.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, I left out the "re-" part; obviously a variable that is *never* assigned should just be removed (although the compiler would optimize it away anyway).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39814
    
  28
. . . and IDEs would notice; Eclipse would mark it with a yellow triangle and suggest removing an unused variable.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hari haran Ravi wrote:
Even the Sun Microsystems' documentation reports that the usage of final objects should be highly cared about as they are directly linked to the program's performance.

Please give us a link to this documentation.
Kevin Hermann
Greenhorn

Joined: Aug 12, 2010
Posts: 7
All,

Here is the documentation that a Wipro contractor provided where there is what I now know are unsubstantiated claims that final local variables have a performance increase. Based on my personal review of this documentation, there is NO CONCLUSIVE evidence that final local variables are an optimization over non-final local variables. Here is the documentation where these performance claims can be made.

Check Style Rules:
http://checkstyle.sourceforge.net/config_coding.html#FinalLocalVariable
http://checkstyle.sourceforge.net/config_coding.html#ParameterAssignment

Forums:
http://www.javalobby.org/java/forums/t84151.html
http://codemonkeyism.com/all-variables-in-java-must-be-final/
http://forums.sun.com/thread.jspa?threadID=5402099
http://www.coderanch.com/t/444731/java/java/CheckStyle-Rule-Making-final-local
http://www.coderanch.com/t/202785/Performance/java/declaring-local-variables-final
http://stackoverflow.com/questions/316352/do-you-finalize-local-variables-and-method-parameters-in-java
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The Checkstyle stuff has *nothing* to do with performance, those rules are entirely about coding style and best practices.

I could still see method finality being a potential help, depending on how the JVM and any JITting work, but still not convinced about locals. Thanks for taking the time to pursue this further!
Kevin Hermann
Greenhorn

Joined: Aug 12, 2010
Posts: 7
Thanks to everyone who took the time to comment on this issue. Since my personal research provides no definitive evidence that final local variables, both primitives and object references, we can close this thread.

Cheers!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

We don't close threads just because they've been answered. Someone may still want to reply. And if not, then this thread will slowly sink away into the depths of JavaRanch.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final modifier and Java performance tuning