Skip to content

Instantly share code, notes, and snippets.

@djangofan
Created May 16, 2012 20:49
Show Gist options
  • Save djangofan/2713839 to your computer and use it in GitHub Desktop.
Save djangofan/2713839 to your computer and use it in GitHub Desktop.
Memory leak generation example in Java
import java.util.HashMap;
import java.util.Map;
public class MemoryLeak {
public static void main(String[] args) {
Map<Key, String> map = new HashMap<Key, String>(1000);
int counter = 0;
while (true) {
// creates duplicate objects due to bad Key class
map.put(new Key("dummyKey"), "value");
counter++;
if (counter % 1000 == 0) {
System.out.println("Map size: " + map.size());
System.out.println("Free memory after count " + counter
+ " is " + getFreeMemory() + "MB");
sleep(1000);
}
}
}
// inner class key without hashcode() or equals()
// bad implementation causes memory leak
static class Key {
private String key;
public Key(String key) {
this.key = key;
}
}
public static void sleep(long sleepFor) {
try {
Thread.sleep(sleepFor);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static long getFreeMemory() {
return Runtime.getRuntime().freeMemory() / (1024 * 1024);
}
}
/**
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;
}
}
@Override
public int hashCode() {
return key.hashCode();
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment