Friday, March 8, 2013

Java memory leaks

Last week i visited one of my customers that had a memory problem.
His application's memory keep rising no matter how much memory they allocate for it.
Each time OutOfMemory Error has accrued the RnD advice the support to increase the memory size.
However, after few days the old OutOfMemory error happened again.
I told the RnD manager that his application suffer from memory leaks, just like C++ it's also a bug.
The RnD manager told me: "we are working in java with GC it's impossible to have memory leaks" well, in java we also may have memory leaks but it is very diffrent from C++.

In java the application will suffer from memory leaks in the following example:

 public static void main(String[] args) {
  Observable ds = new Observable();
  Main m = new Main();
  // do some work...
  // when finish work with main forget removing m from observer

so in this case even if m was removed from other data structure it's still in the heap of the program. The problem is even worse when you are working with application server since you need to understand how beans released from the memory. Such bugs are difficult to detect since you don't know which object is leaked and you have to deeply understand your program in order to clean all your code. However, most of the professional profilers provide you tools to handle memory leaks and you can use them to clean your code. In addition, there are some tools in the java language and some good best practices to avoid memory leaks. so after 1 day of workshop training that i provide for this customer there are no more questions about memory leaks and the guys start working and removed most of the memory leaks from their application. Now it's work better and the customer stop complaining about the endurance of the application.

No comments: