63

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.

Tyler
  • 21,762
  • 11
  • 61
  • 90
user395494
  • 631
  • 1
  • 5
  • 3

7 Answers7

63

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

Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
Shai Barack
  • 677
  • 5
  • 3
  • 9
    Perfect. 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
  • 12
    Solved 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
  • 1
    this 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
54

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.

Xavi Gil
  • 11,460
  • 4
  • 56
  • 71
  • 14
    Sadly 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
16

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+

Community
  • 1
  • 1
yrizk
  • 394
  • 2
  • 8
5

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.

Community
  • 1
  • 1
David Webb
  • 190,537
  • 57
  • 313
  • 299
4

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
Shahul3D
  • 2,129
  • 1
  • 15
  • 16
  • 1
    i 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
1

You can use the emulator menu. Just telnet to localhost on the port of your emulator (default is 5554) and then type help. Follow the instructions from there!

Dharman
  • 30,962
  • 25
  • 85
  • 135
Frank
  • 1,426
  • 1
  • 14
  • 14
0

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'.

Geekarist
  • 787
  • 9
  • 8