如何解决 DeadObjectException 的问题? [英] How to fix problem with DeadObjectException?

查看:26
本文介绍了如何解决 DeadObjectException 的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功完成了一个 Android 项目的实现,并开始测试应用程序的内存泄漏.

I have successfully finished an Android project's implementation and started to test the app for memory leaks.

有时,我在应用程序中长途跋涉后遇到 DeadObjectException 并且所有内存分配都被释放.

Sometimes, I get DeadObjectExceptionafter a long trip in the app and all of the memory allocations are freed.

如何检测此问题?

我对检测内存泄漏的DDMS工具做了一些研究,由于我对DeadObjectExeption一无所知,我不知道从哪里开始.

I have made some research about DDMS tools to detect memory leaks, Due to I have no idea about DeadObjectExeption, I don't know where to start.

推荐答案

这不是内存泄漏问题.内存泄漏的定义(来自维基百科):

This is not a memory leak problem. Definition of the memory leak (from Wikipedia):

计算机科学中的内存泄漏(或泄漏,在此上下文中),当计算机程序获取内存但未能释放它时发生回到操作系统.

A memory leak, in computer science (or leakage, in this context), occurs when a computer program acquires memory but fails to release it back to the operating system.

在这里,你有一个相反的情况 - 内存在它应该释放之前被释放(至少从你的程序的角度来看).

Here, you have an opposite case - memory is freed before it should (at least from your program's point of view).

来自 developer.android.com:

From developer.android.com:

DeadObjectException 扩展 RemoteException

DeadObjectException extends RemoteException

您正在调用的对象已死亡,因为其托管进程已不存在.

例如:

您有 MyActivityMyService 类.您使用 Handler/Messenger 在它们之间进行通信.

You have the classes MyActivity and MyService. You use Handler/Messenger to communicate between them.

您在MyActivity中创建HandlerMessenger,然后将创建的Messenger实例发送给MyService 通过 Intent.然后你做了一些事情,时间过去了,你的 MyActivity 被销毁了,连同它的 HandlerMessenger.现在,如果你处理不好,MyService 不会知道他拥有的 Messenger 不再有效,所以,他会尝试通过它发送一些东西, 并得到 DeadObjectexception:

You create Handler and Messenger in MyActivity, and then send created instance of Messenger to MyService via an Intent. Then you do some stuff, time passes, and your MyActivity gets destroyed, together with it's Handler and Messenger. Now, if you don't handle that well, MyService won't know that Messenger that he has is not valid any more, so, he tries to send something through it, and get DeadObjectexception:

/*向此 Messenger 的处理程序发送消息.

/* Send a Message to this Messenger's Handler.

参数:

message 要发送的消息.通常通过 Message.obtain() 获取.

message The Message to send. Usually retrieved through Message.obtain().

抛出:

RemoteException 如果目标处理程序不再存在,则抛出 DeadObjectException.*/

RemoteException Throws DeadObjectException if the target Handler no longer exists.*/

public void send(Message message) throws RemoteException {...}

public void send(Message message) throws RemoteException {...}

这篇关于如何解决 DeadObjectException 的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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