### Question:
MemoryMetricsCollector extends SystemMetricsCollector<MemoryMetrics> { @Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(MemoryMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); if (!mIsEnabled) { return false; } snapshot.sequenceNumber = mCounter.incrementAndGet(); snapshot.javaHeapMaxSizeKb = getRuntimeMaxMemory() / KB; snapshot.javaHeapAllocatedKb = (getRuntimeTotalMemory() - getRuntimeFreeMemory()) / KB; snapshot.nativeHeapSizeKb = Debug.getNativeHeapSize() / KB; snapshot.nativeHeapAllocatedKb = Debug.getNativeHeapAllocatedSize() / KB; snapshot.vmSizeKb = -1; snapshot.vmRssKb = -1; try { ProcFileReader reader = mProcFileReader.get(); if (reader == null) { reader = new ProcFileReader(getPath()); mProcFileReader.set(reader); } reader.reset(); if (reader.isValid()) { snapshot.vmSizeKb = readField(reader); snapshot.vmRssKb = readField(reader); } } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse memory (statm) field", pe); } return true; }  @Override @ThreadSafe(enableChecks = false) synchronized boolean getSnapshot(MemoryMetrics snapshot); synchronized void enable(); @Override MemoryMetrics createMetrics();  }### Answer:
@Test public void testBrokenFile() throws Exception { MemoryMetricsCollectorWithProcFile collector = new MemoryMetricsCollectorWithProcFile() .setPath(createFile("I am a weird android manufacturer")); MemoryMetrics snapshot = new MemoryMetrics(); collector.getSnapshot(snapshot); assertThat(collector.getSnapshot(snapshot)).isTrue(); assertThat(snapshot.vmSizeKb).isEqualTo(-1); assertThat(snapshot.vmRssKb).isEqualTo(-1); }
@Test public void testVirtualMemory() throws Exception { String statm = "4 2 0 4 0 0 0"; MemoryMetricsCollectorWithProcFile collector = new MemoryMetricsCollectorWithProcFile().setPath(createFile(statm)); MemoryMetrics snapshot = new MemoryMetrics(); assertThat(collector.getSnapshot(snapshot)).isTrue(); assertThat(snapshot.vmSizeKb).isEqualTo(16); assertThat(snapshot.vmRssKb).isEqualTo(8); }
@Test public void testDefaultDisabled() { ShadowDebug.setNativeHeapSize(4 * 1024); ShadowDebug.setNativeHeapAllocatedSize(3 * 1024); MemoryMetrics snapshot = new MemoryMetrics(); MemoryMetricsCollector collector = new MemoryMetricsCollector(); collector.getSnapshot(snapshot); assertThat(collector.getSnapshot(snapshot)).isFalse(); assertThat(snapshot.nativeHeapSizeKb).isEqualTo(0); assertThat(snapshot.nativeHeapAllocatedKb).isEqualTo(0); }