How to track "memory leak" and how to prevent it in java?

Showing Answers 1 - 2 of 2 Answers

pratap

  • May 25th, 2006
 

You may use "JProbe Memory debugger" tool to identify memory leaks in your code.

Thanks,

Pratap

  Was this answer useful?  Yes

satishm

  • May 26th, 2006
 

To reduce possibility of memory leaks, following things should be kept in mind.

Try to avoid long recursive loops in the code. This leads to an OutOfMemoryError.

Avoid collections as static members of a class. If so, carefully manage removals and additions to the Collection. If the collection continues adding members without removing them, it might lead to a leak. Static members persist throughout the life of the program. A static collection will keep references to objects, whether you are using it or not. So the garbage collector might miss collecting the unused contents of the collection.

Avoid populating the HttpSession with too much data. Session persists in the system as long as the user is online, and it might get filled up fast. Additions and removals in the session should be checked.

If you are using AWT/Swing components, call dispose() to kill the windowing component completely.

Check your native code for memory leaks, if you are calling C/C++ code from Java using JNI.

There are other things too, to take care of, but these are the most important ones.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions