Simple examples of memory leaks in java

Giving examples of memory leaks in Java is one the most frequent questions in job interviews. Below is a huge list of simple memory leaks examples, some are collected from my previous class notes, some are collected from the internet.







Example 1: Unclosed connections
 
try {
    Connection connection  = ConnectionFactory.getConnection();
    //Code your stuff
} catch (Exception e) {
    //Print the exception
}
Example 2: Unclosed streams
 
try {
    //Have an input file "inputFile"
    Filereader filereader = new FileReader(inputFile);
    BufferedReader bufferedreader = new BufferedReader(filereader);
    //Code you stuff
} catch (Exception e) {
    //Print the exception

}
Example 3: Unreachable code from JVM garbage collector
 This doesn't need any code to explain, any memory created through native methods could work. 
Example 4: The implementation of ArrayList.remove()
 public E remove(int index) {
    RangeCheck(index);

    modCount++;
    E oldValue = (E) elementData[index];

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index + 1, elementData, index,
                numMoved);
    elementData[--size] = null;

    return oldValue;
}
Example 5: A cool example from Eclipse.
public class StringLeaker
{
    private final String muchSmallerString;

    public StringLeaker()
    {
        // Imagine the whole Declaration of Independence here
        String veryLongString = "We hold these truths to be self-evident...";

        // The substring here maintains a reference to the internal char[]
        // representation of the original string.
        this.muchSmallerString = veryLongString.substring(0, 1);
    }
}
Example 6: Using the java method intern() on a large java String
String str="A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string. A very large string."

//Calling intern
str.intern();
Example 7: Using a final static to hold an object reference!
class JavaProblemsClass {
    static final ArrayList arrayList = new ArrayList (959);
}
Example 8: Using a HashSet with an incorrect hashCode() or equals() [src]
class BadKey {
   // no hashCode or equals();
   public final String key;
   public BadKey(String key) { this.key = key; }
}

Map map = System.getProperties();
map.put(new BadKey("key"), "value"); // Memory leak even if your threads die.


2 comments :

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact