Search in sources :

Example 1 with HprofParser

use of com.squareup.haha.perflib.HprofParser 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);
    }
}
Also used : ClassObj(com.squareup.haha.perflib.ClassObj) Instance(com.squareup.haha.perflib.Instance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) ClassInstance(com.squareup.haha.perflib.ClassInstance) ArrayList(java.util.ArrayList) HprofBuffer(com.squareup.haha.perflib.io.HprofBuffer) HahaHelper.fieldToString(com.squareup.leakcanary.HahaHelper.fieldToString) HahaHelper.asString(com.squareup.leakcanary.HahaHelper.asString) MemoryMappedFileBuffer(com.squareup.haha.perflib.io.MemoryMappedFileBuffer) Snapshot(com.squareup.haha.perflib.Snapshot) HprofParser(com.squareup.haha.perflib.HprofParser)

Example 2 with HprofParser

use of com.squareup.haha.perflib.HprofParser 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));
    }
}
Also used : MemoryMappedFileBuffer(com.squareup.haha.perflib.io.MemoryMappedFileBuffer) Snapshot(com.squareup.haha.perflib.Snapshot) Instance(com.squareup.haha.perflib.Instance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) ClassInstance(com.squareup.haha.perflib.ClassInstance) HprofBuffer(com.squareup.haha.perflib.io.HprofBuffer) HprofParser(com.squareup.haha.perflib.HprofParser)

Aggregations

ArrayInstance (com.squareup.haha.perflib.ArrayInstance)2 ClassInstance (com.squareup.haha.perflib.ClassInstance)2 HprofParser (com.squareup.haha.perflib.HprofParser)2 Instance (com.squareup.haha.perflib.Instance)2 Snapshot (com.squareup.haha.perflib.Snapshot)2 HprofBuffer (com.squareup.haha.perflib.io.HprofBuffer)2 MemoryMappedFileBuffer (com.squareup.haha.perflib.io.MemoryMappedFileBuffer)2 ClassObj (com.squareup.haha.perflib.ClassObj)1 HahaHelper.asString (com.squareup.leakcanary.HahaHelper.asString)1 HahaHelper.fieldToString (com.squareup.leakcanary.HahaHelper.fieldToString)1 ArrayList (java.util.ArrayList)1