Monday, 3 February 2014

Why Memory leak in Java ?

Memory leaks is a common error in programming, especially when using languages that have no built-in automatic garbage collection, such as C and C++.  In these type of programming languages when you need memory then you have to allocate the memory from OS and when your task completed then you have to release the memory manually. Means developer is only responsible for memory management and it depends on once programming .
But languages like java  has there own automatic memory management. Java Virtual Machine periodically checks which objects in memory are still being used and which are not. Unused objects are removed from the memory  and the memory  is  reused again. This process is called garbage collection and the corresponding piece of JVM is called Garbage Collector, or GC.

So memory management in java is fully depended on garbage collector. But Garbage Collector will run according to it s algorithms used and developer can only request Garbage Collector . But is not necessary that it will run and will claim the memory.

Memory leaking often happens when writing small programs which use file IO. You open the file, write your data, but don't close it once you're done.