Skip to content

Instantly share code, notes, and snippets.

Jeronimo López jerolba

Block or report user

Report or block jerolba

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jerolba
jerolba / MapViews.java
Created Aug 5, 2019
Example of potential bugs using incorrectly Map API
View MapViews.java
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class MapViews {
public static void main(String[] args) {
mapChangeModifiesValuesSet();
mapChangeModifiesKeysSet();
View TableBikeySetSample.java
//BikeySet<Avenger, Film> avengerFilms = new TableBikeySet<>();
BikeySet<String, String> avengerFilms = new TableBikeySet<>();
avengerFilms.add("Hulk", "The Avengers");
avengerFilms.add("Iron Man", "The Avengers");
avengerFilms.add("Thor", "Avengers: Age of Ultron");
avengerFilms.add("Thor", "Thor: Ragnarok");
avengerFilms.add("Captain America", "Avengers: Infinity War");
....
if (avengerFilms.contains("Iron Man", "Black Panther")) {
View TableBikeyMapSample.java
//Given the stock of some products and stores
//BikeyMap<Product, Store, Stock> stock = new TableBikeyMap<>();
BikeyMap<String, String, Integer> stock = new TableBikeyMap<>();
stock.put("shirt-ref-123", "store-76", 10);
stock.put("pants-ref-456", "store-12", 24);
...
stock.put("tie-ref-789", "store-23", 2);
//Get the stock of a product/store
Integer inStock = stock.get("shirt-ref-1234", "store-45");
View MemoryConsumtionSimple.java
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
MemoryUsage beforeHeapMemoryUsage = mbean.getHeapMemoryUsage();
Object instance = codeThatCreatesComplexDataStructure();
MemoryUsage afterHeapMemoryUsage = mbean.getHeapMemoryUsage();
long consumed = afterHeapMemoryUsage.getUsed() -
beforeHeapMemoryUsage.getUsed();
System.out.println("Total consumed Memory:" + consumed);
View MemoryHistogramWarm.java
methodThatDoesThings();
MemoryHistogram diff = Histogramer.getDiff(() -> {
return methodThatDoesThings();
});
System.out.println(diff);
View MemoryHistogramFilter.java
MemoryHistogram filterd = diff.filter("Object[]", "java.util.HashMap*",
ArrayList.class, Pattern.compile(".*Hibernate.*"));
View LambdaHistogramScape.java
MemoryHistogram diff = Histogramer.getDiff(() -> {
HashMap<Integer, String> map = new HashMap<>();
for (int i = 0; i < 10000; i++) {
map.put(i, "" + i);
}
return new ArrayList<>(map.values());
});
System.out.println(diff);
View LambdaHistogram.java
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");
View SimpleHistogram.java
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);
}
View ManualHistogram.java
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);
}
}
You can’t perform that action at this time.