- Avoid creating objects in a loop. i.e. Try to avoid create instance of a class in a loop.
- Use String literals instead of String objects (created using the 'new' keyword) if the content is same. JVM maintains an unique list of references for "interned" strings to avoid duplicate String objects in heap memory. So when you create a new string via String Literal, the JVM checks whether the String object exists, if yes, it would returns the reference and would not create a new String object, i.e. unique string objects. But if strings are created with "new", the JVM does not perform checking and would always create a new string object in heap. If you absolutely have to use "new" to create String objects, make sure you "intern" the strings, i.e. intern()
- Make used objects eligible for garbage collection. Whenever you are done with an object make that reference null so that it is eligible for garbage collection.
- Do not keep inheritance chains long. Never keep inheriting chains long since it involves calling all the parent constructors all along the chain until the constructor for java.lang.Object is reached.
- Accessing local variables is faster than accessing class variables
- Use lazy evaluation, lazy object creation whenever possible. Lazy evaluation refers to the technique of avoiding certain computations until they are absolutely necessary. This way we put off certain computations that may never need to be done at all. Lazy object creation allows us to delay the memory allocation to an object till it is not being put into use. This way a lot of memory is saved till the object is actually put in to use.
- Never create objects just for accessing a method.
- Use primitive data types rather than using wrapper classes.
- Whenever possible avoid using class variables, use local variables since accessing local variables is faster than accessing class variables.
- Use StringBuffer for string concatenation operation only if String resolves at runtime. Use + operator for concatenation gives best performance if Strings resolve at compile time.
- Avoid putting method calls, assigning values to variables, or testing for conditions in a loop. Try to put them outside the loop if you can.
- Avoid using method invocation as termination condition in a loop, try to use temporary variable for loop termination condition.
- Avoid using exception handling in a loop.
- When using short circuit operators place the expression which is likely to evaluate to false on extreme left if the expresion contains &&.
- When using short circuit operators place the expression which is likely to evaluate to true on extreme left if the expresion contains only ||.
- Use System.arraycopy() for copying array instead of using a loop.
Some tips that i found at the mutant's musings blog and i think it's very interesting for our knowledge (specially for developers):
Assinar:
Postar comentários (Atom)
0 comentários:
Postar um comentário