在Android的资源泄漏 [英] Resource leak in Android

查看:492
本文介绍了在Android的资源泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发了几个星期的Andr​​oid应用程序,并保持现在每一次看到这个问题:

I have been developing an android app for a few weeks now and keep seeing this problem every now and again:

06-24 14:04:48.915    1530-1539/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:184)
        at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
        at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
        at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
        at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:57)
        at android.os.Binder.execTransact(Binder.java:446)

我能想到的,导致此的唯一的事情是,我用这个code打开浏览器中的网址:

The only thing I can think of that is causing this is that I open a url in the browser using this code:

ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.practiceadmin.com/legal/"));
            browserIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(browserIntent);

        }
    };

但奇怪的是,当我打开android.os.ParcelFileDescriptor.java文件,上面说的import语句:

The strange thing is when I open up my android.os.ParcelFileDescriptor.java file, it says that for the import statements:

import dalvik.system.CloseGuard;
import libcore.io.IoUtils;
import libcore.io.Memory;

它不能解析符号'CloseGaurd'/'libcore。
解决这个任何方向将大大AP preciated。

it cannot resolve symbol 'CloseGaurd'/'libcore'. Any direction in solving this would be greatly appreciated.

推荐答案

这仅仅是一个在这个问题上我的研究报告 - 不是一个答案了。

This is only a report of my research on the issue--not an answer yet.

此问题已经在相关的帖子近日报道:例如,<一个href=\"http://stackoverflow.com/questions/29861414/resolving-java-lang-throwable-exception-in-an-android\">here, <一href=\"http://stackoverflow.com/questions/30488289/android-http-get-explicit-termination-not-called-error-what-am-i-missing\">here和<一个href=\"http://stackoverflow.com/questions/30250545/application-crashes-when-$p$pssing-the-button\">here.

This problem has been reported recently in related posts: for example, here, here and here.

我有一个启用BackupAgent的应用程序。我最近启用StrictMode,我开始看到包含在此同一CloseGuard例外,其他职位,其中包括IBackupAgent的堆栈跟踪。所以,我认为我在BackupAgent出现了问题。

I have an app with an enabled BackupAgent. When I recently enabled StrictMode, I started seeing the same CloseGuard exception included in this and the other posts, which includes IBackupAgent in the stack trace. So I assumed I had a problem in my BackupAgent.

我看到我已经忘了关闭ParcelFileDesciptors在我BackupAgent的 onBackup()方法,所以添加以下语句:

I saw I had neglected to close the ParcelFileDesciptors in my BackupAgent's onBackup() method, so added these statements:

// if there is no prior state, oldState is null
if (oldState != null) {
    oldState.close();
}
newState.close();

随机例外这种变化甚至继续。这时我才发现,在一些问题的其他报告,OP的是全新的Andr​​oid开发者,可能还没有建立BackupAgents他们的应用程序。这使我怀疑,资源泄漏发生在框架code为BackupService,或者是通过其他应用BackupAgents引起的。该logcat的为this崩溃表明 DictionaryBackupAgent 活跃。

我没有为一个应用程序组件远远StrickMode设置如何传播有很好的理解。该StrictMode 文档指出,它不跨进程边界传播时,其状态做粘结剂称之为。这表明一个应用程序的StrictMode设置可能会比预期更大。这些崩溃可能发生随时StrictMode启用和备份发生,并且可能不被它们所报告的应用程序的code引起的。

I don't have a good understanding of how far StrickMode settings for an app component are propagated. The StrictMode documentation indicates that "it does propagate its state across process boundaries when doing Binder calls". This suggests that an app's StrictMode settings may be larger than expected. These crashes may be occurring anytime StrictMode is enabled and a backup occurs, and may not be caused by the code of the app in which they are reported.

这篇关于在Android的资源泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