Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does not implementing equals method in Java cause memory leak

I am trying to understand various reasons for memory leak one of the samples i saw where hashCode() was implemented and not equals(). I have read through that one if one is over ridden the other also has to be over ridden because of contract violation.

this is the sample code

import java.util.HashMap;
import java.util.Map;

public class MemoryLeak {

static class Key { 
    Integer id; 

    Key(Integer id) { 
        this.id = id; 
    } 

    @Override 
    public int hashCode() { 
        return id.hashCode(); 
    }   
} 
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Map m = new HashMap(); 
    while (true) 
        for (int i = 0; i < 10000; i++) 
            if (!m.containsKey(i)) 
                m.put(new Key(i), "Number:" + i); 
}
}

I know i have not implemented the equals() method on purpose. But i want to under stand why is memory leak is created what is going on internally.

thanks

like image 448
kumar Avatar asked Oct 09 '13 12:10

kumar


People also ask

What causes memory leaks in Java?

In general, a Java memory leak happens when an application unintentionally (due to logical errors in code) holds on to object references that are no longer required. These unintentional object references prevent the built-in Java garbage collection mechanism from freeing up the memory consumed by these objects.

What is the main cause of memory leaks in application?

Holding the references of the object and resources that are no longer needed is the main cause of the memory leaks in android applications. As it is known that the memory for the particular object is allocated within the heap and the object point to certain resources using some object reference.

Can a Java program exposes a memory leak?

A small Java application might have a memory leak, but it will not matter if the JVM has enough memory to run your program. However, if your Java application runs constantly, then memory leaks will be a problem. This is because a continuously running program will eventually run out of memory resources.

Will this situation causes a memory leak in Java Class A?

The first scenario that might cause a Java memory leak is referencing a heavy object with a static field. We created our ArrayList as a static field – which will never be collected by the JVM Garbage Collector during the lifetime of the JVM process, even after the calculations it was used for are done.


1 Answers

If you don't implement Key#equals(), no two Key instances will be equal, so Map#containsKey() will always return false. Additionally, you're checking containsKey(i), but not using i as a key. Even if you did implement Key#equals(), that containsKey check is effectively if(true).

Consequently, this code unconditionally adds logically-distinct entries to the map, so its size grows without bound.

like image 142
Matt Ball Avatar answered Oct 31 '22 10:10

Matt Ball