Hi, thanks for checking out my thread and your interest in…
Defiant07’s Super Stupendous & Splendid SwapItOn Script.
Now that I have blatantly lied and oversold things, let me explicitly state that I am new to android, in general, and to bash, especially, so I know the code is far from elegant and efficient. This is my first public script so devs don’t laugh too hard and please offer any suggestions. Now on to the good stuff…
What will this script do?
What are the requirements?
- root access
- busybox: version does not matter so long as it has the following required applets - awk, cat, chmod, clear, cut, dd, df, echo, free, grep, id, mkdir, mkswap, mount, nohup, ps, rm, sed, sleep, swapoff, swapon, sysctl, tee, tr.
- init.d support (if you don’t have it search XDA there are a of couple of apps and numerous scripts that add this feature)
- a kernel capable of swap (to check if swap is supported in terminal enter Code:
storage named “/mnt/sdcard” as opposed to /sdcard0 or something elsefrom v3 forward the script is compatible with most storage locations/mount points
- note: one of the benefits of the method deployed by this script is that it does NOT require a special partition
Disclaimer & Warning:
Option 2 - Make a nandroid backup. Download the attached “Defiant07s_SwapItOn_v13” zip and install it via cwm, reboot, and in a terminal emulator input
su -c swapit
Connecting to PC via USB in Storage Mode - To avoid possible problems use one of the two commands to turn swap off, which are listed below in the uninstall procedure, before connecting to your pc via usb in storage mode.
Uninstall - First in terminal enter
swapoff <swapfile location>
su -c swapitoff
OR even easier from v8 forward you can do:
su -c unswapit
There are other apps and scripts that perform the same/similar function, why use this script?
What is ram swap?
A couple of warnings/cautions: Stuff recalled from the swap area will likely be slower than stuff recalled from normal ram. Also the constant reading and writing can degrade sdcard lifespan, but from what I have read the degradation is negligible enough that something else will break first or you will simply replace your device due to the pace of technology before the degradation is noticed.
What is swappiness and what is the best swappiness value to use?
Maybe some guidelines will help you choose what's right for you...
100 will aggressively swap things
60 is most kernels default
20 is what V6 SuperCharger sets it to
0 will swap only when oom (out of memory) takes effect (i.e. when your minfrees start kicking in)
so I guess a general rule of thumb would be...
use a value between 0-50 if you want maximize for performance (you will notice little or no lag when multitasking because less stuff will be being recalled from the swap area)
use a value between 51-100 if you want to maximize for hard gaming (you will notice some lag when starting games due to things being more aggressively swapped, but it will clear and ultimately the game will run smoother)
use this rule of thumb to guide your experimentation...hands on testing and experience are your best teachers...you can change your swappiness on the fly/without rebooting by using:
echo "XX" > /proc/sys/vm/swappiness
The init.d script seems to be running on boot being that swap is enabled, but the swappiness value I chose is not sticking?
Why do you give the init.d script such high priority?
For those interested in the technical details, here’s the -lt optimal code:
- forgot a few "\", which caused minor bugs in the Ran_SwapOn.log file created by the init.d script and would cause the user's new swappiness level setting to not be applied if they renamed the old init.d script and deleted the newly created "Z99SwapOn" script following rerunning the "swapit" script
- now compatible with all storage names (e.g. /sdcard, /sdcard0, etc.) - thanks to @zeppelinrox
- added precautionary check for swap capability - thanks to @zeppelinrox
- added precautionary check for existence of /etc/init.d (note: it does not check if it's working)
- added precautionary check to ensure user does not make a swap file larger than the available space
- cleaned up code: nested log file, removed unnecessary sleep commands, and minor changes to dialog
- added precautionary check for swappiness value to ensure user does not input an invalid value
- script now calculates and suggests a recommended swap file size based upon the user's accessible ram (by which I mean the value you'd get if you navigated to settings>>apps>>running and added the XXXmb used and XXX mb free values; note: the recommended size is half this sum)
- cleaned up dialog: it should be more readable for users of option 2 (i.e. in terminal) and for users with smaller screens (i.e. phone users) (added a space at the beginning of each line of dialog and limited number of spaces used in each line of dialog)
- remove dialog saying a 0 swappiness value "would be counterproductive" - I was misinformed via a post from some other forum...I've recently read from credible sources that in linux and ubuntu a zero value will still swap things, but only when oom/minfrees are hit
- code should now avoid all remount related errors using marginally modified code from V6 SuperCharger by @zeppelinrox
- should now be compatible with devices that lack an actual external sdcard (note: user will have no location choice and be forced to use /data) - thanks to @zeppelinrox
- new method for detecting storage location and some optimization of code - thanks to @zeppelinrox
- added V6 SuperCharger style 'power shift' scripts aka "Super Stupendous Switch Swap Scripts" (ahh...gotta love alliteration) for: swapon, swapoff, swappiness=0, swappiness=20, swappiness=40, swappiness=60, swappiness=80, swappiness=100
- improved dialog
- init.d script, Z99SwapOn, should no longer cause bootloops - thanks to @zeppelinrox (be sure to check out post #61 for Zep's credits and thank him as well as @dk_zero-cool and @gu5t3r)
- init.d script, Z99SwapOn, will now make the swap file just in case the users accidentally somehow deletes it - thanks to @zeppelinrox
- the swappiness applied by the switch swap scripts will now be applied during all furture reboots (previously the swappiness value was only applied to the current session and swappiness would revert to whatever value was selected in swapit following a reboot) - thanks to @zeppelinrox
- improved swap detection - thanks to @zeppelinrox
- switch swap scripts now in incriments of 10 (swap0, swap10, swap20...etc...swap100)
- improved precautionary check for swappiness value - it should catch typos involving non-numeric characters
- swapit will now ask user if detected storage location is copasetic just in case detected location is wrong or not to the user's liking
- better storage detection - thanks to @zeppelinrox for updated code and @famewolf for bug report and testing
- changed method of getting accessible ram - thanks to @zeppelinrox
- changed some of the info in Ran_SwapOn.log (instead of free, it now shows the output from cat /proc/swaps...this combined with the above change eliminates the usage of free, which should be good for users of Project Xtreme)
- fixed permissions on the switch swap scripts that were added to the pervious version - thanks to @eushaun99 for bug report
- improved dialog of switch swap scripts (swapiton and swapitoff now tell user "it may take a minute")
- added option to skip making the swap file if one is already detected - thanks to @eushaun99 for this suggestion
- added option to choose custom location - thanks to @famewolf for this suggestion
- added optional tweaks for dirty_background_ratio, dirty_ratio, vfs_cache_pressure, dirty_writeback_centisecs, and dirty_expire_centisecs. (if you want to know what these parameters control read relevant descriptions from THIS and pages 109-110 from THIS - the values I have chosen are intended as compromise between swap usage and ram usage - i.e. I am trying to set values that produce a reasonable level of disk writes so as to more effectively use swap, while still keeping enough stuff in ram so as to allow for smooth performance should stuff need to be quickly accessed)
- added switch swap script: unswapit, which will delete everything except itself and the base swapit script
- added closing note regarding procedure for changing swapfile location
- added option to skip closing notes, which I advise against if it is your first use or if you are updating...posts asking questions regarding information therein will be ignored
- added color
- improved switch swap scripts dialogs (it won't hit you all at once; added the action that has been undertaken; added note that the value will also be applied on boot)
- cleaned up code and other minor changes
- redid optional tweaks: user can choose values that are: (1) from ThunderBolt!; (2) a middle ground by me; or (3) from KickAssKernelizer - thanks to @pikachu01 for giving me permission to use some of the sysctl tweaks from ThunderBolt!...and, of course, thanks to @zeppelinrox as well for his generous/liberal usage policies
- less silly/improved precautionary check for swappiness value - thanks to @zeppelinrox
- added switch swap scripts: tweakstb, tweaksmid, and tweakskak, which apply the respective optional tweaks on the fly
- reworked/reordered/improved the core options
- added precautionary check for valid swap file size
- added busybox/required applet check
- added running as root check
- switched back to output from 'free' in Ran_SwapOn.log (was output from 'cat /proc/swaps'; Project Xtreme fixed/renamed its bin script)
- improved the displayed information: the standard out from dd, free, mkswap, and sysctl are now indented one space and added dividers to break-up the dialog into sections
- other minor changes
- fixed busybox test (aliased grep) - thanks to @eushaun99 for bug report
- improved busybox/required applet check: more efficient and all applets are aliased - thanks to @LENAROX for sharing THIS with me...it heavily inspired my code, which, of course, @zeppelinrox subsequently improved/made less dumb...TY TY TY.
- fixed root check (should work with all versions of busybox id) - thanks to @famewolf for bug report
- fixed blank count in init.d script if swapfile creation is skipped bug - thanks to @famewolf for bug report
- changed running as root check to method used in V6 Supercharger - thanks to @zeppelinrox for code and thanks to @psyntific for bug report
- added debugging mechanisms
- cleaned up code considerably: reduced redundancy by restructuring an if statement and usage of functions
- other minor changes
- added option to add an additional delay to the init.d script, Z99SwapOn: users who have switched their internal and external sdcards via the vold.fstab trick should enable this option so as to avoid potential problems caused by the actual external sdcard taking longer to mount - thanks to @inunxelex for bug report and testing
- added new, hip closing graphics...SwapItOn & SmokeItUp, lol :P
- other minor changes/cleaning of code
- redid the swapXXX switch swap scripts so as to eliminate usage of the swappinessvalue.txt file - thanks to @famewolf for the suggestion which inspired this solution to protecting the swappiness value
- due to the above change, added closing note: users should NOT rename or alter the content of the init.d script, Z99SwapOn, otherwise the swapXXX and tweaksXXX switch swap scripts will not function as intended and may even deleteriously effect Z99SwapOn
- tweakstb, tweaksmid, and tweakskak will now apply the respectives settings on boot in addition to the current session
- added new switch swap script: tweaksno (which will cause none of the optional tweaks to be applied on boot; settings in the current session will be unaltered, reboot is required to restore defaults)
- unswapit now actually does what is says it does...forgot the potential Boot and Debug log files
- other minor changes
- added ability to create up to three prioritized swap files - thanks to @hisname for the suggestion
- updated subsidiary scripts in accordance with the above change
- made it so the user does NOT have to run 'unswapit' before rerunning main script if they want to change the location, size, or priority of the swap file(s) - thanks to @hisname for the suggestion
- added /cache and /system as 'quick options' for swap file location
- added 'Function not implemented' error check
- added ouput from 'cat /proc/swaps' to content of /data/Ran_SwapOn.log so as to confirm the number, priority, and location/name of swap file(s)
- updated the precautionary check for swapfile size(s): padded it to ensure there will be at least 20 mb of free space in the swap file location(s) in order to avoid potential problems, especially if using /cache
- other minor changes
- fixed r/o issue in init.d script when using /system that caused swapon error - thanks to @eushaun99 for bug report
- added minfree option - thanks to @zeppelinrox for his usage policies and SuperMinFree Calculator & thanks to @killoid for the suggestion/feature request
- added switch swap scripts: 'changeminfree' (will change minfree values on the fly and for future boots) and 'nominfree' (will cause minfree values to not be applied to future boots, but will not change values for current session)
Much thanks to @zeppelinrox for his numerous suggestions/contributions and for all the great tricks contained in V6 SuperCharger.
Big thanks to @pikachu01 for giving me permission to use some of the sysctl tweaks from Thunderbolt!
Many thanks to @eushaun99 for his questions, suggestions, bug reports, and testing...you have helped move this project forward and have made the OP more informative.
Thanks times two to @famewolf, @psyntific, @inunxelex, @killoid, and @hisname for their suggestions, bug reports, and testing.
Muchas gracias to @LENAROX for his general help and inspirations.
Please hit THANKS and give a 5 STAR rating if you found this product of my labour useful…don’t be a total free rider. Also please post any comments and suggestions. Should you want to use or redistribute this script, even in an altered form, please feel free to do so; all I ask for in return is credit...gimme my props in the form of a shout out and/or a link to this thread.
peep my other script, TrimDropOff .