Search in sources :

Example 11 with Instance

use of com.squareup.haha.perflib.Instance in project leakcanary by square.

the class ShortestPathFinder method visitClassObj.

private void visitClassObj(LeakNode node) {
    ClassObj classObj = (ClassObj) node.instance;
    Map<String, Exclusion> ignoredStaticFields = excludedRefs.staticFieldNameByClassName.get(classObj.getClassName());
    for (Map.Entry<Field, Object> entry : classObj.getStaticFieldValues().entrySet()) {
        Field field = entry.getKey();
        if (field.getType() != Type.OBJECT) {
            continue;
        }
        String fieldName = field.getName();
        if (fieldName.equals("$staticOverhead")) {
            continue;
        }
        Instance child = (Instance) entry.getValue();
        boolean visit = true;
        if (ignoredStaticFields != null) {
            Exclusion params = ignoredStaticFields.get(fieldName);
            if (params != null) {
                visit = false;
                if (!params.alwaysExclude) {
                    enqueue(params, node, child, fieldName, STATIC_FIELD);
                }
            }
        }
        if (visit) {
            enqueue(null, node, child, fieldName, STATIC_FIELD);
        }
    }
}
Also used : ClassObj(com.squareup.haha.perflib.ClassObj) Field(com.squareup.haha.perflib.Field) Instance(com.squareup.haha.perflib.Instance) ClassInstance(com.squareup.haha.perflib.ClassInstance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with Instance

use of com.squareup.haha.perflib.Instance in project leakcanary by square.

the class HahaHelper method asString.

static String asString(Object stringObject) {
    Instance instance = (Instance) stringObject;
    List<ClassInstance.FieldValue> values = classInstanceValues(instance);
    Integer count = fieldValue(values, "count");
    Object value = fieldValue(values, "value");
    Integer offset;
    ArrayInstance charArray;
    if (isCharArray(value)) {
        charArray = (ArrayInstance) value;
        offset = 0;
        // https://android-review.googlesource.com/#/c/83611/
        if (hasField(values, "offset")) {
            offset = fieldValue(values, "offset");
        }
    } else {
        // In M preview 2, the underlying char buffer resides in the heap with ID equaling the
        // String's ID + 16.
        // https://android-review.googlesource.com/#/c/160380/2/android/src/com/android/tools/idea/
        // editors/hprof/descriptors/InstanceFieldDescriptorImpl.java
        // This workaround is only needed for M preview 2, as it has been fixed on the hprof
        // generation end by reintroducing a virtual "value" variable.
        // https://android.googlesource.com/platform/art/+/master/runtime/hprof/hprof.cc#1242
        Heap heap = instance.getHeap();
        Instance inlineInstance = heap.getInstance(instance.getId() + 16);
        if (isCharArray(inlineInstance)) {
            charArray = (ArrayInstance) inlineInstance;
            offset = 0;
        } else {
            throw new UnsupportedOperationException("Could not find char array in " + instance);
        }
    }
    checkNotNull(count, "count");
    checkNotNull(charArray, "charArray");
    checkNotNull(offset, "offset");
    if (count == 0) {
        return "";
    }
    char[] chars = charArray.asCharArray(offset, count);
    return new String(chars);
}
Also used : Instance(com.squareup.haha.perflib.Instance) ClassInstance(com.squareup.haha.perflib.ClassInstance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) Heap(com.squareup.haha.perflib.Heap)

Example 13 with Instance

use of com.squareup.haha.perflib.Instance 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)13 ClassInstance (com.squareup.haha.perflib.ClassInstance)13 Instance (com.squareup.haha.perflib.Instance)13 ClassObj (com.squareup.haha.perflib.ClassObj)6 RootObj (com.squareup.haha.perflib.RootObj)4 HahaHelper.asString (com.squareup.leakcanary.HahaHelper.asString)4 HahaHelper.fieldToString (com.squareup.leakcanary.HahaHelper.fieldToString)4 Field (com.squareup.haha.perflib.Field)2 HprofParser (com.squareup.haha.perflib.HprofParser)2 Snapshot (com.squareup.haha.perflib.Snapshot)2 HprofBuffer (com.squareup.haha.perflib.io.HprofBuffer)2 MemoryMappedFileBuffer (com.squareup.haha.perflib.io.MemoryMappedFileBuffer)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Heap (com.squareup.haha.perflib.Heap)1 RootType (com.squareup.haha.perflib.RootType)1 Type (com.squareup.haha.perflib.Type)1 Map (java.util.Map)1