Skip to content

Instantly share code, notes, and snippets.

@jonfung
Created August 10, 2019 20:52
Show Gist options
  • Save jonfung/e4dd0db2b5060b2c822ec74cffa609bf to your computer and use it in GitHub Desktop.
Save jonfung/e4dd0db2b5060b2c822ec74cffa609bf to your computer and use it in GitHub Desktop.
Pinterest: Upgrading Java Operational Metrics 3
// 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