Android service still resides in RAM after onDestroy() is called

187 Views Asked by At

I'd like to perform the memory test of my android package.

In my package, there is only one very simple service.

The service is started only by bindService from another package. (not startService)

After, another package (package 1) calls bindService to my package, the package termintates. Then the log of my package is as follows.

04-04 16:13:06.962 5468-5468/memorytestapp I/: @onCreate

04-04 16:13:06.966 5468-5468/memorytestapp I/: @onBind

04-04 16:13:12.221 5468-5468/memorytestapp I/: @onUnbind
04-04 16:13:12.258 5468-5468/memorytestapp I/: @onDestroy

After onDestroy is called, I expect that my package do not resides in RAM after certain time.

However, even though the package 1 (caller) do not resides in RAM right after the termination, my packagee (callee) still resides in RAM continuously.

After 3 hours, when I enter the 'adb sehll dumpsys activity oom | grep my package name' to check the memory status, it shows this line.

Proc # 5: cch B/ /CEM trm: 0 5468:memorytestapp/u0a227 (cch-empty)

Although my package is in cached status, it still resides in RAM continuously. Even when I insert the code "stopService or stopSelf" at onDestroy, it has same status!

I really want my package not to reside in RAM after onDestroy is called.

Is there any opinion or comment to this situation??

Sincerely, Logan.

Source code of my package is as follows.

public class TestService extends Service {

    @Override
    public void onCreate(){
        super.onCreate();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return true;
    }

    @Override
    public void onRebind(Intent arg0){
    }
}
1

There are 1 best solutions below

2
kgandroid On

Services are cached after calling stopservice(). They are called empty process.

A process that doesn't hold any active application components. The only reason to keep this kind of process alive is for caching purposes, to improve startup time the next time a component needs to run in it. The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.

See this link and read about empty process.

However you can kill a process forcefully by using :

int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);