[SCRIPT][UPDATED 01.26.14] Enable Ram Swap & Create a Swap File with |=~>SwapItOn<~=|
##### 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?
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’)
storage named “/mnt/sdcard” as opposed to /sdcard0 or something else from 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.
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 input
su -c 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.
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
; 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.
OR even easier from v8 forward you can do:
su -c unswapit
and this will remove everything expect the base "swapit" script and /system/xbin/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:
echo "XX" > /proc/sys/vm/swappiness
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.
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
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
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
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
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
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.
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.
Code no longer fits in posts due to character limits. Click on the txt attachment for easy, quick viewing.
File Type: zip Defiant07s_SwapItOn.zip - [Click for QR Code] (593.8 KB, 60 views)
File Type: zip swapit_[NOT_FLASHABLE].zip - [Click for QR Code] (1.5 KB, 30 views)
File Type: zip Defiant07s_SwapItOn_v2.zip - [Click for QR Code] (593.8 KB, 37 views)
File Type: zip swapit_[NOT_FLASHABLE]_v2.zip - [Click for QR Code] (1.5 KB, 22 views)
File Type: zip Defiant07s_SwapItOn_v3.zip - [Click for QR Code] (594.3 KB, 48 views)
File Type: zip swapit_[NOT_FLASHABLE]_v3.zip - [Click for QR Code] (2.1 KB, 33 views)
File Type: zip Defiant07s_SwapItOn_v4.zip - [Click for QR Code] (594.5 KB, 52 views)
File Type: zip swapit_[NOT_FLASHABLE]_v4.zip - [Click for QR Code] (2.2 KB, 50 views)
File Type: zip Defiant07s_SwapItOn_v5.zip - [Click for QR Code] (595.1 KB, 54 views)
File Type: zip swapit_[NOT_FLASHABLE]_v5.zip - [Click for QR Code] (2.9 KB, 42 views)
File Type: zip Defiant07s_SwapItOn_v6.zip - [Click for QR Code] (595.7 KB, 52 views)
File Type: zip swapit_[NOT_FLASHABLE]_v6.zip - [Click for QR Code] (3.5 KB, 32 views)
File Type: zip Defiant07s_SwapItOn_v7.zip - [Click for QR Code] (595.8 KB, 140 views)
File Type: rar swapit_[NOT_FLASHABLE]_v7.rar - [Click for QR Code] (3.4 KB, 80 views)
File Type: zip Defiant07s_SwapItOn_v8.zip - [Click for QR Code] (597.5 KB, 94 views)
File Type: zip swapit_[NOT_FLASHABLE]_v8.zip - [Click for QR Code] (5.3 KB, 73 views)
File Type: zip Defiant07s_SwapItOn_v9.zip - [Click for QR Code] (598.5 KB, 15 views)
File Type: zip swapit_[NOT_FLASHABLE]_v9.zip - [Click for QR Code] (6.2 KB, 22 views)
File Type: zip Defiant07s_SwapItOn_v9_fix.zip - [Click for QR Code] (598.5 KB, 45 views)
File Type: zip swapit_[NOT_FLASHABLE]_v9_fix.zip - [Click for QR Code] (6.2 KB, 41 views)
File Type: zip Defiant07s_SwapItOn_v9_fix2.zip - [Click for QR Code] (598.5 KB, 208 views)
File Type: zip swapit_[NOT_FLASHABLE]_v9_fix2.zip - [Click for QR Code] (6.3 KB, 119 views)
File Type: zip Defiant07s_SwapItOn_v9_fix3.zip - [Click for QR Code] (598.6 KB, 80 views)
File Type: zip swapit_[NOT_FLASHABLE]_v9_fix3.zip - [Click for QR Code] (6.4 KB, 56 views)
File Type: zip Defiant07s_SwapItOn_v10.zip - [Click for QR Code] (599.3 KB, 165 views)
File Type: zip swapit_[NOT_FLASHABLE]_v10.zip - [Click for QR Code] (7.0 KB, 117 views)
File Type: zip Defiant07s_SwapItOn_v11.zip - [Click for QR Code] (600.0 KB, 793 views)
File Type: zip swapit_[NOT_FLASHABLE]_v11.zip - [Click for QR Code] (7.8 KB, 575 views)
File Type: zip Defiant07s_SwapItOn_v12.zip - [Click for QR Code] (601.7 KB, 126 views)
File Type: zip swapit_[NOT_FLASHABLE]_v12.zip - [Click for QR Code] (9.4 KB, 102 views)
Hit THANKS if I was redundant, did not help, or was generally disruptive and ignorant . Device & ROM Details:
whats about sdcard life,swap decrease the life of sd,s.
Yeah it will degrade the life of the sdcard, which is why I advise users to perform due diligence in the Disclaimer/Warning.
But from the research I did it seems that the degradation is negligible enough that something else in the device will break before the sdcard. Or even more likely the user will replace their device due to pace of technology before the degradation is really noticed.
Hit THANKS if I was redundant, did not help, or was generally disruptive and ignorant . Device & ROM Details:
One glance at any developer section of any device forum on XDA and youll find … more
21 Sep 2014
XDA Developers was founded by developers, for developers. It is now a valuable resource for people who want to make the most of their mobile devices, from customizing the look and feel to adding new functionality. Are you a developer?