W/System: A resource failed to call release

后端 未结 3 3927
甜味超标
甜味超标 2020-12-11 03:00

As the title says. When using the console of AndroidStudio on my app it shows:

W/System: A resource failed to call release.

Sometimes it is said multiple ti

相关标签:
3条回答
  • 2020-12-11 03:15

    This message comes from dalvik.system.CloseGuard. When debugging, you can set it up to create stack traces as you create resources, so that you can track down what objects aren't being closed.

    It's not part of the framework API, so I'm using reflection to turn that on:

    try {
        Class.forName("dalvik.system.CloseGuard")
                .getMethod("setEnabled", boolean.class)
                .invoke(null, true);
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
    

    more info: https://wh0.github.io/2020/08/12/closeguard.html

    0 讨论(0)
  • 2020-12-11 03:16

    The answer from @guest works, but you can achieve the exact same thing without resorting to reflection using Strict Mode. Specifically, something like:

    StrictMode.setVmPolicy(new VmPolicy.Builder()
                     .detectLeakedClosableObjects()
                     .penaltyLog()
                     .build());
    

    In general, strict mode can do much more for you though (see link above to doc), and all you need to do for a default setup is:

    StrictMode.enableDefaults();  # <-- This includes warning on leaked closeables
    
    0 讨论(0)
  • 2020-12-11 03:23

    I don't think that you can get any more information out of Logcat.

    The memory view of the Android Profiler is probably a good place to start. Looking at it while using your app should give you an idea of what actions cause memory to be allocated and not released. You can also select sections from the timeline and drill down to specific allocations by class.

    Alternatively LeakCanary is a great library to detect memory leaks.

    0 讨论(0)
提交回复
热议问题