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.


13 comments :

  1. I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. Feeling so glad about your article. Thanks…!
    magento training course in chennai
    magento training institute in chennai
    magento 2 training in chennai
    magento development training
    magento 2 course
    magento developer training

    ReplyDelete
  2. Such a wonderful article and I feel that it is best to write more on this topic. Thank you so much because i learn a lot of ideas about it. Keep posting...
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata
    SEO Course In Kolkata

    ReplyDelete
  3. Informative post indeed, I’ve being in and out reading posts regularly and I see alot of engaging people sharing things and majority of the shared information is very valuable and so, here’s my fine read. give more detais
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  4. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete

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