Skip to content

About

Keeps games on AYN handhelds from being closed by Android's low memory killer on loading screens. File cache control and optional parked-RAM onlining, started from AYN's built-in Run script as Root. No rooting, no PC.

Topics

Resources

Stars

18 stars

Watchers

1 watching

Forks

Repository files navigation

AynRamScript

Heavy games on AYN handhelds can get closed in the middle of a loading screen while several GB of RAM still show as available. AynRamScript is a small background script written to stop that. It is started from AYN's own Run script as Root setting, so it needs no rooting, no bootloader unlock and no computer.

It does not change any Android or kernel setting. After a reboot it is just not running any more, unless autostart was turned on.

Download the package for 16 GB devices Download the package for 12 GB devices Download the package for 8 GB devices Download the package that picks the limits itself

Total downloads

Unofficial. Not affiliated with AYN. Only download it from this repository's Releases.

Which package

Package For Limits Tested
16gb 16 GB of RAM, such as the Thor Max as measured on a Thor Max yes
12gb 12 GB, such as the Thor Pro the 16 GB limits times 3/4 not yet
8gb 8 GB, such as the Thor Base the 16 GB limits times 1/2 not yet
auto any of the above reads the RAM at start and uses the matching set on a Thor Max

All four are the same script, only the numbers in dropcache_tuning.sh differ. The limits were measured on a 16 GB Thor Max. The 12 and 8 GB numbers are those scaled down. Take the package for your RAM, or auto if you are not sure.

Install

  1. On the handheld, download the zip and unpack it into Download. You should end up with a folder named exactly Download/AYNRAMSCRIPT.
  2. Open Settings → Handheld Settings → Run script as Root (called Odin Settings or Thor Settings on some firmware) and pick Download/AYNRAMSCRIPT/dropcache_run_plus.sh.
  3. Open dropcache_status.txt in the same folder. It should say running.
  4. Play. After a reboot, do step 2 again, or run dropcache_autostart_on.sh once and it starts by itself.

With a computer it is one command from the unpacked zip, then step 2:

adb push AYNRAMSCRIPT /sdcard/Download/

To update, copy the new folder over the old one and pick dropcache_run_plus.sh again. This also works coming from 7.2.

Which file does what

Everything works the same way: open Run script as Root, pick one file from Download/AYNRAMSCRIPT, done. Each file does one thing and writes the result to dropcache_status.txt in the same folder.

You want to Pick this file
Start (recommended) dropcache_run_plus.sh
Start without switching the parked RAM on dropcache_run.sh
Stop dropcache_stop.sh
Have it start by itself after every reboot dropcache_autostart_on.sh
Undo that dropcache_autostart_off.sh
Get a report to attach to an issue dropcache_report.sh
Remove it dropcache_uninstall.sh, then delete the folder
  • You will probably only ever need the first one. What plus adds is explained under What it does.
  • Picking a start file while the script is already running restarts it. Nothing breaks.
  • dropcache_autostart_on.sh only switches autostart on. It remembers whether you last started with or without plus.
  • The report is written to dropcache_report.txt: state, device, settings, why Android last closed the app, and the full log.
  • dropcache_body.sh, dropcache_ctl.sh and dropcache_tuning.sh are the script itself. Do not pick those.

What it does

This is what happens when a heavy game gets closed while loading, as far as the logs and the source of Android's low memory killer show (the notes are in research/):

  • Android keeps most spare RAM as file cache. That memory counts as "available", but it is not free.
  • When a scene loads, the GPU wants a lot of memory within a few seconds. The kernel can't empty the cache that fast and stalls.
  • The low memory killer (lmkd) takes the stall for "out of memory" and closes apps, the game included.
  • On top of that, Qualcomm's power saving keeps 2 to 3 GB of RAM parked and only switches it on once memory is already short.

At a loading screen 6.7 GB showed as available, 6.3 GB of it was file cache and 0.2 GB was free

The script can't do anything about the two in the middle, so it works on the first and the last. It watches for the apps you tell it to (DroidDeck by default), and while one of them is open:

  • Every 3 seconds it looks at the free memory and the size of the file cache. If free memory is low and enough new cache has piled up to be worth it, it writes 1 to /proc/sys/vm/drop_caches. That only drops clean cache, meaning copies of files that are still on storage, so nothing is lost.
  • If it was started with plus, it switches the parked RAM on as soon as the app starts. Qualcomm's driver does the same thing later by itself, and it parks the RAM again after you stop playing.

The rest of the time it sleeps and checks every 30 seconds whether one of the apps has been opened.

What has been tested

One device so far: an AYN Thor Max (16 GB, Android 13, firmware Thor_V1.0.0.377). About three hours of UNBEATABLE through DroidDeck with plus on, and Android did not close it once. Without the script the same game kept getting closed on loading screens. Installing, restarting, stopping, the report and autostart across a reboot were checked on that device too.

A 108 minute session: memory in use climbs to 14.5 of 16 GB while the script keeps the file cache small, nothing is closed

This is the longest logged session. The game and the system climb to 14.5 of the 16 GB while the script keeps cutting the file cache back, and nothing gets closed. The dashed line is where the same game used to get closed without the script: at about 11 GB in use, as read off the RAM display in AYN's own panel. Every logged session is charted in research/FINDINGS.md.

