Search in sources :

Example 1 with Snapshot

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);
    }
}
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 Snapshot

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);
}
Also used : Snapshot(com.squareup.haha.perflib.Snapshot) ArrayList(java.util.ArrayList) RootObj(com.squareup.haha.perflib.RootObj) Test(org.junit.Test)

Example 3 with Snapshot

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);
}
Also used : Snapshot(com.squareup.haha.perflib.Snapshot) Before(org.junit.Before)

Example 4 with Snapshot

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));
    }
}
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

Snapshot (com.squareup.haha.perflib.Snapshot)4 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 HprofBuffer (com.squareup.haha.perflib.io.HprofBuffer)2 MemoryMappedFileBuffer (com.squareup.haha.perflib.io.MemoryMappedFileBuffer)2 ArrayList (java.util.ArrayList)2 ClassObj (com.squareup.haha.perflib.ClassObj)1 RootObj (com.squareup.haha.perflib.RootObj)1 HahaHelper.asString (com.squareup.leakcanary.HahaHelper.asString)1 HahaHelper.fieldToString (com.squareup.leakcanary.HahaHelper.fieldToString)1 Before (org.junit.Before)1 Test (org.junit.Test)1