13

On Ice Cream Sandwich (ICS) the OS categorizes your app and each category has its own threshold in which the OS uses for determining which app to close first when it runs out of memory.

Has anyone of you been annoyed by, say you opened a timer to run in the background to measure the time elapsed of something, then you opened few more apps and got back to your timer just to see it was reset or closed? I mean, seriously, it is really excruciatingly annoying.

Is there a way to force an app to remain running in the background no matter what even when the OS runs out of memory?

1
  • 7
    That is a sign a bad code by the App. A user should never have to bother with this and Android provides means so that an App can save their state and restore it later on (since the early Android versions). You should send an bug report to the App developer.
    – Flow
    Jul 9, 2012 at 6:54

5 Answers 5

8

The accepted answer turned out incorrect or outdated. The "App Settings" module for the Xposed app allows you to specify on a per app basis to keep apps from being killed or freed:

http://repo.xposed.info/module/de.robv.android.xposed.installer

http://repo.xposed.info/module/de.robv.android.xposed.mods.appsettings

Responsible care should of course be exercised when playing with apps changing such low level behaviour.

1
  • One should note, that this requires root and is the installation of a heavy Android OS modification. Jan 31, 2017 at 10:01
5

Simple answer: No. Android always uses OOM (Out-Of-Memory) prioritizing to free unused memory. You can change the priorities of apps (at least until reboot) with some task managers but even then if the memory runs low, apps in the background start getting killed.

Think about this scenario: you've downloaded a badly coded app which runs on boot, causes a buffer overflow and hogs all your memory. Normally, this app is killed, but if it's set to stay open it effectively bricks your phone until reboot, and after that starts again, and again, etc.

Some timers use services with high priorities to avoid this problem, but if you launch enough high-performance, resource-hogging apps while the timer is in the background, it will eventually get killed.

10
  • 1
    It doesn't need to be that way, IMO. Let the system do its old ways by default and in case the user intends one particular app to run in the background just at that particular moment when the need arises, just provide that option. It doesn't need to have another permission category since it is users' discretion to "pin" an app. I don't want an app to be permanently unstoppable once it runs. I just want an option to do this when there's a need just like what I have described in my question. Jul 10, 2012 at 16:46
  • 1
    @onik a buffer overflow has nothing to do with hogging memory. Do you know what a buffer overflow is?
    – user14344
    Oct 22, 2014 at 18:47
  • 3
    @onik so the answer is no, you don't know what a buffer overflow is. Got it.
    – user14344
    Oct 22, 2014 at 22:58
  • 1
    @onik also an infinite loop in a linked list could certainly run you into an OOM long before you've looped Integer.MAX_VALUE times. Why do you think things are limited by Integer.MAX_VALUE anyway?
    – user14344
    Oct 22, 2014 at 23:01
  • 1
    @onik correct: by definition, a buffer overflow cannot expand the allocated memory size. If it expands the allocated memory size, it is not a buffer overflow. You're thinking of an infinite loop, I think. Jun 20, 2017 at 6:26
1

No. However, two methods to alleviate the problem come to mind but both of them can only be implemented by the developer: The first one is to display a notification, Android will give apps that do this a higher priority than others. The second one is to save the start time so when the timer gets eventually restarted, it won't be reset.

4
  • 1
    Running Apps without reason with Foreground priority (that's why the notification is needed) is considered bad practice. As is said before: Android provides a nice Api that even allows timers/counters that aren't affected by the OOM killer. There is a nice post by Dianne Hackborn about this topic
    – Flow
    Jul 9, 2012 at 7:06
  • I don't doubt that there is such an API, but where in this question has this been mentioned before?
    – Nova
    Jul 9, 2012 at 7:10
  • 1
    Also, there is obviously a reason to keep the app running if at all possible in this case, unlike the game example you posted.
    – Nova
    Jul 9, 2012 at 7:13
  • 4
    No there is no reason to keep an App running just because of a timer. Just use onSaveInstanceState and onRestoreInstanceState to safe the App state, including the timer information. No need to run in Foreground. Even if it's a alarm you could use AlarmManager to make sure that it's executed later. The only reason where Foreground should be used is: 1. You have some actual foreground activity going on, like playing music or showing a video. 2. You want to keep a data connection open (e.g. IMAP IDLE (push)).
    – Flow
    Jul 9, 2012 at 7:43
0

1st method :--If you make Sticky Thread in the program , yes you can forcefully keep your program on the run. However remember the user at advanced setting mode can stop background running apps. However if you write a the sticky thread in a manner that it starts at any give event occured then this can be achieved.

2nd method : IF the user is non rooted android and u want to run an app on th background . use SL4 script in the background form the app. use a script that will do you background checks and programs and write the front end to show something else.

Eg: In most game engines this is in built. They connect to server s even though the user turned off the app. The reason the engine create multiple scripts in the phone which is randaomly checked at any particular events and that TRIGGER is used to run your app continously on th background of foreground.

***Sorry for the TYPO's .. spellchecker not available. Hope u achieve what u intended to get.

1
  • Welcome to Android Enthusiast, a Q&A site for Android end-users. As far as I can understand, the 2 methods you provided can only be done from the developer-side, i.e. those who have the source code of the app. Unfortunately, that might not be helpful for most of readers here because they are not developing an app. If this might be applicable for non-developer, consider editing the answer to make it clear on how to do these without touching the app's source code. Thanks.
    – Andrew T.
    Feb 22, 2019 at 17:57
-1

There is sometimes an option to lock the app. This prevents killing or stopping of that application in the phone.

Lock option is not available in the OS itself, using some 3rd party apps like GO Task Manager, etc, can help.

1
  • 2
    OOM killing is done by the kernel and no app can prevent it or "lock" a process. The app you have linked to is a task killer that kills apps itself. The locking feature you are referring to will only prevent killing by the task killer.
    – Nova
    Jul 9, 2012 at 15:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .