What is meant by Memory Leak?
The memory leak is a condition in which the program continues to hold on to the allocated memory even after the program no longer needs it and it just lies there i.e. inaccessible but still stored in the memory. Now, you can get memory leaks for java by manual memory management via JNI and thus have memory leaks, or by having bugs in JVM may also lead to memory leak.
Now, there can be many ways in which you may achieve memory leak ( from two ways listed above), few of them are :-
- Field holding object reference
class Darthvader {
static final ArrayList arraylist = new ArrayList(100);
}
2. Unclosed connections
try {
Connection connect = ConnectionFactory.getConnection();
…
} catch (Exception s) {
s.printStacktrace();
}
3.Open streams
try {
BufferedReader buffer = new BufferedReader(new FileReader(inputFile));
…
} catch (Exception s) {
s.printStacktrace();
}