Wednesday 7 August 2013

What is a thread leak? How can I trace whether the application has a thread leak?

Thread leak is when a application does not release references to a thread object properly. Due to this some Threads do not get garbage collected and the number of unused threads grow with time. Thread leak can often cause serious issues on a Java application since over a period of time too many threads will be created but not released and may cause applications to respond slow or hang.

Q:How can I trace whether the application has a thread leak?
Ans:If an application has thread leak then with time it will have too many unused threads. Try to find out what type of threads is leaking out. This can be done using following ways:

  • Give unique and descriptive names to the threads created in application. - Add log entry in all thread at various entry and exit points in threads.

  • Change debugging config levels (debug, info, error etc) and analyze log messages.

  • When you find the class that is leaking out threads check how new threads are instantiated and how they're closed.

  • Make sure the thread is Guaranteed to close properly by doing following - Handling all Exceptions properly.

  • Make sure the thread is Guaranteed to close properly by doing following

No comments: