Here is an article about memory leaks, what causes them and how to resolve them.
Originally posted by ajay verma:
how can i come to know about memory is leaking or not?
can you dictate me related one with the help of program?
and what is memory leaking?
There is no simple answer here. In C/C++, a memory leak is defined as memory that has been malloc'ed (new'ed), and lost. All pointers to the memory are gone, which means that it is not possible to reclaim (free / delete) the memory. Yet, the program still thinks that it is in use.
In Java, the GC takes care of this. So it is not possible to have memory leaks due to setting references to null. However, it is possible to have memory leaks due to "forgotten" references. For example, you write a cache that is used for optimation purposes, but you forget that it uses a static hashmap somewhere. Even if you set all the references to null, this cache located in some static variable will always have a reference.
Now, if this code is part of a jar file, used by the company (and you are no longer with the company), it is highly unlikely that it will be found.
For me, I generally use a profiler to determine memory leaks. The profiler that I use provides a view into the heap -- I know what objects are in the heap, how many of them are there, and their average size. With this, you can determine which objects that are growing in the heap, which shouldn't be... this can be used as likely places to look for memory leaks.
The profiler that I use also provides views into resources, number of open sockets, open files, amount of memory used by which subsystem, etc. With this, you can tell if the system is leaking resources, which is also a sign of a memory leak.
I know the objects are put on the stack and become eligible for garbage collection after exiting the method but what if I need load large number of different objects into several different Array and perform different processing for these arrays(for loop) can I set the an array object to null immediately so its possible to be garbage collected
before loading another set of different objects into another array? or else the memory will all be used up in that method.
I know the objects are put on the stack and become eligible for garbage collection after exiting the method
When talking about objects you MUST always be careful to distinquish between references and objects. References are on the stack, the JVM is managing the objects elsewhere.
GC is perfectly capable of recognizing that there are no more valid references to an object while still inside methods and before exiting the method.
For example variables declared inside code blocks of various kinds. You can experiment with this by coding a class with a System.out.print in the finalize method.