Thursday, January 13, 2011

Java : Best Practices for avoiding memory leaks in Java

Hope you find this post useful with the TOP-20 memory leak prevention steps.

1.Use logging frameworks like Log4J which uses I/O buffers instead of System.out.println.
2.Set the initial capacity of a collection.Empty vector has array size 10.
3. Collection classes, such as hash tables and vectors, are common places to find the cause of a memory leak.
4.Use ArrayLists, HashMap instead of Vector,Hashtable etc wherever possible.
5. Unregister when the class is no longer needed. Otherwise it will exist until the application close.
6.Use Thread pooling and database connection pooling techniques wherever possible.
7.In case of I/O operations use buffering when writing to and reading from
files and/or streams.Avoid reader/writers use streams instead of this.
8. Integrate your java application with JProfiler so that you can check which class or method is taking more memory.
9. If you are application using database ensure that tables are not containing unwanted data [historical data].
   otherwise it will load every time when application starts and collection classes’ size will increase.
10. Application design is also an important factor for memory leak. If possible do changes in design level.
11. Use mutable StringBuffer/StringBuilder classes instead of immutable String objects.
12.Remove references  to the short-lived objects from long-lived objects like Java collections.
13.Create objects before entering the loop and avoid creating new objects for each iteration.

14.Reduce calls to Date, Calendar, etc related classes.
15.Use tools like JProfiler,JProbe,OptimizeIt to find memory leaks in your java application.


When working with Swings,windows and Threads
-----------------------------------------------------------------

Java talks to the underlying GUI which is typically written in C++ which does not have automatic garbage collection.
If you fail to use dispose method properly, Java won’t run out of RAM, but the GUI will.
16. Failing to use dispose method create true memory leaks.
17. Threads are huge objects. Make sure you nullify pointers to them after you no longer need them.
   Make sure you properly terminate threads or you will fill up RAM with moribund Threads.
18. Don’t create Threads until you are ready to start them.
19. In Swing make sure you use JFrame. setDefaultCloseOperation ( JFrame. DISPOSE_ON_CLOSE );
    instead of the default JFrame.setDefaultCloseOperation ( JFrame.HIDE_ON_CLOSE );
otherwise, when a user closes a window, the frames are just hidden. You don’t recover the resources.
20. Use JFrame. setDefaultCloseOperation ( JFrame. DO_NOTHING_ON_CLOSE ); when you have a WindowListener. windowClosing method.

21. To permanently close a frame :
frame.setVisible( false );
frame.dispose();
frame = null;

2 comments:

  1. Perhaps the best collection I have seen. Crisp and clear.

    ReplyDelete
  2. usefull information. Fastthread is a usefull tool which analyse different states of thread in detail.I hope this may help you people too. Capture thread dumps

    ReplyDelete

subversion video