onTrimMemory callback not invoked

1.5k views Asked by At

I have a long running service, for performing certain background operation. Now, I've read up on several threads, that Android considers killing these services, when the RAM is low, as the background LRU cache has to be cleared up a bit to accomodate more pages. So, I let Android kill it, and I save my service data in onTrimMemory callback (when the level is 80), and I hope that I'd get this callback everytime before Android kills the process of my service. But under some circumstances, I don't get any callback in onTrimMemory and my process is killed, leading to loss of data.

Here are the logs before my process is killed :

06-15 20:43:18.150 17240 17365 I Icing   : Indexing 4C988B73CC982428683ECBAE956EE592EB3D7590 from com.google.android.googlequicksearchbox
06-15 20:43:18.170   888   888 I 
ConfigService: onDestroy
06-15 20:43:18.290 17240 17365 I Icing   : Not enough disk space for indexing trimmable
06-15 20:43:18.360   584   710 I InputReader: Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.213 ] when=2010937946000
06-15 20:43:18.360   584   709 I InputDispatcher: Delivering touch to: action: 0x4, toolType: 1
06-15 20:43:18.360   584   709 I InputDispatcher: Delivering touch to: action: 0x0, toolType: 1
06-15 20:43:18.360   584   709 I InputDispatcher: Delivering touch to: action: 0x0, toolType: 1
06-15 20:43:18.390   584  1075 W ActivityManager: Failed setting oom adj of ProcessRecord{4251b490 17777:com.example.gaurav/u0a137} to 13
06-15 20:43:18.390   584  1075 W ActivityManager: Failed setting oom adj of ProcessRecord{4260cf38 16704:android.process.media/u0a24} to 15
06-15 20:43:18.400   584  1075 I ActivityManager: Killing 17971:com.google.android.apps.docs/u0a60 (adj 11): empty,  17028K
06-15 20:43:18.430   584   927 I ActivityManager: Process com.example.gaurav (pid 17777) (adj 11) has died.
06-15 20:43:18.440   584   927 W ActivityManager: Failed setting oom adj of ProcessRecord{4260cf38 16704:android.process.media/u0a24} to 11
06-15 20:43:18.440   584   885 I ActivityManager: Process android.process.media (pid 16704) (adj 13) has died.

My process is com.example.gaurav, and there's no onTrimMemory log before my process gets killed. Anyone here could explain what those logs indicate? I already googled but couldn't find anything convincing. Regarding the foreground service solution, I'd want to avoid that as my service is long running, and making it foreground consumes a lot of battery.

@pskink, This is my onTrimMemory callback method in service :

@Override
    public void onTrimMemory(int level) {
        // TODO Auto-generated method stub
        Log.v(LOG_TAG, "on Trim memory callback, level : " + level);
        super.onTrimMemory(level);
        if (level >= TRIM_MEMORY_COMPLETE && UsageSharedPrefernceHelper.isServiceRunning(mContext)) {
            Log.v(LOG_TAG, "Trim memory level is greater than 80, kill service");
                saveDataOnKill();
        }
    }

Also, I do get logs sometimes, such as :

06-15 20:43:46.000 18420 18420 V MyService: on Trim memory callback, level : 80

So, it's not about the absence of logs certainly.

0

There are 0 answers