Created
August 10, 2019 20:52
-
-
Save jonfung/e4dd0db2b5060b2c822ec74cffa609bf to your computer and use it in GitHub Desktop.
Pinterest: Upgrading Java Operational Metrics 3
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
// Unoptimized | |
// list will not be garbage collected | |
// cumbersome instantiation | |
List<Integer> list = someList(); | |
Stats.addGauge("metric_name", new Function0<Object>() { | |
List<Integer> listRef = list; | |
@Override | |
public Double apply() { | |
return (double) listRef.size(); | |
} | |
}); | |
// Optimized | |
// Weak reference to the list is maintained | |
// list is returned from the gauge call, removes need for anonymous class | |
List<Object> list = Stats.gauge("metric_name", new ArrayList<>, l -> l.size()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment