0

Is there a way to determine whether a process has died? I have a system service that stores information on each client that connects to it. When the process that is hosting that client dies, I would like to clean up my cache. I've searched all over google but it seems that this is not possible :(

One solution that I came across was using Binder's linkToDeath, but that design does not mesh really well with my current design. Is there an alternative ?

Thanks

Jon
  • 1,381
  • 3
  • 16
  • 41

2 Answers2

0

The state of Thread you can determine by using isAlive() method to this Thread, or use getState() to determine thread state more precisely.

Binder has method isBinderAlive(), but I didn't use it so can't add anything uncovered in officical docs.

Rodion Altshuler
  • 1,713
  • 1
  • 15
  • 31
  • Thanks for idea. However, I believe that will only tell me if the current Thread has died. I was hoping for a solution that would tell me whether the entire process has died. – Jon Apr 11 '13 at 17:36
0

The approach of using linkToDeath is the only one which I know of.

Normally here's what I would do:

  • Have a class, IMySystemService.aidl which is implemented by your system service.
  • Have a listener, IMySystemServiceListener.aidl which is implemented by each client.
  • Add a method such as IMySystemService.addListener(IMySystemServiceListener listener)

Then, use linkToDeath on the listener within your system service to get a callback for when each client dies. You can then clean up the relevant parts of your cache.

If your project is at all complex already, you probably have some suitable per-client listener already?

Can you answer why linkToDeath doesn't work for your design?

One common pattern for caches is to use WeakHashMap for your cache - but this only works if you have a key object which is garbage-collected when the client disappears. I believe that this, in turn, can only be achieved using linkToDeath so we're back to where we started!

Adrian Taylor
  • 4,054
  • 2
  • 24
  • 26