In Real Applications, how do you know that you have a Memory Leak

If you profile your application, you can notice a graph like a saw tooth. Here is how you can determine this with the help of jconsole for the above bad key class example. All you have to do is while your MemoryLeak is running, get the Java process id by typing. C:\>jps 5808 Jps 4568 … Read more

How will you Fix the Above Memory Leak

By providing proper implentation for the key class as shown below with the equals() and hashCode() methods.   ….. static class Key { private String key;   public Key(String key) { this.key = key; }     @Override public boolean equals(Object obj) {   if (obj instanceof Key) return key.equals(((Key) obj).key); else return false;   … Read more

How will you go About Creating a Memory Leak in Java

In Java, memory leaks are possible under a number of scenarios. Here is a typical example where hashCode( ) and equals( ) methods are not implemented for the Key class that is used to store key/value pairs in a HashMap. This will end up creating a large number of duplicate objects. All memory leaks in … Read more