Skip to content

Instantly share code, notes, and snippets.

View jerolba's full-sized avatar

Jeronimo López jerolba

View GitHub Profile
MemoryHistogram diff = Histogramer.getDiff(() -> {
HashMap<Integer, String> map = new HashMap<>();
for (int i = 0; i < 10000; i++) {
map.put(i, "" + i);
}
return map;
});
HistogramEntry nodes = diff.get("java.util.HashMap$Node");
Histogramer histogramer = new Histogramer();
MemoryHistogram histogram = histogramer.createHistogram();
HistogramEntry arrayList = histogram.get("java.util.ArrayList");
System.out.println(arrayList.getInstances());
System.out.println(arrayList.getSize());
for (HistogramEntry entry : histogram) {
System.out.println(entry);
}
public void memoryHistogram() {
String name = ManagementFactory.getRuntimeMXBean().getName();
String PID = name.substring(0, name.indexOf("@"));
Process p = Runtime.getRuntime().exec("jcmd " + PID + " GC.class_histogram");
try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
input.lines().forEach(System.out::println);
}
}
+-------------+-----------+------------+--------------+---------------+---------------+
| Nº Products | Nº Stores | Total | TupleMap(ms) | Tuple new(ms) | DoubleMap(ms) |
+-------------+-----------+------------+--------------+---------------+---------------+
| 1,000 | 100 | 100,000 | 12.08 | 12.55 | 3.81 |
| 2,500 | 100 | 250,000 | 37.11 | 38.21 | 14.35 |
| 5,000 | 100 | 500,000 | 77.40 | 77.39 | 46.84 |
| 7,500 | 100 | 750,000 | 125.96 | 126.35 | 68.53 |
| 10,000 | 100 | 1,000,000 | 148.55 | 141.47 | 163.37 |
| 1,000 | 250 | 250,000 | 48.96 | 50.41 | 18.30 |
| 2,500 | 250 | 625,000 | 140.25 | 144.70 | 70.04 |
+-------------+-----------+------------+--------------+---------------+
| Nº Products | Nº Stores | Total | TupleMap(ms) | DoubleMap(ms) |
+-------------+-----------+------------+--------------+---------------+
| 1,000 | 100 | 100,000 | 33.1 | 5.61 |
| 2,500 | 100 | 250,000 | 103.38 | 24.78 |
| 5,000 | 100 | 500,000 | 177.71 | 116.43 |
| 7,500 | 100 | 750,000 | 272.02 | 160.95 |
| 10,000 | 100 | 1,000,000 | 314.94 | 241.86 |
| 1,000 | 250 | 250,000 | 129.18 | 19.82 |
| 2,500 | 250 | 625,000 | 292.97 | 114.85 |
+-----------------------------+-----------+-------------+
| Class | instances | size |
+-----------------------------+-----------+-------------+
| java.util.HashMap$Node | 5.010.000 | 160.320.000 |
| java.util.HashMap$Node[] | 10.001 | 41.185.552 |
| java.util.HashMap | 10.001 | 480.048 |
| com.jerolba.bikey.DoubleMap | 1 | 16 |
+-----------------------------+-----------+-------------+
+----------------------------+-----------+-------------+
| Class | instances | size |
+----------------------------+-----------+-------------+
| java.util.HashMap$Node | 5.000.000 | 160.000.000 |
| com.jerolba.bikey.Tuple | 5.000.000 | 120.000.000 |
| java.util.HashMap$Node[] | 1 | 33.554.448 |
| java.util.HashMap | 1 | 48 |
| com.jerolba.bikey.TupleMap | 1 | 16 |
+----------------------------+-----------+-------------+
public class MyObjectKey {
public int hashCode() {
return (firstKey << 16) + secondKey;
}
}
@jerolba
jerolba / HashCodeHistogram.java
Created February 7, 2019 20:11
HashCode value histogram calculation
for (int i = 0; i < n_products; i++) {
for (int j = 0; j < n_stores; j++) {
int hashValue = HashMap.hash(Objects.hash(i, j));
histogram.add(hashValue);
}
}
@jerolba
jerolba / HashMapHashFunction.java
Created February 7, 2019 20:10
HashMap hash function implementation
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}