[SCRIPT][UPDATED 01.26.14] Enable Ram Swap & Create a Swap File with |=~>SwapItOn<~=|

defiant07

Senior Member
Feb 26, 2013
410
496
0
[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?
  • 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:
    cat /proc/cpuinfo
    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.
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 input
Code:
su
swapit
or
Code:
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
Code:
swapoff <swapfile location>
or
Code:
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:
Code:
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:
Code:
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
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 :p.
 

Attachments

Last edited:

defiant07

Senior Member
Feb 26, 2013
410
496
0
Code no longer fits in posts due to character limits. Click on the txt attachment for easy, quick viewing.
Download History:
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)
:laugh::cool::p:victory::D;)
 

Attachments

Last edited:

defiant07

Senior Member
Feb 26, 2013
410
496
0
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.
 

zeppelinrox

Senior Member
Dec 21, 2010
9,374
21,587
0
IN THE FREAKIN' OP
What are the requirements?
  • root access
  • 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:
    cat /proc/cpuinfo
    and under ‘features’ you should see ‘swp’ or ‘swap’)
  • storage named “/mnt/sdcard” as opposed to /sdcard0 or something else
  • note: one of the benefits of the method deployed by this script is that it does NOT require a special partition
Nice... just a couple of ideas.

You can check for support with awk '/swap/||/swp/&&/eature/' /proc/cpuinfo
ie.
Code:
if [ "`awk '/swap/||/swp/&&/eature/' /proc/cpuinfo`" ]; then echo " Swapping is Supported"
else echo " No Swap for You! :p"
fi
Check the sdcard path and use the found path.
From V6...
Code:
if [ "`ls $EXTERNAL_STORAGE`" ]; then storage=${EXTERNAL_STORAGE#*mnt}
elif [ "`ls $EXTERNAL_STORAGE2`" ]; then storage=${EXTERNAL_STORAGE2#*mnt}
elif [ "`ls $USBHOST_STORAGE`" ]; then storage=${USBHOST_STORAGE#*mnt}
elif [ "`ls $SECONDARY_STORAGE`" ]; then storage=${SECONDARY_STORAGE#*mnt}
elif [ "`ls $PHONE_STORAGE`" ]; then storage=${PHONE_STORAGE#*mnt}
else storage="/sdcard"
fi 2>/dev/null
I remove the /mnt tho just to make the output look cleaner and it doesn't cause any problems anyway.

So then I use the $storage variable everywhere instead of /sdcard
 

-CALIBAN666-

Senior Member
Feb 24, 2012
6,734
4,283
253
39
Leverkusen,Rheindorf
Nice... just a couple of ideas.

You can check for support with awk '/swap/||/swp/&&/eature/' /proc/cpuinfo
ie.
Code:
if [ "`awk '/swap/||/swp/&&/eature/' /proc/cpuinfo`" ]; then echo " Swapping is Supported"
else echo " No Swap for You! :p"
fi
Check the sdcard path and use the found path.
From V6...
Code:
if [ "`ls $EXTERNAL_STORAGE`" ]; then storage=${EXTERNAL_STORAGE#*mnt}
elif [ "`ls $EXTERNAL_STORAGE2`" ]; then storage=${EXTERNAL_STORAGE2#*mnt}
elif [ "`ls $USBHOST_STORAGE`" ]; then storage=${USBHOST_STORAGE#*mnt}
elif [ "`ls $SECONDARY_STORAGE`" ]; then storage=${SECONDARY_STORAGE#*mnt}
elif [ "`ls $PHONE_STORAGE`" ]; then storage=${PHONE_STORAGE#*mnt}
else storage="/sdcard"
fi 2>/dev/null
I remove the /mnt tho just to make the output look cleaner and it doesn't cause any problems anyway.

So then I use the $storage variable everywhere instead of /sdcard
thats the soul of LINUX!!!!
 

nitephlight

Senior Member
Mar 16, 2012
275
100
0
nice work for sure, I'm a swapper myself, though i prefer swap partitioning due to old habits :p

my2cents: make sure your other vm settings do not conflict with the swappiness you intend to achieve (or perceive hehe). things like cache pressure and page cluster can affect the swapping, but modifying these settings in order to *achieve* swap performance is silly and risky. V6 as noted has a swappiness setting that needs override, and make sure to create a power shift profile for low minfrees - works well with zepp's multi tasking mod :)

Sent from my DROID BIONIC using Tapatalk 2
 
  • Like
Reactions: defiant07

defiant07

Senior Member
Feb 26, 2013
410
496
0
Nice... just a couple of ideas.

You can check for support with awk '/swap/||/swp/&&/eature/' /proc/cpuinfo
ie.
Code:
if [ "`awk '/swap/||/swp/&&/eature/' /proc/cpuinfo`" ]; then echo " Swapping is Supported"
else echo " No Swap for You! :p"
fi
Check the sdcard path and use the found path.
From V6...
Code:
if [ "`ls $EXTERNAL_STORAGE`" ]; then storage=${EXTERNAL_STORAGE#*mnt}
elif [ "`ls $EXTERNAL_STORAGE2`" ]; then storage=${EXTERNAL_STORAGE2#*mnt}
elif [ "`ls $USBHOST_STORAGE`" ]; then storage=${USBHOST_STORAGE#*mnt}
elif [ "`ls $SECONDARY_STORAGE`" ]; then storage=${SECONDARY_STORAGE#*mnt}
elif [ "`ls $PHONE_STORAGE`" ]; then storage=${PHONE_STORAGE#*mnt}
else storage="/sdcard"
fi 2>/dev/null
I remove the /mnt tho just to make the output look cleaner and it doesn't cause any problems anyway.

So then I use the $storage variable everywhere instead of /sdcard
Thanks those are great suggestions and I will implement them in the near future. I was also thinking of adding a check to make sure the user does not make the swap file larger than the space available and cleaning things up with nest files.

nice work for sure, I'm a swapper myself, though i prefer swap partitioning due to old habits :p

my2cents: make sure your other vm settings do not conflict with the swappiness you intend to achieve (or perceive hehe). things like cache pressure and page cluster can affect the swapping, but modifying these settings in order to *achieve* swap performance is silly and risky. V6 as noted has a swappiness setting that needs override, and make sure to create a power shift profile for low minfrees - works well with zepp's multi tasking mod :)

Sent from my DROID BIONIC using Tapatalk 2
Thanks man...those are good tips and it wouldn't be worth having if it didn't work along side zep's mods :p.

I was reading an article about how swap works in Linux today and it said from some kernel in 2.0.x series onwards there is no performance difference between a swap partition and swap file. Thus, since android in based on 2.6.x, I am assuming, perhaps wrongly since there was divergent development, that this lack of performance difference holds true for all versions of android.

=====

hate that 8 thanks a day thing...they should let you roll them over like phone minutes or something
 
Last edited:
  • Like
Reactions: eushaun99

defiant07

Senior Member
Feb 26, 2013
410
496
0
Released version 3.

Major Update:
v3
  • 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
EDIT:
Should anyone want to add a precautionary check to ensure user does not input an invalid swappiness value, you can add the following code beginning on line 74:
Code:
if [ "100" -lt "$desiredswappiness" ]; then
echo Oops...that is not a valid value...try again...Bye :p
exit 69
fi
I will add this to the next update if I find other changes that warrant a new release.
 
Last edited:

eushaun99

Senior Member
Feb 8, 2013
2,014
970
193
21
Subang Jaya
Hey @defiant07, so I tried your script, and as you know I'm new to this swap file thing. My question is, what would be the recommended swappiness value? I dunno if a too high value may degrade SD card faster, or a too low value may be useless?

Sent from my GT-P7500 using Tapatalk HD
 
  • Like
Reactions: defiant07

defiant07

Senior Member
Feb 26, 2013
410
496
0
Hey @defiant07, so I tried your script, and as you know I'm new to this swap file thing. My question is, what would be the recommended swappiness value? I dunno if a too high value may degrade SD card faster, or a too low value may be useless?

Sent from my GT-P7500 using Tapatalk HD
It depends on your preferences, which is why I let the user set its value. Logically, yeah higher values would accelerate degradation.

Maybe some guidelines will help choose what's right for you...

100 will aggressively swap things
60 is most kernels default
20 is what 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 (i.e. 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'll 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 the rule of thumb and experiment...you can change your swappiness on the fly/without rebooting by using:
Code:
echo "XX" > /proc/sys/vm/swappiness
where XX is your desired swappiness (you can do it with or without the quotation marks). This way you don't have to rerun the swapit script and wait for the swapfile to be created.

I personally use 100 and notice very little performance difference between when swap is off and when I have it on at 100, but I have a low ram device. I think your device has a decent amount of ram (724 mb accessible ram from what I see on google) so the difference maybe more noticeable...if you do not do a lot of hard gaming or really crazy multitasking I suppose I'd say you should stick to the lower end of your preference spectrum (maybe 0-20 or 50-70).
 
Last edited:

eushaun99

Senior Member
Feb 8, 2013
2,014
970
193
21
Subang Jaya
Thanks for yet another awesome explanation. :D

I've read your guideline a few times to understand fully, and I decided to try a swappiness value of 40, since I do play games quite often but not a hard gamer kinda type. In your script, there were some read only file system errors and the Z99SwapOn init.d script was empty. Here are the errors:
Code:
 Please wait while I create the swap file and boot script...
according to your specifications. It could take 1-5 minutes.
The required files will be created in /storage/emulated/legacy.

360+0 records in
360+0 records out
377487360 bytes transferred in 13.889 secs (27178872 bytes/sec)
Setting up swapspace version 1, size = 377483264 bytes
swapon: /storage/emulated/legacy/swapfile: Invalid argument
mkdir failed for /sqlite_stmt_journals, Read-only file system
/system/xbin/swapit[141]: can't create temporary file /sqlite_stmt_journals/mksh.GGHkBF7419: No such file or directory
BTW, I think you should put the guideline and the explanation you gave me in the tab 10.1 forum in the second post of the OP so that newbies like me :p can understand better and input a more reasonable swappiness value instead of banging in numbers and then say that their SD card broke or the script did nothing.



Sent from my GT-P7500 using Tapatalk HD
 
  • Like
Reactions: defiant07

defiant07

Senior Member
Feb 26, 2013
410
496
0
Thanks for yet another awesome explanation. :D

I've read your guideline a few times to understand fully, and I decided to try a swappiness value of 40, since I do play games quite often but not a hard gamer kinda type. In your script, there were some read only file system errors and the Z99SwapOn init.d script was empty. Here are the errors:
Code:
 Please wait while I create the swap file and boot script...
according to your specifications. It could take 1-5 minutes.
The required files will be created in /storage/emulated/legacy.

360+0 records in
360+0 records out
377487360 bytes transferred in 13.889 secs (27178872 bytes/sec)
Setting up swapspace version 1, size = 377483264 bytes
swapon: /storage/emulated/legacy/swapfile: Invalid argument
mkdir failed for /sqlite_stmt_journals, Read-only file system
/system/xbin/swapit[141]: can't create temporary file /sqlite_stmt_journals/mksh.GGHkBF7419: No such file or directory
BTW, I think you should put the guideline and the explanation you gave me in the tab 10.1 forum in the second post of the OP so that newbies like me :p can understand better and input a more reasonable swappiness value instead of banging in numbers and then say that their SD card broke or the script did nothing.



Sent from my GT-P7500 using Tapatalk HD
No problem man, I am glad to be of whatever assistance I can to you :).

Although I know you're smart enough to follow directions, just to double check you are running the script as root/superuser right?

Not totally sure why you are getting that invalid argument error for the swapon command. If you look in /system/bin and /system/xbin do you see a file named "swapon" in either location? If that is missing, you'll need to install a busybox that has it. I probably should add busybox to requirements and list the needed applets. But the mkdir and the temp file errors are related to /system not being remounted rw. Without that directory and temp file it cannot make the init.d script.

Does
Code:
mount -o rw,remount /system
mount /system as rw? Also please try
Code:
busybox mount -o rw,remount /system
an lmk if that remounts /system as rw.

And since it seems to have made the swapfile in /storage/emulated/legacy try turning swap on manually
Code:
swapon /storage/emulated/legacy/swapfile
does that return an invalid argument error?

BTW - I added it as FAQ and I plan to add my response to your previous inquiry as a FAQ as well...I will update the OP when I release v4 (it's a bit smarter and will calculate your recommended swapfile size)...need to test it first and wait for your reply as I may need to call busybox or use Zep's trick where he uses like ten different remount commands.

=====EDIT=====
nvm, just try v4...I got impatient and integrated Zep's solution. Although this will likely not solve that invalid argument error so lmk the answers to the swapon related questions.
 
Last edited:
  • Like
Reactions: eushaun99

defiant07

Senior Member
Feb 26, 2013
410
496
0
Released v4...minor update.

v4
  • 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
 

eushaun99

Senior Member
Feb 8, 2013
2,014
970
193
21
Subang Jaya
No problem man, I am glad to be of whatever assistance I can to you :).

Although I know you're smart enough to follow directions, just to double check you are running the script as root/superuser right?

Not totally sure why you are getting that invalid argument error for the swapon command. If you look in /system/bin and /system/xbin do you see a file named "swapon" in either location? If that is missing, you'll need to install a busybox that has it. I probably should add busybox to requirements and list the needed applets. But the mkdir and the temp file errors are related to /system not being remounted rw. Without that directory and temp file it cannot make the init.d script.

Does
Code:
mount -o rw,remount /system
mount /system as rw? Also please try
Code:
busybox mount -o rw,remount /system
an lmk if that remounts /system as rw.

And since it seems to have made the swapfile in /storage/emulated/legacy try turning swap on manually
Code:
swapon /storage/emulated/legacy/swapfile
does that return an invalid argument error?

BTW - I added it as FAQ and I plan to add my response to your previous inquiry as a FAQ as well...I will update the OP when I release v4 (it's a bit smarter and will calculate your recommended swapfile size)...need to test it first and wait for your reply as I may need to call busybox or use Zep's trick where he uses like ten different remount commands.

=====EDIT=====
nvm, just try v4...I got impatient and integrated Zep's solution. Although this will likely not solve that invalid argument error so lmk the answers to the swapon related questions.
Yes, it ran as root. There is swapon, swapoff, swapit files in /system/xbin. If I'm not mistaken, the mount command should be this, right?
Code:
mount -o remount,rw /system
BTW, I set TE to run
Code:
su
mount -o remount,rw /system
automatically each time I open TE. So I actually dunno why I have those errors. I'm using busybox 1.21.1-jb bionic if it matters. Maybe I should try another busybox? Oh yeah, the swapon command did not return any invalid argument error. I'll try v4 but you'll have to wait a few hours because I'm going out soon.

Edit: I tried v4, no more read only file system errors, but there is still the invalid arguments error.
Code:
Please wait while I create the swap file and
 boot script according to your specifications.
 It could take 1-5 minutes.

swapoff: /storage/emulated/legacy/swapfile: Invalid argument
360+0 records in
360+0 records out
377487360 bytes transferred in 40.941 secs (9220276 bytes/sec)
Setting up swapspace version 1, size = 377483264 bytes
UUID=861e05f3-9419-49df-93cd-644256fe4600
swapon: /storage/emulated/legacy/swapfile: Invalid argument
Does the invalid argument affect anything? Because overall I'm actually noticing a definite increase in performance.

Sent from my GT-P7500 using Tapatalk HD
 
Last edited:
  • Like
Reactions: defiant07
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone