This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final modifier and Java performance tuning

 
Kevin Hermann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Re-assigned.
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's re-assigned, then sorry; I must have misunderstood the previous post.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and IDEs would notice; Eclipse would mark it with a yellow triangle and suggest removing an unused variable.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic