Finding memory Leak:
-------------------
1) remove references to the short-lived objects from long-lived objects like Java collections
2) reuse objects where possible. It is cheaper to recycle objects than creating new objects each time
3) use mutable StringBuffer/StringBuilder classes instead of immutable String objects in computation expensive loops
4) static factory methods
5) Creating and destroying objects occupies a
significant chunk of the JVM's time. Wherever possible, you should look for ways to minimize the number of
objects created in your code
6) Use ArrayLists, HashMap etc as opposed to Vector, Hashtable etc where possible. This is because the
methods in ArrayList, HashMap etc are not synchronized . Even better is to
use just arrays where possible.
7) Set the initial capacity of a collection (e.g. ArrayList, HashMap etc) and StringBuffer/StringBuilder
appropriately. This is because these classes must grow periodically to accommodate new elements. So,
if you have a very large ArrayList or a StringBuffer, and you know the size in advance then you can speed
things up by setting the initial size appropriately
8) Minimize the use of casting or runtime type checking like instanceof in frequently executed methods or
in loops. The “casting” and “instanceof” checks for a class marked as final will be faster. Using
“instanceof” construct is not only ugly but also unmaintainable.
9) Do not compute constants inside a large loop. Compute them outside the loop. For applets compute it in
the init() method. Avoid nested loops (i.e. a “for” loop within another “for” loop etc) where applicable and
make use of a Collection class as discussed
10) Avoid using System.out.println and use logging frameworks like Log4J etc, which uses I/O buffers
No comments:
Post a Comment