[SCRIPT][UPDATED 01.26.14] Enable Ram Swap & Create a Swap File with |=~>SwapItOn<~=|
peep my other script, TrimDropOff
.
##### Added Minfree Option #####
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?
It will do three things: (1) it will create up to three swap files that are a sizes specified by the user [note: user also has choices for swap file locations]; (2) it will create an init.d script that enables ram swap and sets the swappiness level to a user specified value during each boot process; and (3) it will create 'shift' scripts so as to facilitate turning swap on/off, changing swappiness on the fly, and changing other swap related parameters on the fly.
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
and under ‘features’ you should see ‘swp’ or ‘swap’)Code:cat /proc/cpuinfo
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:
Although this script works as it should on my device I am not responsible for bootloops, bricks, bad big booty b*tches, or the imminently impending zombie apocalypse. Furthermore, please perform due diligence and do some research about the costs and benefits of ram swap before using this script. In addition, please use good practice and make a nandroid backup before making any changes to your system.
Usage:
Option 1 - Make a nandroid backup. Download the attached “swapit_[NOT_FLASHABLE]_v13” zip, extract it wherever you want it, and run the script with root/superuser permission in ScriptManager or a similar app, although I have found other apps lacking/deficient. Read the script’s output/follow the script’s instructions, reboot, and you’ll be good to go. Note: Users without an actual external sdcard may have to use /data to avoid swapon/swapoff errors. Should users choose to use a custom location this may result in swapon/swapoff errors and I will NOT respond to posts regarding such.
Option 2 - Make a nandroid backup. Download the attached “Defiant07s_SwapItOn_v13” zip and install it via cwm, reboot, and in a terminal emulator inputorCode:su swapit. Read the script’s output/follow the script’s instructions, reboot, and you’ll be good to go. Note: Users without an actual external sdcard may have to use /data to avoid swapon/swapoff errors. Should users choose to use a custom location this may result in swapon/swapoff errors and I will NOT respond to posts regarding such.Code: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 enterorCode:swapoff <swapfile location>; example of swapfile location: /sdcard/swapfile. Now you can delete the “swapfile” and “swappinessvalue” files located in where you chose to put them (e.g. /sdcard, /sdcard0, or /data) as well as the “Z99SwapOn” script located in /etc/init.d and the “swapit” script located wherever you put it or in /system/xbin if you flashed it. Also delete "Ran_SwapOn.log" from /data and "swapiton" "swapititoff" "swap0" "swap10" "swap20" "swap30" "swap40" "swap50" "swap60" "swap70" "swap80" "swap90" and "swap100" from /system/xbin.Code:su -c swapitoff
OR even easier from v8 forward you can do:and this will remove everything expect the base "swapit" script and /system/xbin/unswapit.Code:su -c unswapit
Possible FAQ:
There are other apps and scripts that perform the same/similar function, why use this script?
Unlike apps there is no ram and cpu usage - it applies your settings during the boot process…basically all you need to do set it up once and then you can forget about it. Unlike all the similar scripts I’ve looked at this script takes user input for the swapfile size and swappiness value - the others use predefined sizes and values, which imo is silly since preferences and needs vary from user to user and from device to device. In addition, unlike many similar apps and scripts there is NO need for a special partition.
What is ram swap?
Swap basically uses a portion of your sdcard kinda like ram. On a more technical level, your device will keep what it can in its normal ram until it starts hitting your minfrees or even before it hits your minfrees depending on swappiness level (e.g. a swappiness of 100 will aggressively preemptively swap thing whereas a swappiness of 0 will only swap things when oom/minfree values start taking effect). Once it hits your minfrees, instead of killing stuff, or once it hits the limit determined by your other swap related settings (swappiness, pressure, etc.) it will start moving/"swapping" inactive blocks/pages of memory (i.e. background stuffs) to the swap area, which could be a file or partition (from what I have read there seems to be no performance difference between a file and partition). This frees up ram for active/foreground stuff, which is great for hard gaming imo, especially if you have a low ram device. If something that was moved/"swapped" to the swap area needs to be accessed it will be moved/"swapped" back in to normal ram.
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?
Swappiness controls the intensity/tendency with which things are swapped. The "best" swappiness value depends on your preferences, which is why I let the user set its value.
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:where XX is your desired swappiness (you can do it with or without the quotation marks). Or even easier use the 'shift' scripts that are created by v5 forward.Code: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?
The most likely cause of this is a conflicting init.d script. The solution is to search your other init.d scripts for “swappiness” and edit value accordingly.
Why do you give the init.d script such high priority?
I personally am a user of V6 SuperCharger (<<best script ever imo) and I found that when running this script at lower priority two things would happen: (1) minfree and adj values did not stick; and (2) logging was totally jacked up.This may not happen on your device so feel free experiment with renaming the init.d script so as to adjust its priority. Note: should you rename it and then later rerun the main “swapit” script to change your settings, upon completion you should delete the new “Z99SwapOn” script located in /etc/init.d [do not worry your new settings will still be applied by the old script] otherwise you will have duplicate scripts.From v11 forward, do NOT rename or alter the contents of Z99SwapOn. Doing so will cause the switch swap scripts to function improperly, which may have deleterious consequences.
For those interested in the technical details, here’s the -lt optimal code:
CODE IS NOW TOO LONG TO EMBED IN POSTS...SEE TXT ATTACHMENT IN SECOND POST
Changelog:
Code:[B][I]v2[/I][/B] [LIST] [*]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 [/LIST] [B][I]v3[/I][/B] [LIST] [*]now compatible with all storage names (e.g. /sdcard, /sdcard0, etc.) - thanks to [user=3357461]@zeppelinrox[/user] [*]added precautionary check for swap capability - thanks to [user=3357461]@zeppelinrox[/user] [*]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 [/LIST] [B][I]v4[/I][/B] [LIST] [*]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 [URL="http://forum.xda-developers.com/showthread.php?t=991276"]V6 SuperCharger[/URL] by [user=3357461]@zeppelinrox[/user] [/LIST] [B][I]v5[/I][/B] [LIST] [*]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 [user=3357461]@zeppelinrox[/user] [*]new method for detecting storage location and some optimization of code - thanks to [user=3357461]@zeppelinrox[/user] [*]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 [/LIST] [B][I]v6[/I][/B] [LIST] [*]init.d script, Z99SwapOn, should no longer cause bootloops - thanks to [user=3357461]@zeppelinrox[/user] (be sure to check out post #61 for Zep's credits and thank him as well as @[URL="http://forum.xda-developers.com/member.php?u=3147360"]dk_zero-cool[/URL] and @[URL="http://forum.xda-developers.com/member.php?u=4665716"]gu5t3r[/URL]) [*]init.d script, Z99SwapOn, will now make the swap file just in case the users accidentally somehow deletes it - thanks to [user=3357461]@zeppelinrox[/user] [*]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 [user=3357461]@zeppelinrox[/user] [*]improved swap detection - thanks to [user=3357461]@zeppelinrox[/user] [*]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 [/LIST] [I][B]v7[/B][/I] [LIST] [*]better storage detection - thanks to [user=3357461]@zeppelinrox[/user] for updated code and [user=320962]@famewolf[/user] for bug report and testing [*]changed method of getting accessible ram - thanks to [user=3357461]@zeppelinrox[/user] [*]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 [user=5113073]@eushaun99[/user] for bug report [*]improved dialog of switch swap scripts (swapiton and swapitoff now tell user "it may take a minute") [/LIST] [I][B]v8[/B][/I] [LIST] [*]added option to skip making the swap file if one is already detected - thanks to [user=5113073]@eushaun99[/user] for this suggestion [*]added option to choose custom location - thanks to [user=320962]@famewolf[/user] 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 [URL="https://www.kernel.org/doc/Documentation/sysctl/vm.txt"]THIS [/URL] and pages 109-110 from [URL="http://www.redbooks.ibm.com/redpapers/pdfs/redp4285.pdf"]THIS[/URL] - 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 [/LIST] [I][B]v9[/B][/I] [LIST] [*]redid optional tweaks: user can choose values that are: (1) from [URL="http://forum.xda-developers.com/showthread.php?t=1556013"]ThunderBolt![/URL]; (2) a middle ground by me; or (3) from [URL="http://forum.xda-developers.com/showthread.php?p=36794328#post36794328"]KickAssKernelizer[/URL] - thanks to [user=3599329]@pikachu01[/user] for giving me permission to use some of the sysctl tweaks from [URL="http://forum.xda-developers.com/showthread.php?t=1556013"]ThunderBolt![/URL]...and, of course, thanks to [user=3357461]@zeppelinrox[/user] as well for his generous/liberal usage policies [*]less silly/improved precautionary check for swappiness value - thanks to [user=3357461]@zeppelinrox[/user] [*]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 [/LIST] [I][B]v9_fix[/B][/I] [LIST] [*]fixed busybox test (aliased grep) - thanks to [user=5113073]@eushaun99[/user] for bug report [/LIST] [I][B]v9_fix2[/B][/I] [LIST] [*]improved busybox/required applet check: more efficient and all applets are aliased - thanks to [user=4629194]@LENAROX[/user] for sharing [URL="https://github.com/lenarox/avcore/blob/master/avcore.sh"]THIS[/URL] with me...it heavily inspired my code, which, of course, [user=3357461]@zeppelinrox[/user] subsequently improved/made less dumb...TY TY TY. [*]fixed root check (should work with all versions of busybox id) - thanks to [user=320962]@famewolf[/user] for bug report [*]fixed blank count in init.d script if swapfile creation is skipped bug - thanks to [user=320962]@famewolf[/user] for bug report [/LIST] [I][B]v9_fix3[/B][/I] [LIST] [*]changed running as root check to method used in [URL="http://forum.xda-developers.com/showthread.php?t=991276"]V6 Supercharger[/URL] - thanks to [user=3357461]@zeppelinrox[/user] for code and thanks to [user=4804849]@psyntific[/user] for bug report [*]added debugging mechanisms [*]cleaned up code considerably: reduced redundancy by restructuring an if statement and usage of functions [*]other minor changes [/LIST] [I][B]v10[/B][/I] [LIST] [*]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 [user=886367]@inunxelex[/user] for bug report and testing [*]added new, hip closing graphics...SwapItOn & SmokeItUp, lol :P [*]other minor changes/cleaning of code [/LIST] [I][B]v11[/B][/I] [LIST] [*]redid the swapXXX switch swap scripts so as to eliminate usage of the swappinessvalue.txt file - thanks to [user=320962]@famewolf[/user] 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 [/LIST] [I][B]v12[/B][/I] [LIST] [*]added ability to create up to three prioritized swap files - thanks to [user=4729636]@hisname[/user] 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 [user=4729636]@hisname[/user] 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 [/LIST] [I][B]v12_fix[/B][/I] [LIST] [*]fixed r/o issue in init.d script when using /system that caused swapon error - thanks to [user=5113073]@eushaun99[/user] for bug report [/LIST] [I][B]v13[/B][/I] [LIST] [*]added minfree option - thanks to [user=3357461]@zeppelinrox[/user] for his usage policies and SuperMinFree Calculator & thanks to [user=5107949]@killoid[/user] 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) [/LIST]
Credits:
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.
### Download 13. Added Minfree Option ###
peep my other script, TrimDropOff
Attachments
-
601.7 KB Views: 16,503
-
603.9 KB Views: 27,325
Last edited: