My app is a background service running in the foreground. In low memory situations some of my processing threads are being killed without the app being restarted. This is causing very strange behavior. I would like to be alerted by the OS when the memory is low before my process are killed. This will allow me to release memory or restart my app. I can't seem to find a broadcast or notification for this situation.
-
good question, I'm also wondering how to receive notifications for low memory. – paiego Nov 11 '12 at 12:58
-
Here's my answer maybe you will find what you're looking for https://stackoverflow.com/a/69727078/5135372 – Farido mastr Oct 27 '21 at 11:03
2 Answers
I suggest you read "Managing Your App's Memory" here: https://developer.android.com/training/articles/memory.html. The system callback method onTrimMemory will inform you of different RAM usage scenarios as indicated by the value of the method argument. See https://developer.android.com/reference/android/content/ComponentCallbacks2.html#onTrimMemory(int) for details. This method's description says:
Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process. This will happen for example when it goes in the background and there is not enough memory to keep as many background processes running as desired. You should never compare to exact values of the level, since new intermediate values may be added -- you will typically want to compare if the value is greater or equal to a level you are interested in.
To retrieve the processes current trim level at any point, you can use ActivityManager.getMyMemoryState(RunningAppProcessInfo).
For example, TRIM_MEMORY_COMPLETE as an argument to onTrimMemory:
public static final int TRIM_MEMORY_COMPLETE Level for onTrimMemory(int): the process is nearing the end of the background LRU list, and if more memory isn't found soon it will be killed. Constant Value: 80 (0x00000050)

- 167
- 1
- 6
- There is one background service “DeviceStorageMonitorService” continuously running inside Android, which checks system storage directory periodically.
- This service maintains a memory threshold which is 10% of the assigned internal system memory, so in case of P2 it is 861MB, so memory threshold is 86 MB. We can change this by changing macro DEFAULT_THRESHOLD_PERCENTAGE inside file DeviceStorageMonitorService.java.
- It checks the size of available memory with the memory threshold, if it is less than threshold memory, it will broadcast notification and sticky broadcast intent ACTION_DEVICE_STORAGE_LOW. In case of full memory intent ACTION_DEVICE_STORAGE_FULL
Receive this broadcasted intent in your receiver and handle the application.

- 44
- 1
-
I came across this alright but I figured it was monitoring the device internal or external storage not the RAM. So do you think if I release memory or restart my app when I receive the ACTION_DEVICE_STORAGE_LOW intent it should solve my problem? Thanks – null Mar 21 '12 at 11:45
-
I look in to it there, yes this is indeed what i am looking for. Thanks again, hope this helps someone else. – null Mar 21 '12 at 11:51
-
i feel it should solve your problem. when you get this intent. you need to handle your application because framework will inform to app when memory it low, if app does not handle it, it will kill the process which will have low priority. – Wish Mar 21 '12 at 11:55
-
This is true, do you think restarting the app with solve this by increasing its priority – null Mar 21 '12 at 12:06
-
1Having just looked through the source I'm pretty sure your original instinct that this is only for device storage (i.e. flash memory) not RAM was correct. It's monitoring free space on the /system, /data and /cache filesystems and alerting when they're too low. It doesn't even look at available RAM. – Jules Mar 01 '13 at 09:10