|
|
|
@ -40,65 +40,38 @@ public class CoalMine {
|
|
|
|
|
static void setup(boolean enabled) {
|
|
|
|
|
List<ObjectInspector> inspectors = new ArrayList<>(LeakCanary.getConfig().getObjectInspectors());
|
|
|
|
|
|
|
|
|
|
inspectors.add(new ObjectInspector() {
|
|
|
|
|
@Override
|
|
|
|
|
public void inspect(@NonNull ObjectReporter reporter) {
|
|
|
|
|
String clazz = SimpleTask.class.getName();
|
|
|
|
|
reporter.whenInstanceOf(clazz, new Function2<ObjectReporter, HeapObject.HeapInstance, Unit>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Unit invoke(ObjectReporter reporter, HeapObject.HeapInstance instance) {
|
|
|
|
|
HeapField fname = instance.get(clazz, "name");
|
|
|
|
|
if (fname != null) {
|
|
|
|
|
String name = fname.getValue().readAsJavaString();
|
|
|
|
|
reporter.getNotLeakingReasons().add("name=" + name);
|
|
|
|
|
// https://square.github.io/leakcanary/recipes/#identifying-leaking-objects-and-labeling-objects
|
|
|
|
|
for (Class<?> clazz : new Class<?>[]{SimpleTask.class, TwoStateOwner.class, RunnableEx.class})
|
|
|
|
|
inspectors.add(new ObjectInspector() {
|
|
|
|
|
@Override
|
|
|
|
|
public void inspect(@NonNull ObjectReporter reporter) {
|
|
|
|
|
String className = clazz.getName();
|
|
|
|
|
reporter.whenInstanceOf(className, new Function2<ObjectReporter, HeapObject.HeapInstance, Unit>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Unit invoke(ObjectReporter reporter, HeapObject.HeapInstance instance) {
|
|
|
|
|
HeapField hfName = instance.get(className, "name");
|
|
|
|
|
if (hfName != null) {
|
|
|
|
|
String label = hfName.getValue().readAsJavaString();
|
|
|
|
|
reporter.getLabels().add("name=" + label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Could be different class loader
|
|
|
|
|
if (className.equals(SimpleTask.class.getName())) {
|
|
|
|
|
HeapField hfStarted = instance.get(className, "started");
|
|
|
|
|
if (hfStarted != null) {
|
|
|
|
|
Long started = hfStarted.getValue().getAsLong();
|
|
|
|
|
if (started != null) {
|
|
|
|
|
String label = (started == 0 ? null : new Date(started).toString());
|
|
|
|
|
reporter.getLabels().add("started=" + label);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
HeapField fstarted = instance.get(clazz, "started");
|
|
|
|
|
if (fstarted != null) {
|
|
|
|
|
Long started = fstarted.getValue().getAsLong();
|
|
|
|
|
if (started != null)
|
|
|
|
|
reporter.getNotLeakingReasons().add("started=" + new Date(started));
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
inspectors.add(new ObjectInspector() {
|
|
|
|
|
@Override
|
|
|
|
|
public void inspect(@NonNull ObjectReporter reporter) {
|
|
|
|
|
String clazz = TwoStateOwner.class.getName();
|
|
|
|
|
reporter.whenInstanceOf(clazz, new Function2<ObjectReporter, HeapObject.HeapInstance, Unit>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Unit invoke(ObjectReporter reporter, HeapObject.HeapInstance instance) {
|
|
|
|
|
HeapField fname = instance.get(clazz, "name");
|
|
|
|
|
if (fname != null) {
|
|
|
|
|
String name = fname.getValue().readAsJavaString();
|
|
|
|
|
reporter.getNotLeakingReasons().add("name=" + name);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
inspectors.add(new ObjectInspector() {
|
|
|
|
|
@Override
|
|
|
|
|
public void inspect(@NonNull ObjectReporter reporter) {
|
|
|
|
|
String clazz = RunnableEx.class.getName();
|
|
|
|
|
reporter.whenInstanceOf(clazz, new Function2<ObjectReporter, HeapObject.HeapInstance, Unit>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Unit invoke(ObjectReporter reporter, HeapObject.HeapInstance instance) {
|
|
|
|
|
HeapField fname = instance.get(clazz, "name");
|
|
|
|
|
if (fname != null) {
|
|
|
|
|
String name = fname.getValue().readAsJavaString();
|
|
|
|
|
reporter.getNotLeakingReasons().add("name=" + name);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
|
|
|
|
|
.dumpHeap(enabled && BuildConfig.DEBUG)
|
|
|
|
|