Background service keeps the app memory - android

I have an app with a service in background. The service runs since the boot, but when the app is started, and closed, the service keeps all the memory that the app uses.
How can I clean all that memory for the service to run light like in the boot ?

Stop the service, if you are not doing so already (via stopService() or stopSelf()).
After that, your process will eventually be terminated by the OS, when it needs the RAM for other processes.

Related

Does an empty foreground service keep an android app alive?

I'm working on an app that keeps communicating with a device by Bluetooth(ble) when it's in both foreground mode and background mode.
I know I should implement ble jobs in a foreground service in Android, but the app is written in flutter and all codes are in dart.
It seems that for now even though there isn't a foreground service, the app keeps alive in background mode.
But I want it to be alive as long as possible.
So I'm thinking about making an empty foreground service...
Will an empty foreground service make an app has some priority in background mode?
And is it ok to do so?
Thanks.
The system can terminate an application in the background at any point. In practice if there is no need for its resources, it runs several minutes maybe more.
When using a foreground service, at some point only the service will run, so there is no real use of "empty service". Closing the application manually will leave only the service running.
Yes, an "empty" Foreground Service will usually prevent the app process from being killed automatically by the system, unless the device is critically low on memory.
How the code in the service class itself looks like does not matter. The important thing is that when a Foreground Service is running in the app process, the whole process will be prevented from being killed. This means that you can have Dart threads in the same process that won't be killed. Activities belonging to the same process that are in the background can still be "destroyed" though, i.e. the onDestroy callback can be called.
Since BLE connections in Android uses the Binder mechanism which are not tied to any of the standard Components (Service, Broadcast Receiver, Activity, ...) that otherwise control how the app process stays alive, having an "empty" Foreground Service is actually a common way to keep BLE connections alive.

How to keep android process alive

How does the apps like Swiftkey, Locker Master manages to keep process alive even after it has been removed from back stack?
They also don't use sticky notification.
Update:-
I need to keep my process alive. But in my case service is active but process gets killed.
They all have one or more unbound background service.
To kill those services you can go into Settings -> Apps -> Running.
However the services may be restarted at any time by other apps or system by sending a system wide message (intent). In most cases it is restarted by
Time events
Boot complete
Other apps
Other intents
In the Swiftkey case, it will be started by Android OS when it needs to show a keyboard.
EDIT: you can specify that a service runs in a remote process by adding this to the service definition in AndroidManifest
android:process="process_name_here"
However there is no such thing as a service that cannot be killed and can run forever. Android OS may start killing your service if it is running low on resources or the service is running for a long period of time. To overcome this, you can make a forground running service, but it needs to show a notification. It can also be killed by task managers, like you mentioned. You should instead start focusing on how to save it's state so you are able to restart it later.

Android Service Gets Killed

I have a service in my application which gets killed automatically. I have tested my application in Android 4.0 (Ice Cream Sandwich)and it runs fine, but when i run the same application in Android 4.4.2 the service gets killed automatically. Could anyone tell me the scenario where a service gets killed automatically?
Android system kills the process when the RAM is in pressure (on low memory), it needs to recycle RAM resources by killing the lowest priority of the apps currently running in the background. So you might set the Service's priority to foreground though this thread runs in background.
From the docs:
A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)
To prevent services from being killed by android's memory management you should start an On Going notification from the particular service and dismiss it when your service ends

Is START_STICKY useful if the app is force-closed by the user

for my android app I want that users should not be allowed to kill it. The app has a service running that waits continuously for event notifications and currently if the user goes to the task manager he can force close the app and kill the service.
I read about START_STICKY but I am not sure what it does exactly...and when. If the app is automatically killed(low memory ??), does START_STICKY ensure that the service is restarted so the app will function normally again?
If that is indeed the case, can I use START_STICKY to restart the service even if the user force closes it?
or is there any other way to prevent the user from closing the app???
As far I know, START_ STICKY is useful if your are implementing a service that uses a server socket for socket programming. Sometimes Android OS may, under low memory circumstances, kill background services so that memory may be reclaimed. So if you use START_ STICKY then the service will not be considered for reclaiming.

When Service is killed, can the process be still alive?

My app has both Service and Activities.
Suppose the process of my app is running. And after a while, the Android OS kills the service in my app due to low memory. My question is, is it possible that Android kills the service only and keeps the process alive? Or when a service is killed, the process must be killed too?
Thanks.
I'm 99,9% sure of this: if the service goes, there goes the process too. The conditions to kill a service are pretty demanding such as in low memory situations. My own experience is that the whole app is killed when the service dies. The service and the process are tied together. Yes, you can manually stop your service without killing your process, but I'm almost certain that when the OS kills your service because of low memory, then bye-bye process.
See Hackbod's answer and comments: Android service killed
See http://developer.android.com/reference/android/app/Service.html and onLowMemory:
This is called when the overall system is running low on memory, and
would like actively running process to try to tighten their belt.
While the exact point at which this will be called is not defined,
generally it will happen around the time all background process have
been killed, that is before reaching the point of killing processes
hosting service and foreground UI that we would like to avoid killing.
Background info: http://about-android.blogspot.com/2010/07/lifecycle-of-android-application.html
This is also interesting: Will we leak the android service connection if the client's process is killed by android?

Categories

Resources