use of com.squareup.haha.perflib.io.HprofBuffer 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.io.HprofBuffer 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