didi/DoKit · error · IllegalStateException

Could not find the {} class in the heap dump.

Error message

Could not find the {} class in the heap dump.

What it means

During leak analysis, HeapAnalyzer.findLeakingReference() looks up the com.squareup.leakcanary.KeyedWeakReference class in the parsed snapshot. If the class is absent, no references were ever watched with this LeakCanary instrumentation, so analysis cannot proceed and it throws IllegalStateException.

Source

Thrown at Android/dokit-leakcanary/src/main/java/com/squareup/leakcanary/HeapAnalyzer.java:227

        // Repopulate snapshot with unique GC roots.
        gcRoots.clear();
        uniqueRootMap.forEach(new TObjectProcedure<String>() {
            @Override
            public boolean execute(String key) {
                return gcRoots.add(uniqueRootMap.get(key));
            }
        });
    }

    private String generateRootKey(RootObj root) {
        return String.format("%s@0x%08x", root.getRootType().getName(), root.getId());
    }

    private Instance findLeakingReference(String key, Snapshot snapshot) {
        ClassObj refClass = snapshot.findClass(KeyedWeakReference.class.getName());
        if (refClass == null) {
            throw new IllegalStateException(
                    "Could not find the " + KeyedWeakReference.class.getName() + " class in the heap dump.");
        }
        List<String> keysFound = new ArrayList<>();
        for (Instance instance : refClass.getInstancesList()) {
            List<ClassInstance.FieldValue> values = HahaHelper.classInstanceValues(instance);
            Object keyFieldValue = HahaHelper.fieldValue(values, "key");
            if (keyFieldValue == null) {
                keysFound.add(null);
                continue;
            }
            String keyCandidate = HahaHelper.asString(keyFieldValue);
            if (keyCandidate.equals(key)) {
                return HahaHelper.fieldValue(values, "referent");
            }
            keysFound.add(keyCandidate);
        }
        throw new IllegalStateException(
                "Could not find weak reference with key " + key + " in " + keysFound);

View on GitHub (pinned to 626827cddb)

Solutions

  1. Only analyze heap dumps captured by the same LeakCanary installation that watched the references
  2. Verify the watcher is installed (LeakCanary.installedRefWatcher() != null) before triggering analysis
  3. Add ProGuard keep rules for com.squareup.leakcanary.** so KeyedWeakReference survives obfuscation
  4. For arbitrary hprof inspection, use a real heap tool (MAT, Android Studio) instead of LeakCanary's analyzer

Example fix

// before
AnalysisResult result = heapAnalyzer.checkForLeak(arbitraryHprofFile, referenceKey);

// after
// only analyze dumps produced by LeakCanary's own HeapDumper
if (!heapDumpFile.getName().contains("leakcanary")) {
  // not a LeakCanary dump; use MAT instead
}
Defensive patterns

Strategy: validation

Validate before calling

if (LeakCanary.installedRefWatcher() != null && dumpWasProducedByLeakCanary(heapDumpFile)) { heapAnalyzer.checkForLeak(heapDumpFile, key); }

Try / catch

try { result = heapAnalyzer.checkForLeak(file, key); } catch (IllegalStateException e) { // not a LeakCanary-captured dump or watcher never installed; use MAT instead }

Prevention

When it happens

Trigger: Calling the analyze path (checkForLeak / findLeakingReference) with a heap dump that was NOT captured by this LeakCanary version — e.g. a manually captured dump (am dumpheap), a dump from an app where the RefWatcher never watched anything (buildAndInstall not called, watcher disabled), or a dump from an incompatible LeakCanary version whose class name differs.

Common situations: Feeding arbitrary .hprof files (from Android Studio profiler or bug reports) into LeakCanary's analyzer. Analyzing a dump from a process where leak watching was disabled. ProGuard/R8 renaming the KeyedWeakReference class so snapshot.findClass misses it.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/18d5bc4a14ada025. Report an issue: GitHub.