Android 在附加的堆栈跟踪中获取了资源但从未释放过。有关避免资源泄漏的信息,请参阅 java.io.Closeable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25544021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:32:32  来源:igfitidea点击:

A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks

androidandroid-strictmode

提问by dreamer1989

i am getting this message in logcat A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.where to look for leak and what does it meant by " See java.io.Closeable"

我在 logcat 中收到此消息在 附加的堆栈跟踪中获取了资源但从未释放过。有关避免资源泄漏的信息,请参阅 java.io.Closeable。在哪里寻找泄漏以及“参见 java.io.Closeable”是什么意思

采纳答案by suitianshi

That means you have opened something but never close them.Closablehave a method closewhich you must call to release the resources associated with the component when you no longer need it.

这意味着您打开了某些东西,但从未关闭它们。Closable有一个方法close,当你不再需要它时,你必须调用它来释放与组件关联的资源。

To look for the leak, you can try MAT, I often use it to find memory leaks(static data holding a reference to Activity, etc).

要查找泄漏,您可以尝试MAT,我经常使用它来查找内存泄漏(包含对 Activity 的引用的静态数据等)。

回答by superpuccio

For me the problem happened because I was overriding the method onBackPressed()without calling the super()

对我来说,问题发生是因为我onBackPressed()没有调用super()

@Override
public void onBackPressed() {
    //some coding here
    super.onBackPressed();
}

回答by erbsman

if you see something like:

如果你看到类似的东西:

10-12 16:46:44.719 2710-2719/? E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
10-12 16:46:44.719 2710-2719/? E/StrictMode: java.lang.Throwable: Explicit termination method 'end' not called
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at dalvik.system.CloseGuard.open(CloseGuard.java:184)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at java.util.zip.Inflater.<init>(Inflater.java:82)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okio.GzipSource.<init>(GzipSource.java:57)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:490)

in your stacktrace, there is a known bug in older versions of okhttp that you can avoid by forcing the use of a newer version in your gradle file.

在您的堆栈跟踪中,旧版本的 okhttp 中存在一个已知错误,您可以通过在 gradle 文件中强制使用较新版本来避免该错误。

compile 'com.squareup.okhttp3:okhttp:3.2.0'

编译'com.squareup.okhttp3:okhttp:3.2.0'

that solved a very similar problem for me at least.

至少对我来说解决了一个非常相似的问题。

回答by ByteHamster

The same error message also shows up when there is a problem in AndroidManifest.xml. For me, the <activity>tag accidentally went out of <application>

中出现问题时也会显示相同的错误消息AndroidManifest.xml。对我来说,<activity>标签不小心掉了<application>

This is correct:

这是对的:

<application ... >
    ...
    <activity ... />
</application>

This will result in "A resource was acquired at attached stack trace but never released"when starting the activity:

这将导致在启动活动时“在附加的堆栈跟踪中获得了资源,但从未释放”

<application ... >
    ...
</application>

<activity ... />

回答by antonkronaj

This has happened to me while launching a second activity from another activity when I did not declare the second activity in Application.mk.

当我没有在 Application.mk 中声明第二个活动时,这发生在我从另一个活动启动第二个活动时。

回答by vanomart

My error was caused by enabling the strict mode. When I was doing redeployments on a test phone using adb, some resources were not closed correctly.

我的错误是由启用严格模式引起的。当我使用 adb 在测试手机上进行重新部署时,某些资源没有正确关闭。

I "fixed" the error by removing deathPenalty from the strict mode:

我通过从严格模式中删除 DeathPenalty 来“修复”错误:

            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectAll()
                    .penaltyLog()
//                    .penaltyDeath()
                    .build());