In order to generate the notifications i need to know about how to generate the low battery and low memory interrupts programmatically. Can any one please provide your suggestions.I am aware of Intents.
-
You need to override `onTrimMemory(int level)` in your code in order to handle memory events. – IgorGanapolsky Apr 22 '16 at 17:15
-
5Low memory https://play.google.com/store/apps/details?id=me.empirical.android.application.fillmemory – k4dima Jan 28 '18 at 18:59
7 Answers
To trigger your onTrimMemory
callbacks:
adb shell am send-trim-memory <process-name> <level>
e.g. adb shell am send-trim-memory com.example.app MODERATE

- 8,025
- 3
- 44
- 57

- 677
- 5
- 3
-
9Perfect. I also like `RUNNING_CRITICAL` to get the full effect. – IgorGanapolsky Apr 22 '16 at 17:09
-
3@MaksimDmitriev I get that error when using a physical device, but it works for me on emulator – matt--- Jul 29 '16 at 23:17
-
9`adb shell am send-trim-memory com.myapp MODERATE` `Error: Failure to set the level - probably Unknown Process: com.myapp`. Any idea why? That package definitely exists. Also, I'm having a hard time finding documentation on that command. – AutonomousApps Sep 16 '16 at 18:51
-
12Solved it. The error message is wrong (the key is the word 'probably'). What actually was happening in this case is that it wasn't recognizing MODERATE, despite the fact that it's listed as a valid level. Using `RUNNING_CRITICAL` worked. – AutonomousApps Sep 16 '16 at 19:59
-
3@AutonomousApps it won't let you send a TRIM_MEMORY level that is invalid for the current state of the app. For example, if your app is in the foreground then it will fail to send the trim event for any level higher than TRIM_MEMORY_RUNNING_CRITICAL. This took me over 10 minutes to figure out. – androidguy Feb 11 '18 at 00:12
-
1this only works on an EMULATOR, on my real device I always had errors like: "Unable to set a higher trim level than current level" or "Unable to set a background trim level on a foreground process" – Alberto M Aug 01 '22 at 11:07
Low memory can also be simulated using Background process limit
under the device developer options.
Go to Settings > Developer options
. Under the app
section change the Background process limit
to No background processes
Now your activity will be killed every time you switch to another app. Useful for testing state saving and state restoration.

- 11,460
- 4
- 56
- 71
-
14Sadly this is not true at all. Here is [great explanation](http://android.stackexchange.com/a/62393) what exactly this option does. – s.maks Apr 14 '16 at 12:36
-
I have an app with bg services and push notifications enabled, the app won't be killed as for other apps. – Alberto M Nov 13 '17 at 11:45
yes, this api triggers the same callback you would get if you registered a context to ComponentCallback2, specifically the ComponentCallback2#onTrimMemory this wasn't mentioned here, so I thought I'd make it clear. The syntax for this command is:
am send-trim-memory [--user <USER_ID>] <PROCESS> [HIDDEN|RUNNING_MODERATE|BACKGROUND|RUNNING_LOW|MODERATE|RUNNING_CRITICAL|COMPLETE]
Note: this command is only available on devices running Marshmallow+
-
3Very true. I could only get this to work on devices running Marshmallow and above. – Demitrian Oct 03 '16 at 13:42
On the Android Emulator you can set the power status by connecting to the Emulator console and using the power
command.
As far as low memory goes, you just need to make sure that your application can handle being killed without warning when it is in the background. Testing this is one of the very few cases that actually call for a Task Manager on Android, or if you're running Android 2.2 you can kill applications via Settings.
There are ways of reducing the memory available to applications but I think they're unnecessary.

- 1
- 1

- 190,537
- 57
- 313
- 299
-
1Hi Dave Webb, I want to do it programmatically to show an low battery alert even though it is not. – user395494 Sep 07 '10 at 09:07
-
Hi Dave Webb, In emulator its ok. But how can i do the same in real device..? – user395494 Sep 07 '10 at 09:15
-
There isn't a way to make a phone show a low battery alert when the battery is not low. – David Webb Sep 07 '10 at 12:12
To simulate low Battery warning, try this command in the way answered by Frank:
power capacity 10 // It will set the battery level into 10%
For low Memory:
ulimit -Sv 15000 //The current memory limit will set to 15000 Kb

- 2,129
- 1
- 15
- 16
-
1i am running the application in device and in DDMS view, i could see the heap memory viewer and i see the heap memory allocated as 18.2 MB. Using your command i set current memory as 1500. So why application did not crash or allocated memory did not reduce ? – Prem Feb 17 '14 at 19:19
-
2@Prem, After 'telnet' to 'localhost 5554', I'm trying 'ulimit' command. But always it is returning error 'KO: unknown command, try 'help''. I'm using Win7 OS. Pls help me. – Venkatesh Achanta Jul 17 '14 at 04:25
-
@IgorGanapolsky I searched, it's a shell build-in command which exists in most of Linux OS, including Android. So you can run it by `adb shell ulimit -Sv 15000` – CalvinChe Oct 23 '19 at 03:40
To trigger the memory trim event, an app can be used that fills all the RAM of the device, and that triggers the event.
There are many on the Play Store, they can be found by searching for 'fill ram'.

- 787
- 9
- 8