That is all the testing there is. Other AYN models and RAM sizes other than 16 GB have not been tried. The script should start on any AYN handheld that has Run script as Root, but whether the limits fit is something only a report from such a device can tell. If you try it, please run dropcache_report.sh and attach dropcache_report.txt to an issue, even if everything works.

Status and log

dropcache_status.txt is rewritten every time you run one of the files above: one line with the state, then the last 25 log lines. dropcache_report.txt has the whole log.

Log line Meaning
start v8.0 16gb plus=1 uid=0 ... Started, as root, with plus. The second word is the package.
ram ... limits 16gb high ... apps ... RAM found, the limits in use (kB) and the apps watched.
note: ... Something to act on, such as a package that does not match the device's RAM.
lmkd line ... The free-memory level under which the low memory killer may close apps on this device. Measured only.
<app> started (pid N): onlined N blocks ... Plus switched N blocks of parked RAM on. onlined 0 means they were on already.
hb free ... filecache ... psi_full10 ... Once a minute: free memory, file cache, memory pressure, and two more measurements.
drop(cache), drop(low), drop(floor) The cache was dropped, by which rule, and what that freed.
Settings

Copy dropcache.conf.example to dropcache.conf in the same folder, remove the # in front of the line you want, and start again with dropcache_run_plus.sh or dropcache_run.sh. Delete dropcache.conf to go back to the defaults.

  • APPS lists the apps to watch by package name, separated by commas. The default is DroidDeck (com.droiddeck.launcher), the only one tested. Any other app that runs your games goes here the same way.
  • HIGH_MB, CAP_MB, LOW_MB, LOW_NEW_MB, FLOOR_MB, FLOOR_NEW_MB override single limits.
  • FAST_POLL=1 and REREAD_MB=128 switch on two optional refinements that have only had one short session on a device.

The file is read as plain KEY=VALUE lines and is never run as a script. A line the script does not understand is skipped and named in the log.

Troubleshooting
Problem What to do
No dropcache_status.txt appears The folder is not at Download/AYNRAMSCRIPT, or one of the three files that are the script itself was picked.
Status says start failed: could not copy the script The folder is incomplete. Unpack the zip again.
Log says note: these limits are for ... The package does not match the device's RAM. Take the one it names, or auto.
Stutter After a drop the game reads some files again, which can be felt. Compare with a session after dropcache_stop.sh.
The game still closed Run dropcache_report.sh soon after and attach dropcache_report.txt to an issue.
A script file was edited on Windows and no longer runs It was saved with Windows line endings. Unpack the zip again. dropcache.conf is fine either way.
Risks and limits
  • This is experimental. It has been tested on one device type, with one app (DroidDeck) and one game (UNBEATABLE).
  • You may notice small stutters. After a drop the game has to read some files from storage again. A few were felt in one test session.
  • Plus switches the parked RAM on with the same kernel operation Qualcomm's driver uses. It caused no problems in testing, but how the kernel reacts if the hardware ever fails that operation is not known.
  • A game that needs more memory than the device has will still be closed.
  • Autostart adds one line to /data/boot_start.sh, a file AYN's firmware runs at boot. That was confirmed on Thor firmware Thor_V1.0.0.377. Other firmware may not have that hook.
  • Run script as Root runs whatever file you pick with full rights, so only run files from this repository's Releases.
For developers
16gb/ 12gb/ 8gb/ auto/    the packages, each with an AYNRAMSCRIPT folder ready to copy to the device
tests/                    behaviour tests for all packages
research/                 real logs, charts and what they showed, a reading of lmkd's source, a memory model
tools/                    sync_packages.sh, make_release.sh
File in a package Role
dropcache_body.sh The background script: reads memory, decides, drops. Start reading here.
dropcache_tuning.sh Every number the script decides with. The one file that differs between the packages.
dropcache_ctl.sh Install, start, stop, report, autostart, uninstall. Never touches memory.
the other dropcache_*.sh The files the user picks: one command line each, calling dropcache_ctl.sh.

Run script as Root sends a file to AYN's root service one line at a time. Variables and the working directory do not carry over to the next line, and lines of 256 characters or more did not run correctly in testing. That is why each of those files is a single short line.

The tests run the real scripts under mksh, the shell Android uses, and play time and memory to them. They need Python 3 and mksh:

python3 tests/test_dropcache.py

Edit shared files in 16gb/, run sh tools/sync_packages.sh, then the tests. sh tools/make_release.sh builds the four zips.

Support

If the script saved you a few loading screens, you can buy me a coffee.

Buy me a coffee

Credits and licence

AynRamScript is MIT licensed, without warranty. AYN, Thor, Odin, Qualcomm and Snapdragon belong to their owners.

  • @reddraggone9 found that AYN's root service runs /data/boot_start.sh at boot, which autostart uses.
  • Memory offlining on Qualcomm devices is described in Sudarshan Rajagopalan's talk Dynamic DDR Memory Offlining at the Linux Plumbers Conference 2023.
  • drop_caches is documented in the Linux kernel documentation, lmkd in the Android documentation.
  • The lmkd source under research/lmkd/source is the Android Open Source Project's daemon with Qualcomm's changes (Apache-2.0), taken from the AOSPA mirror.
  • The tests use mksh, the shell Android ships as sh.

About

Keeps games on AYN handhelds from being closed by Android's low memory killer on loading screens. File cache control and optional parked-RAM onlining, started from AYN's built-in Run script as Root. No rooting, no PC.

Topics

Resources

Stars

18 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Contributors

Languages