Created
January 20, 2018 11:46
-
-
Save vikasverma787/fd7b656b6d0ef12ae38013f308eedbf1 to your computer and use it in GitHub Desktop.
Memory Analyzer WeakHashMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
save Heap dump using jconsole : | |
Microsoft Windows [Version 6.1.7601] | |
Copyright (c) 2009 Microsoft Corporation. All rights reserved. | |
C:\Users\vikas.verma1>cd workspace\demo | |
C:\Users\vikas.verma1\workspace\demo> | |
C:\Users\vikas.verma1\workspace\demo>cd src | |
C:\Users\vikas.verma1\workspace\demo\src>jmap -dump:format=b,file=heap.bin 6016 | |
Dumping heap to C:\Users\vikas.verma1\workspace\demo\src\heap.bin ... | |
Heap dump file created | |
C:\Users\vikas.verma1\workspace\demo\src> | |
--------------------------------- | |
Here I am tried with Integer value in weakHashMap key is an large object as Employee. | |
debug.memory: allocated: 1,362.00MB of 3,616.00MB (1,204.00MB free) | |
Here Now with same object for value and key. | |
https://www.toptal.com/java/hunting-memory-leaks-in-java#memleak | |
https://developers.redhat.com/blog/2014/08/14/find-fix-memory-leaks-java-application/ | |
package com.gc.refrences; | |
import static com.jayway.awaitility.Awaitility.await; | |
import java.lang.ref.WeakReference; | |
import java.text.DecimalFormat; | |
import java.util.HashMap; | |
import java.util.WeakHashMap; | |
import java.util.concurrent.TimeUnit; | |
class Employee1 { | |
private String name; | |
private int id; | |
private Object object; | |
public Employee1(String name, int id) { | |
super(); | |
this.name = name; | |
this.id = id; | |
object = largeObj(); | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Object largeObj() { | |
StringBuilder stringB = new StringBuilder(2000000); // for the 2mb one | |
String paddingString = getName(); | |
while (stringB.length() + paddingString.length() < 2000000) | |
stringB.append(paddingString); | |
// use it | |
return stringB.toString(); | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getId() { | |
return id; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + id; | |
result = prime * result + ((name == null) ? 0 : name.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Employee1 other = (Employee1) obj; | |
if (id != other.id) | |
return false; | |
if (name == null) { | |
if (other.name != null) | |
return false; | |
} else if (!name.equals(other.name)) | |
return false; | |
return true; | |
} | |
} | |
public class MemoryAnayzer { | |
public static void main(String[] args) { | |
// Blo | |
logHeap(); | |
//HashMap<Employee, Employee> hm = new HashMap<>(); | |
//WeakHashMap<Employee1, WeakReference<Employee1>> hm = new WeakHashMap(); | |
WeakHashMap<Employee1, Integer> hm = new WeakHashMap(); | |
for (int i = 0; i < 500; i++) { | |
Employee1 e1 = new Employee1("vikas" + i, i); | |
Employee1 e2 = new Employee1("Ram" + i, 501 + i); | |
WeakReference<Employee1> reference = new WeakReference(e1); | |
WeakReference<Employee1> reference1 = new WeakReference(e2); | |
hm.put(e1, i); | |
hm.put(e2, 501+i); | |
} | |
//System.gc(); | |
logHeap(); | |
} | |
public static void logHeap() { | |
DecimalFormat df = new DecimalFormat(); | |
df.setMaximumFractionDigits(2); | |
df.setMinimumFractionDigits(2); | |
System.out.println("debug. ================================="); | |
System.out.println( | |
"debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory() / 1048576)) | |
+ "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory() / 1048576)) + "MB (" | |
+ df.format(new Double(Runtime.getRuntime().freeMemory() / 1048576)) + "MB free)"); | |
} | |
} | |
o/p ::debug.memory: allocated: 1,306.00MB of 3,616.00MB (1,023.00MB free) | |
Here Now with same object for value and key: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment