I understand and accept the fact that the Android OS knows better than me when it's the right time to terminate the process, but I haven't yet heard a good explanation on why it's wrong to use the killProcess() method. After all - it's part of the Android API.
What I do know is that calling this method while other threads are doing potentially important work (operations on files, writing to DB, HTTP requests, running services..) results in the threads being terminated and it's clearly not good. Also, I know I can benefit from the fact that "re-opening" the application will be faster because the system stills "holds" in memory state from the last time the app was used, and killProcess() prevents that.
Besides this reason, assuming I don't have such operations, and I don't care my application whether my app will start from scratch every time it is opened, are there other reasons why I should not use the killProcess() method?
And another thing - I'm developing games with the Unity3D framework and exporting the project to Android. When I decompiled the generated apk, I was very surprised to find out that the java source code created from unity - implementing Unity's - Application.quit() method, with Process.killProcess(Process.myPid()).
In a perfect world, with perfect code and libraries, you shouldn't need to call Process.killProcess(Process.myPid()) and the OS will correctly kill your application as appropriate. Also there will be peace in the Middle East, pigs will fly, and the halting problem will be solved.
Well, Unit3d is most probably using native code, and they are killing the process as an insurance -- they don't want to leak memory. You could argue whether this is a good idea or not, but the fact that they used it does not mean that you should too.
Maybe there are some extreme cases where you would want to use killProcess(), but usually the OS does this for you, according to current load and usage. Not sure what kind of an answer you are looking for -- you are aware that using killProcess() might break things, unless you can justify its usage, don't use it.
it is a bad idea. for one, your activity won't worth with instrumentation, as those frameworks register on all the lifecycle methods and only after clean execution do they report a test success status. if you kill the process from underneath the test application, it will report a failure and it won't be clear this is happening.
In my opinion, the reason why kill process is not recommend by some Android developer is , it's hard to make sure all the service, activity quit safely and properly if you just simply kill the process. For a APP developer, their APP may sharing data with other APP, some Activities may export to others. If you kill the process, other app might goes wrong.Moreover, in most of cases, you actually do not need "kill" the app, just finish what you are doing and what you don't wanna user see, leave other works to the Android system. They know better than us.OK, if you know clear what you wanna do, don't care about open faster and so on, like you wanna quit a game when users click some button, your game of course will not export to other app, will not sharing data outside your game, it's ok to do this, just carefully make sure no other services or thread need running. In this case, I recommend finish all the activities before you kill the process, just to make sure Activities properly save state, I'll call something like:
android-studio was installed via snap. But sometimes processes still hang in the background and eat a lot of CPU (and hence battery power) after closing android-studio. Then I need to kill those manually using the process ID with switch -9.
snap is a system for application distribution and installation. Processes spawned from a program installed as a snap are no different from processes of any other software running on the system, and thus are to be managed identically.
In other words, there is nothing specific to processes that are associated with software installed as a snap. Each software package causes one or more processes to be started. Which ones these are are specific to the software.
Thus, no general answer can be given on the question "how to stop processes of a snap package". This comment equally applies if you leave out the word "snap". So really, a specific answer is only possible for specific software.
So, I'm using Andronix app on my Galaxy Note20 Ultra to run Ubuntu 20 on my phone, but Android 12 killes the background process of Termux every less than 5 minutes, which makes it unusable. However, I was told that I would be able to disable it with Android 13, and now that I updated my phone to Android 13 today, I can't find the option to disable phantom process killer. I thought it should be in feature flags in dev options, but the list is empty, any suggestions?
I was having same problem. I just found a workaround for this. You can start the Unity activties in a different process than the main one in your app. For this you will have to add following in your manifest for all the Unity activities (in my case UnityPlayerProxyActivity, UnityPlayerActivity, UnityPlayerNativeActivity).
After time searching around, I found the solution for avoiding kill process in UnityPlayerActivity when we call finish();
In Activity extends UnityPlayerActivity, you should override onPause method and call super.onDestroy() on it instead of super.onPause().
Have a nice day!
I'd like a browser app (FireFox) not to be killed every now and then by the system's task killer since I'd like to use certain single page applications that hold some data and are not loaded very quickly. Is there any way to deal with this, even if root is required?
Some time ago, I've updated my device from a 1 GB RAM to another one, 3 GB RAM and everything went well (browser stopped to get killed at all). But after a system update in Jan 2016, things got even worse, now FireFox usually gets killed even after an alarm clock rang or I opened a camera app. This is a major issue for me so I appreciate any help about this.
It's possible with root, you can tweak oom_adj values to prevent apps from being killed, alternatively force the target app to stay in memory by "locking" it or change some related settings responsible for killing apps in low memory condition.
Android uses a different way of handling processes. Instead of killing every process after its activity ended, processes are kept until the system needs more memory. The idea is to give speed improvements if you start that activity again. But how/when does Android kill a process if it needs more memory and which process to kill first?
This is managed by the LMK (Low Memory Killer) driver of Android. You may already know that every app/process in Android is assigned an oom_adj value, which indicates the likelihood of it being killed when an out of memory (OOM) situation occurs. More higher it's value, the higher likelihood of it getting killed. Valid range is -17 to +15. (if in the -17 range means it won't get killed). According to that, there are six groups (OOM groups), into which apps/processes are categorised:
CONTENT_PROVIDER: This is a process with a content provider that does not have any clients attached to it. If it did have any clients, its adjustment would be the one for the highest-priority of those processes.
These groups are defined by oom_adj value limits, and apps would fall into one of those groups according to the oom_adj value assigned to that particular app. "Foreground apps" usually have an oom_adj value of 0 or less (so they are the least killable; i.e High priority).
"Empty apps" have a higher oom_adj (they are killed early; i.e Low priority). Also, oom_adj value changes according to the state of the user app; it's 0 when the app is active in the foreground and assigned a higher value when the app goes to the background.
Why their "killability" differ? Apps belonging to these different groups (that have different oom_adj's), start to get killed at different levels of free RAM. These triggering RAM limits are defined by the LMK minfree values. Above 6 categories correspond with 6 RAM limits which are set in the LMK minfree. eg: Stock Android 4.3 in my device comes with the minfree values of 48,60,72,84,96,120. (these are in MB).
Practically what it means is, Empty apps will get killed when ram goes below 120mb, Content providers when it goes below 96mb, Hidden apps when it goes below 84mb and so on.. lastly starts killing Foreground apps when ram goes below 48mb. (You may notice that this last value (48mb) is not desirable when using memory intensive apps like heavy games).
It is said that there are many OOM process categories that are assigned different oom_adj priorities by the ActivityManagerService, but eventually all of those would be considered under above six slots/groups (according to oom_limits), for the purpose of killing by the LMK minfree triggers. Therefore, those six are the important ones for normal users.
The post above is very thorough, but I will add instructions for a few specific models (stolen from ASM app page). Note that for apps that use more memory (eg Firefox), the system will likely eventually kill the app anyway when other apps request memory space. If the cached ram is needed, the largest and oldest apps go first regardless of battery saving settings, and Firefox is a big spender.
The best thing about this method is that you can combine this command with two additional ADB commands to automate the flow of going to home screen, then killing the application and then launching it again:
Setting background processes limit to 0 instructs Android OS to eagerly kill all background processes regardless of memory conditions. Therefore, the moment any application will be pushed to background it will be killed.
Before explaining the bug, you first need to understand how the In-App Payments SDK works. The In-App Payments SDK works by launching an isolated Activity (card entry activity), processing the information, and providing different ways to handle the result via callbacks. These callbacks will receive a card nonce (a single use token) that can be used to perform a payment or save the credit card nonce for a customer profile. Developers can use these options to process a payment right after we get a nonce, before or after the activity finishes, and tell the app user if the payment went through or the card was saved.
df19127ead