【问题标题】:Is there a way to stop android services in init.rc gracefully (without SIGKILL)?有没有办法优雅地停止 init.rc 中的 android 服务(没有 SIGKILL)?
【发布时间】:2016-08-08 20:33:23
【问题描述】:

我有一个本机 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?

【问题讨论】:

    标签: android linux service signals init


    【解决方案1】:

    您的“无内存泄漏”站在内核方面,但是在一个进程结束之前需要在用户空间完成许多工作,例如以下,

    • 关闭文件并将缓存数据刷新到内核层。
    • 删除一些临时文件。
    • 通知其他进程我即将死亡。
    • 将RAM中缓存的一些数据写入磁盘,否则DISK上的数据会不一致。

    …… 对于上述情况,发送 SIGKILL 非常粗暴。

    【讨论】:

      【解决方案2】:

      在与同事讨论后,我们的共识是,在 SIGKILL 之后,内核将回收与进程相关的所有内存,并且不会发生内存泄漏。

      如果有人有任何与此相矛盾的信息,请告诉我。

      【讨论】:

        猜你喜欢
        • 2016-03-25
        • 2023-01-23
        • 2011-11-20
        • 1970-01-01
        • 2013-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多