如何解决DeadObjectException问题? [英] How to fix problem with DeadObjectException?
问题描述
我已经成功完成了Android项目的实施,并开始测试该应用程序是否存在内存泄漏.
I have successfully finished an Android project's implementation and started to test the app for memory leaks.
有时候,我在应用程序中经过长时间的旅行后得到了DeadObjectException
,并且所有的内存分配都被释放了.
Sometimes, I get DeadObjectException
after 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.
推荐答案
这不是内存泄漏问题.内存泄漏的定义(来自Wikipedia):
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
您正在调用的对象已死亡,因为它的托管过程不再存在.
例如:
您具有类MyActivity
和MyService
.您使用Handler
/Messenger
在它们之间进行通信.
You have the classes MyActivity
and MyService
. You use Handler
/Messenger
to communicate between them.
您在MyActivity
中创建Handler
和Messenger
,然后通过Intent
将创建的Messenger
实例发送到MyService
.然后,您做一些事情,时间流逝,您的MyActivity
和Handler
和Messenger
一起被销毁.现在,如果您处理得不好,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.*/
公共无效的send(消息)抛出RemoteException {...}
public void send(Message message) throws RemoteException {...}
这篇关于如何解决DeadObjectException问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!