### Question:
MemoryMetrics extends SystemMetrics<MemoryMetrics> { @Override public MemoryMetrics diff(@Nullable MemoryMetrics b, @Nullable MemoryMetrics output) { if (output == null) { output = new MemoryMetrics(); } if (b == null) { output.set(this); } else { MemoryMetrics latest = sequenceNumber >= b.sequenceNumber ? this : b; output.sequenceNumber = latest.sequenceNumber; output.javaHeapMaxSizeKb = latest.javaHeapMaxSizeKb; output.javaHeapAllocatedKb = latest.javaHeapAllocatedKb; output.nativeHeapSizeKb = latest.nativeHeapSizeKb; output.nativeHeapAllocatedKb = latest.nativeHeapAllocatedKb; output.vmSizeKb = latest.vmSizeKb; output.vmRssKb = latest.vmRssKb; } return output; } MemoryMetrics(); @Override MemoryMetrics set(MemoryMetrics metrics); MemoryMetrics sum(@Nullable MemoryMetrics b, @Nullable MemoryMetrics output); @Override MemoryMetrics diff(@Nullable MemoryMetrics b, @Nullable MemoryMetrics output); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); public long sequenceNumber; public long javaHeapMaxSizeKb; public long javaHeapAllocatedKb; public long nativeHeapSizeKb; public long nativeHeapAllocatedKb; public long vmSizeKb; public long vmRssKb; }### Answer:
@Override @Test public void testDiff() throws Exception { MemoryMetrics a = createMemoryMetrics(1); MemoryMetrics b = createMemoryMetrics(2); MemoryMetrics sum = new MemoryMetrics(); MemoryMetricsCollector collector = new MemoryMetricsCollector(); collector.enable(); collector.getSnapshot(a); collector.getSnapshot(b); sum = b.diff(a, sum); assertThat(sum.sequenceNumber).isEqualTo(b.sequenceNumber); assertThat(sum.javaHeapMaxSizeKb).isEqualTo(b.javaHeapMaxSizeKb); assertThat(sum.javaHeapAllocatedKb).isEqualTo(b.javaHeapAllocatedKb); assertThat(sum.nativeHeapSizeKb).isEqualTo(b.nativeHeapSizeKb); assertThat(sum.nativeHeapAllocatedKb).isEqualTo(b.nativeHeapAllocatedKb); assertThat(sum.vmSizeKb).isEqualTo(b.vmSizeKb); assertThat(sum.vmRssKb).isEqualTo(b.vmRssKb); sum = a.diff(b, null); assertThat(sum.sequenceNumber).isEqualTo(b.sequenceNumber); assertThat(sum.javaHeapMaxSizeKb).isEqualTo(b.javaHeapMaxSizeKb); assertThat(sum.javaHeapAllocatedKb).isEqualTo(b.javaHeapAllocatedKb); assertThat(sum.nativeHeapSizeKb).isEqualTo(b.nativeHeapSizeKb); assertThat(sum.nativeHeapAllocatedKb).isEqualTo(b.nativeHeapAllocatedKb); assertThat(sum.vmSizeKb).isEqualTo(b.vmSizeKb); assertThat(sum.vmRssKb).isEqualTo(b.vmRssKb); }