use of com.squareup.haha.perflib.Snapshot in project leakcanary by square.
the class HeapAnalyzer method findTrackedReferences.
public List<TrackedReference> findTrackedReferences(File heapDumpFile) {
if (!heapDumpFile.exists()) {
throw new IllegalArgumentException("File does not exist: " + heapDumpFile);
}
try {
HprofBuffer buffer = new MemoryMappedFileBuffer(heapDumpFile);
HprofParser parser = new HprofParser(buffer);
Snapshot snapshot = parser.parse();
deduplicateGcRoots(snapshot);
ClassObj refClass = snapshot.findClass(KeyedWeakReference.class.getName());
List<TrackedReference> references = new ArrayList<>();
for (Instance weakRef : refClass.getInstancesList()) {
List<ClassInstance.FieldValue> values = classInstanceValues(weakRef);
String key = asString(fieldValue(values, "key"));
String name = hasField(values, "name") ? asString(fieldValue(values, "name")) : "(No name field)";
Instance instance = fieldValue(values, "referent");
if (instance != null) {
String className = getClassName(instance);
List<String> fields = describeFields(instance);
references.add(new TrackedReference(key, name, className, fields));
}
}
return references;
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
use of com.squareup.haha.perflib.Snapshot in project leakcanary by square.
the class HeapAnalyzerTest method ensureUniqueRoots.
@Test
public void ensureUniqueRoots() {
Snapshot snapshot = createSnapshot(DUP_ROOTS);
heapAnalyzer.deduplicateGcRoots(snapshot);
Collection<RootObj> uniqueRoots = snapshot.getGCRoots();
assertThat(uniqueRoots).hasSize(4);
List<Long> rootIds = new ArrayList<>();
for (RootObj root : uniqueRoots) {
rootIds.add(root.getId());
}
Collections.sort(rootIds);
// 3 appears twice because even though two RootObjs have the same id, they're different types.
assertThat(rootIds).containsExactly(3L, 3L, 5L, 6L);
}
use of com.squareup.haha.perflib.Snapshot in project leakcanary by square.
the class HahaHelperTest method setUp.
@Before
public void setUp() {
buffer = new FakeHprofBuffer();
snapshot = new Snapshot(buffer);
// set HPROF identifier size; required for Object instance field lookups
// cf. https://java.net/downloads/heap-snapshot/hprof-binary-format.html
snapshot.setIdSize(4);
}
use of com.squareup.haha.perflib.Snapshot in project leakcanary by square.
the class HeapAnalyzer method checkForLeak.
/**
* Searches the heap dump for a {@link KeyedWeakReference} instance with the corresponding key,
* and then computes the shortest strong reference path from that instance to the GC roots.
*/
public AnalysisResult checkForLeak(File heapDumpFile, String referenceKey) {
long analysisStartNanoTime = System.nanoTime();
if (!heapDumpFile.exists()) {
Exception exception = new IllegalArgumentException("File does not exist: " + heapDumpFile);
return failure(exception, since(analysisStartNanoTime));
}
try {
HprofBuffer buffer = new MemoryMappedFileBuffer(heapDumpFile);
HprofParser parser = new HprofParser(buffer);
Snapshot snapshot = parser.parse();
deduplicateGcRoots(snapshot);
Instance leakingRef = findLeakingReference(referenceKey, snapshot);
// False alarm, weak reference was cleared in between key check and heap dump.
if (leakingRef == null) {
return noLeak(since(analysisStartNanoTime));
}
return findLeakTrace(analysisStartNanoTime, snapshot, leakingRef);
} catch (Throwable e) {
return failure(e, since(analysisStartNanoTime));
}
}
Aggregations