我有一个 native Android 服务,它由通过 GUI 设置的属性启动和停止。 init.rc中启动和停止服务的命令如下:
service myservice /system/bin/myservice
class late_start
user my_service
disabled
on property:persist.myapp.myservice.enable=1
start myservice
on property:persist.myapp.myservice.enable=0
stop myservice
有JNI内存资源需要在服务停止时清理,但是貌似服务是通过SIGKILL停止的:
6944 6974 V myservice: IMyProcessA::onTransact()
712 712 I AP_KERNEL: init: Service 'myservice' is being killed...
510 510 I ServiceManager: service 'ProcessA.MyService' died
656 656 V ProcessA: ProcessA::NotificationClient::binderDied()
656 656 V ProcessA: removeNotificationClient() 0xb6bc60a8, pid 6944
656 656 V ProcessA: ProcessA::NotificationClient::~NotificationClient()
712 712 I AP_KERNEL: init: Service 'myservice' (pid 6944) killed by signal 9
712 712 I AP_KERNEL: init: Service 'myservice' (pid 6944) killing any children in process group
因为没有办法拦截 SIGKILL 来清理,init.rc 'stop myservice' 是否有任何选项可以将终止信号更改为我可以拦截的信号,例如 SIGTERM?
最佳答案
你的“无内存泄漏”是在内核端,但是很多工作需要在一个进程结束前在用户空间完成,比如下面的,
- 关闭文件并将缓存数据刷新到内核层。
- 删除一些临时文件。
- 通知其他进程我快要死了。
- 将RAM中缓存的一些数据写入磁盘,否则磁盘上的数据会不一致。
…… 对于以上,发送SIGKILL是非常粗暴的。
关于android - 有没有办法优雅地停止 init.rc 中的 android 服务(没有 SIGKILL)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38838044/