Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Tolls

Thread: Java Performance Tips

  1. #1
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Post Java Performance Tips

    Try, catch block should not be in loop (unless specifically required).



    All Collection API Classes should be specifically reinitialized to null (since JVM has to specifically do it & GC spends much time in doing these functions).



    Release JDBC resources when done with tasks.



    Use inner join instead of multiple queries.



    Optimize SQL queries.



    Always use Dynamic SQL (Prepare Statement) rather than the static SQL like Statement.



    Try to avoid repeated DB calls to retrieve the same data. Instead of that retrieve the data from the table and keep the data in Java objects and use that in the subsequent call. (make Model class for that and use getter, setter methods)



    Avoid Select * instead give the names of fields to be selected in query (e.g. Select id,name from Student).

    Using getXX(int ColumnIndex) instead of getXX(String columnName): Depending upon the no of columns in your resultSet, a getXX field operation using column Index is two times as fast as than using Column Name.



    Do not use Vectors, instead of that use Collection classes like ArrayList.



    Avoid using method call as loop termination test. E.g. Don’t use list.size(), instead use store that in a temporary variable and then use the temporary variable for testing loop termination test.



    Avoid throwing Exception in normal flow of execution since try-catch-finally block is costly.



    Build proper WHERE clause in SQL SELECT for database indexing. (In left of Join select table with less no of records).



    Try using java Collection classes instead of array.



    String Concatenation: Check that For String concatenation use StringBuilder (JDK 1.5 has StringBuilder over StringBuffer) instead of String ‘+’ operator.



    Debugging Statements: Do not use System.out.println or printstackTrace(). Use log4j.debug() for debugging. In catch blocks add log4.error() wherever applicable.



    Prepare Statement: Use prepared statement for retrieving data when we have parameterized queries(insert, update, delete) else use statements.



    Avoid object instantiation inside Loop. If possible create object outside loop and reuse the same object inside the loop.



    Minimize object lifetimes. Keep the lifetimes of your data as short as possible. Place the declaration in the most nested block that first uses the data. If operator new is used, do the new just before the first use, and the set the reference to null just after the last use.



    Minimum use of Session variables in the code instead set variables in Request. Set session variables as null after use.



    The constants are not static we can make it final for better performance(e.g. if we have fix value for variable than make it final private void testMethod(final int id){}).



    Apply Transactions wherever necessary.



    Apply Query Caching if is required as per functionality.



    Minimize data conversions or Castings



    Try to use primitive java objects instead Wrapper classes as like Wrapper classes (i.e. Integer) allocate large data amounts in memory.



    Replace a long if-else-if chain by a switch if possible; this is much faster.



    Declaring a method as private, final, or static makes calls to it faster. Of course, you should only do this when it makes sense in the application.



    Do not add JavaScript directly in the JSP Code; instead include JS File and include in Jsp (e.g <script src=”include/common.js”></script>).



    Use STRUTS, JSTL specific UI Tag Library in JSP pages.



    Exceptions are taken care in the code and highest levels of Exceptions are routed to Error Page; others are properly logged using logger.

    see more : (snipped link)
    Last edited by Tolls; 09-18-2013 at 10:22 AM. Reason: Removed spammy link

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,749
    Rep Power
    5

    Default Re: Java Performance Tips

    Why would anyone visit those links after what you posted? You just posted meaningless and at times completely untrue statements without any kind of reasoning or explanation behind them at all. Judging by the quality of this post I'd say you plucked most of this stuff from roseindia.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,853
    Rep Power
    19

    Default Re: Java Performance Tips

    I'm game for a laugh...:)

    Quote Originally Posted by visionsdeveloper View Post
    Try, catch block should not be in loop (unless specifically required).
    Nice get out at the end, but that get out makes the whole point nonsense.
    Quote Originally Posted by visionsdeveloper View Post
    All Collection API Classes should be specifically reinitialized to null (since JVM has to specifically do it & GC spends much time in doing these functions).
    Nonsense as well.
    Objects should exist in the smallest scope required by the application.
    Quote Originally Posted by visionsdeveloper View Post
    Release JDBC resources when done with tasks.
    That applies to all resources.
    I'll give you half a point for this one.
    Quote Originally Posted by visionsdeveloper View Post
    Use inner join instead of multiple queries.
    I agree you should let the db do the work it's designed for, but insisting on an inner join is incorrect.
    Half a point again.
    Quote Originally Posted by visionsdeveloper View Post
    Optimize SQL queries.
    OK. Your first full point!
    Quote Originally Posted by visionsdeveloper View Post
    Always use Dynamic SQL (Prepare Statement) rather than the static SQL like Statement.
    Oh, you were doing so well as well.
    Nope. Sometimes a plain old Statement is fine.
    Sometimes you might even mix bind variables with concatenated ones as well. Isn't life complicated?
    Quote Originally Posted by visionsdeveloper View Post
    Try to avoid repeated DB calls to retrieve the same data. Instead of that retrieve the data from the table and keep the data in Java objects and use that in the subsequent call. (make Model class for that and use getter, setter methods)
    Agree with the Model, disagree that you shouldn't make multiple calls. It depends. Storing large amounts of data can make an app unscalable.
    Half.
    Quote Originally Posted by visionsdeveloper View Post
    Avoid Select * instead give the names of fields to be selected in query (e.g. Select id,name from Student).
    I'll give you this, as I tend to agree...though I'm sure there are cases where a simple select * might be reasonable. I don't like them, though.
    Quote Originally Posted by visionsdeveloper View Post
    Using getXX(int ColumnIndex) instead of getXX(String columnName): Depending upon the no of columns in your resultSet, a getXX field operation using column Index is two times as fast as than using Column Name.
    This falls under "premature optimisation".
    For complex queries using the column name can help the readability of the code.
    So depends...half.
    Quote Originally Posted by visionsdeveloper View Post
    Do not use Vectors, instead of that use Collection classes like ArrayList.
    Well, a Vector is now a Collection class, it implements List.
    But, unless you need thread safety (for which there are other options), yeah.
    Quote Originally Posted by visionsdeveloper View Post
    Avoid using method call as loop termination test. E.g. Don’t use list.size(), instead use store that in a temporary variable and then use the temporary variable for testing loop termination test.
    Premature optimisation.
    Quote Originally Posted by visionsdeveloper View Post
    Avoid throwing Exception in normal flow of execution since try-catch-finally block is costly.
    Um, if you're throwing an exception then presumably it isn't "normal flow".
    You probably want to reword this one.
    Quote Originally Posted by visionsdeveloper View Post
    Build proper WHERE clause in SQL SELECT for database indexing. (In left of Join select table with less no of records).
    Entirely depends on the database.
    Any proper one will handle your SQL correctly without you attempting to second guess it.
    Quote Originally Posted by visionsdeveloper View Post
    Try using java Collection classes instead of array.
    Depends.
    Quote Originally Posted by visionsdeveloper View Post
    String Concatenation: Check that For String concatenation use StringBuilder (JDK 1.5 has StringBuilder over StringBuffer) instead of String ‘+’ operator.
    Depends.
    Quote Originally Posted by visionsdeveloper View Post
    Debugging Statements: Do not use System.out.println or printstackTrace(). Use log4j.debug() for debugging. In catch blocks add log4.error() wherever applicable.
    Depends on what you're doing.
    Quote Originally Posted by visionsdeveloper View Post
    Prepare Statement: Use prepared statement for retrieving data when we have parameterized queries(insert, update, delete) else use statements.
    Um...this makes no sense? You might want to reword it. Especially since this seemingly contradicts your earlier one.
    Quote Originally Posted by visionsdeveloper View Post
    Avoid object instantiation inside Loop. If possible create object outside loop and reuse the same object inside the loop.
    No point. See my point earlier about defining variables in the smallest possible scope. That applies here.
    No wonder you sometimes feel the need to set things to null.
    Quote Originally Posted by visionsdeveloper View Post
    Minimize object lifetimes. Keep the lifetimes of your data as short as possible. Place the declaration in the most nested block that first uses the data. If operator new is used, do the new just before the first use, and the set the reference to null just after the last use.
    This contradicts the one I've just dealt with. Did you actually read through these or simply mindlessly copy/paste?
    Quote Originally Posted by visionsdeveloper View Post
    Minimum use of Session variables in the code instead set variables in Request. Set session variables as null after use.
    OK.
    Quote Originally Posted by visionsdeveloper View Post
    The constants are not static we can make it final for better performance(e.g. if we have fix value for variable than make it final private void testMethod(final int id){}).
    Premature optimisation.
    Quote Originally Posted by visionsdeveloper View Post
    Apply Transactions wherever necessary.
    Vague. And how does this relate to performance?
    Quote Originally Posted by visionsdeveloper View Post
    Apply Query Caching if is required as per functionality.
    Vague.
    Quote Originally Posted by visionsdeveloper View Post
    Minimize data conversions or Castings
    Vague.
    Quote Originally Posted by visionsdeveloper View Post
    Try to use primitive java objects instead Wrapper classes as like Wrapper classes (i.e. Integer) allocate large data amounts in memory.
    They do? Define large. And what if you have them in a Collection? Or need them to be possibly null? What's the cost of conversion?
    Quote Originally Posted by visionsdeveloper View Post
    Replace a long if-else-if chain by a switch if possible; this is much faster.
    Have you timed this?
    If nothing else this falls under premature optimisation.
    Quote Originally Posted by visionsdeveloper View Post
    Declaring a method as private, final, or static makes calls to it faster. Of course, you should only do this when it makes sense in the application.
    The last sentence makes this another vague point.
    Quote Originally Posted by visionsdeveloper View Post
    Do not add JavaScript directly in the JSP Code; instead include JS File and include in Jsp (e.g <script src=”include/common.js”></script>).
    Why not? How does that affect the performance of the code?
    Quote Originally Posted by visionsdeveloper View Post
    Use STRUTS, JSTL specific UI Tag Library in JSP pages.
    Why Struts? How does that improve performance?
    Quote Originally Posted by visionsdeveloper View Post
    Exceptions are taken care in the code and highest levels of Exceptions are routed to Error Page; others are properly logged using logger.
    How does this improve performance?

    Oh, and I'm going to remove the link.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Performance Tips

    Most of the statements in the OP are pure drivel.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,749
    Rep Power
    5

    Default Re: Java Performance Tips

    Quote Originally Posted by Tolls View Post
    optimize SQL queries
    OK. Your first full point!
    No point from me. This states it as a rule and it is not - absolutely not. This falls in the category premature optimization; you only optimize queries when you have performance issues.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,853
    Rep Power
    19

    Default Re: Java Performance Tips

    Ah, but I was being generous.
    I was basing that on a SQL query that was more complex than "select * from users".
    To be honest, if that had been further down the list it would have got a "vague"...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,749
    Rep Power
    5

    Default Re: Java Performance Tips

    You're too generous for your own good. If you are free to fill in blanks then I'm free to alter the statement altogether: study SQL, study the particulars of the DBMS and right off the bat write properly reasoned SQL statements so you don't even need to optimize later.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Java Performance Tips

    To tell y'all the truth, I stopped reading about half-way through the second one. "Clueless" was already apparent to me.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,749
    Rep Power
    5

    Default Re: Java Performance Tips

    Yes, but its a good thread to abuse :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,853
    Rep Power
    19

    Default Re: Java Performance Tips

    Quote Originally Posted by masijade View Post
    To tell y'all the truth, I stopped reading about half-way through the second one. "Clueless" was already apparent to me.
    What can I say, I had a test running and had time to fritter away...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Learning Java - What are your tips?
    By behedwin in forum New To Java
    Replies: 2
    Last Post: 10-16-2012, 09:56 AM
  2. Any tips on building online 2d game? (Just tips)
    By Lionlev in forum Networking
    Replies: 0
    Last Post: 10-16-2012, 12:56 AM
  3. New To Java. Books? Tips?
    By kevintran in forum Forum Lobby
    Replies: 6
    Last Post: 08-16-2012, 06:03 PM
  4. Java Newbie Overall Tips
    By Hossein in forum New To Java
    Replies: 4
    Last Post: 04-05-2010, 10:32 PM
  5. Performance optimization tips for J2EE
    By karuna in forum Java Tip
    Replies: 0
    Last Post: 06-10-2008, 08:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •