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.
Informative article. Im very happy to visit this page. keep it up and Good job.
ReplyDeletejavascript training in chennai
javascript training in Velachery
core java training in chennai
core java course
core java classes
C C++ Training in Chennai
Appium Training in Chennai
JMeter Training in Chennai
This stays one of the best and attractive post I have read yet. Great sharing this post.
ReplyDeleteSpoken English Classes in Chennai
Best Spoken English Classes in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Coaching in Anna Nagar
Spoken English Class in Anna Nagar
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
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
liposuction cost Korea Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.
ReplyDelete