How to make and use a swap partition on SD-card (SGS3)

How to make and use a swap partition on SD-card (SGS3)

Postby yank555 » 13 Mar 2013, 18:20

Before proceding, please consider learning from Garfield :

Image
(Thanx to MuertoKo for this educational image)

I have spend numerous hours making this mod ... and writing this OP, so please have the decency to at least read it before asking questions.


REMEMBER

FIRST OF ALL, do a Nandroid backup, as well as a backup of your sd-card content !

You're doing this at your very own risk, I'm not to be held responsible if something goes wrong :cool:


There's another little warning I need to add to this, using part of your sd-card as swap space will cause that part of your card to undergo lots of read/writes, thus shortening the life of that part of the card...

I've been using hard swap for quite a long time now, no issues to report, but don't tell me I didn't warn you :cool:

Now that said, let's get going ;)


In case somebody wants to check it out, here is the swap activation script I wrote (attached) as well as explanations on how to make it all work :

I. Hard swap (using a swap partition on the SD card)

(This is the alternative I recommend, rather than using a pagefile on internal memory, considering the SD card being worn to be cheaper than the internal memory to be worn)


Here is how to use this mod :

NB: This mod will only work if the kernel you use has swap support built in !

1) Have I mentiond to backup your system (Nandroid) and SD-card ?! Cause now would be a real good time to do so :D

==> While doing so, now would be a great time to read the F.A.Q. in post 3. Especially do read at least the first question !!

2) Partition your SD-card (Minitool Partition Wizard, 4ext, CWM...) - for a step by step documentation have a look here.
You can create as many partitions as you like/can, as long as all are primary partitions. This MOD will look for the swap partiton and find it even if it's not the last, though it's common practice for swap to be the last partition.

Just please don't create more than one swap partition, I don't know how the script would react if there were more than one ! You've been warned !


3) Download "Hard-Swap-Yank555.lu_Flasher-vx.y.zip" and put it onto your SD-card

4) Boot into recovery

5) Flash "Hard-Swap-Yank555.lu_Flasher-vx.y.zip" from your SD-card (like a ROM or kernel)

6) Reboot and enjoy !!

To check if all went fine, start a terminal and type "free", you'll need to see something different than 0 in your swap line, look at the attached print-screen ;)

Enjoy and please let me know how it worked for you !

NB: In case you have zram activated, I very strongly recommend to disable it, so you get all your RAM back as RAM.

If you want to know what the difference is between hard swap and zram, please read the F.A.Q..


----------8<----------8<----------8<----------8<----------8<----------8<----------8<----------8<----------8<----------8<----------8<----------8<----------8<----------

Here is how to completely remove this mod :

1) Download "Hard-Swap-Yank555.lu_Uninstall.zip" and put it onto your SD-card

2) Boot into recovery

3) Flash "Hard-Swap-Yank555.lu_Uninstall.zip" from your SD-card (like a ROM or kernel)

And all will be again as nothing had ever happend ;)

JP.

Image
Nexus 5 (aka. Hammerhead) 32Gb
Note 3 SM-N9005 32Gb Proudly eFused
SGS3 GT-I9300 32Gb
HTC Sensation XE
HTC HD2
TF300TG 32Gb

Image
Credits FAdrums !
User avatar
yank555
-----------
 
Posts: 3520
Joined: 11 Mar 2013, 20:51
Has thanked: 924 times
Been thanked: 4608 times

Re: How to make and use a swap partition on the SD-card

Postby yank555 » 13 Mar 2013, 18:21

Hard swap installer

Version 1.2 (19/01/2013)
  • busybox installation path is now not important anymore, it will work as long as it's somewhere in the PATH

  • Download link MD5 2c99b06d20b79e1b38d229c608a6a980

    NB: If hardswap is working for you now, no need to update, it won't change anything for you !

Version 1.1 (10/08/2012)
  • swap actication script name change for compatibility with v6 supercharger mod (simply reflash the mod installer to change the script name)
  • Still had no time yet to look into the Android Low Memory Killer Setting not sticking when set in the script, recommendations below still apply.
  • Setting Android Low Memory Killer too aggressively may lead to lags, especially on kernels 3.0.39 and more, I suggest trying a little less aggressive LMK settings :
    • 4Mb Foreground Apps
    • 8Mb Visible Apps
    • 16Mb Secondary Server
    • 32Mb Hidden Apps
    • 48Mb Content Providers
    • 64Mb Empty Apps

  • Download link MD5 8fb78afb97e1526d685348fed6d6baae

Version 1.0a (05/08/2012)
  • Problem getting an error flashing on some devices fixed (hopefully !)
  • Flasher v1.0 download removed
  • Had no time yet to look into the Android Low Memory Killer Setting not sticking when set in the script, recommendations below still apply.

  • Download link MD5 e8f2eb118aa3f6d8c067d55c9262589d

Version 1.0 (05/08/2012)
  • Initial MOD version
  • Setting Android Low Memory Killer didn't stick when set in the script, will have to look into that. I recommend the following settings (System Tuner can do that for you) :
    • 2Mb Foreground Apps
    • 4Mb Visible Apps
    • 5Mb Secondary Server
    • 8Mb Hidden Apps
    • 12Mb Content Providers
    • 16Mb Empty Apps

  • Download link MD5 5868cd1c6be67e8e9b0d8ec89640d3cf

Hard swap uninstaller

Uninstall (10/08/2012)
  • uninstaller updated to uninstall both old and new script (due to namechange in v1.1)

  • Download link MD5 b101bc5ffc0340f6bb60c05489791b27
Nexus 5 (aka. Hammerhead) 32Gb
Note 3 SM-N9005 32Gb Proudly eFused
SGS3 GT-I9300 32Gb
HTC Sensation XE
HTC HD2
TF300TG 32Gb

Image
Credits FAdrums !
User avatar
yank555
-----------
 
Posts: 3520
Joined: 11 Mar 2013, 20:51
Has thanked: 924 times
Been thanked: 4608 times

Re: How to make and use a swap partition on the SD-card

Postby yank555 » 13 Mar 2013, 18:21

Frequently Asked Questions

What is the benefit of this mod ?

    The thing is, we have a certain amount of RAM at our disposal, plus our devices are getting faster and faster, getting more and more cores, but not that much RAM increase.

    Let me illustrate my point :

    htc HD2 (my frist device to run Android) - 2009

    RAM : 448 MB usable
    CPU : 1 GHz Qualcomm Snapdragon (single core)

    SGS3 - 2012

    RAM : 778 MB usable
    CPU : 1.4 GHz quad-core Cortex-A9

    So CPU speed is up by way much more than RAM, and I'm not taking GPU into consideration, plus storage speed increase etc...

    Now those devices tend to be used in multitasking, having many apps running at the same time.

    What those need is :

    - CPU power to run on (we have plenty !)
    - RAM to fit in (we have little)

    Now even though an APP is running, not every bit of it is really needed, so those parts can be swapped out (put into swap) freeing these memory blocks and making room for more apps to fit in RAM.

    So what will this mod do for you : Allow for more multitasking

    Keep more apps running in RAM at the same time, making the device snappier when switching from one app to the other.

    One app won't run faster !

    If now you don't have swap, Android will have to gently ask the least used app to please close (save data, and quit) to free up RAM. Now when you get back to that app, Android will have to reload it, the app will start and reload it's data.

    You can see, this will take more time.
How do swap and Android Low Memory Task Killer get along ?

  • 1) What is swap ?

      Memory is divided into memory blocks that apps can use. In Android one memory block has 4096 bytes. Each memory block holds also the information when it has been used for the last time.

      Considering this, one single app will be using many blocks. Some of those the app will be using a lot, some it won't. Take a simple card game. When you're playing, you're not using the app's settings menu, thus not using any of the blocks that hold those parts of the app.

      Swap is a process that is used in most of modern operating systems. The virtual memory management system (that's swap !) will swap out all the blocks that were not used for a certain time, this "certain time" is influenced by swappiness and a few other parameters that we can change.

      The plus is that the memory blocks that have been swapped out will free up RAM to be used by other apps, thus allowing for more apps to run at the same time.

      On the other hand, if you need a swapped out block, that will cost extra time, since it's not located in memory any more but on the swap partition, so it will need to be reloaded into RAM (possibly by swapping out another block to make room for this).

      That should give you a glimpse on the benefits and the costs of using swap.

      That should also give you an idea of why LMK (= Android's own Low Memory Killer) settings matter, since if almost no free ram is available, a swap in will generally cost a swap out first the the swap in !

      Consider swap as a way to have only part of the apps in RAM instead of having all of it, and as such allowing for more apps to run at the same time.

      BTW, it does not add RAM, you can only add RAM by actually adding physical RAM, anyone telling you otherwise is either lying or does not know what she/he's talking about ;)
  • 2) What does Android Low Memory Killer do ?

      All the apps you run are put into different categories (as a degree of importance).

      Each category will have a number of memory pages (the 4Kb pages I spoke about above) limit, which as soon as free ram drops below of that limit, will make LMK start killing tasks of that category.

      So the higher the limits, the sooner it will start its blood bath and kill away, or put otherwise, the higher the limit, the more free RAM you will have at any time.

      ALMK is your guardian of free RAM.
  • 3) Now swap & ALMK ??? How can swap work if ALMK keeps killing away ??

      If you understood correctly the way those two work, you will have noticed they have two different triggers :

      - LMK : number of free memory blocks per app category
      - swap : time a block has not been used

      So swap will be filled over time, by unused blocks of apps running for a longer period of time (widgets, background apps mostly, but not exclusively).

      By filling swap, memory blocks are beeing freed, so more free RAM is available.

      If more memory blocks are freed, the LMK will be triggered later, since as long as it sees enough free blocks, it's happy and will do nothing.

    Hopefully this explains to everyone how this all works, and helps you understand what is what, and why some combos work and some just don't.

    In essence, don't use too low LMK values, as low memory will induce lag, because it will either require app kills (which need resources) or swap out / swap in operation (which also need resources, less though).

    High LMK settings will not impeed swap to work, it will be used less or it will take more uptime to be used the same, since less apps can reside in memory, but with swap and given LMK settings, you will always be able to run more apps than without swap at the same given LMK settings.
Can I umount / remove my SD card without powering off the phone when using this mod ?

    You can unmount / remount the SD card, since that will only unmount / remount the FAT/exFAT partition (e.g. the one you see in Windows and "/mnt/extSdCard").

    But you should by no means remove the SD card (even if unmounted) while your system is running !

    It would be as if removing part of RAM without prior notice while the system is running.

    Always power off the device before removing the SD card !
What tool can I use to partition my SD card ?

    I can recommend using Minitool Partition Wizard Home Edition, it is free and it is really good.

    You can download it from here

    Always backup your data before partitioning your card, your data will probably be lost !

    PS: Remember, all the partitions you create must be primary partitions, else neither Windows nor your SGS3 will recognise the SD card. If you make a mistake, don't worry, just repartition and you're good to go !
Swappiness 100 is too harsh for me, can I change that ?

    I set it to 100, max... This is what the mod will do for you. If you want to change that, just edit the following file :

    /system/etc/init.d/991swap

    And replace 100 whith any value between 0 and 100 here :

    Code: Select all
    logfile_location="/data"
    sdcard_device="/dev/block/mmcblk1"
    swap_partition=`/system/xbin/fdisk -l $sdcard_device | /system/xbin/grep swap | /system/xbin/awk '{print $1;}'`
    swappiness=100
    sdcard_wait=30


    Talking about swappiness, it might be worth reading this.
What size do you recommend for the swap partition ?

    I personally have used 256Mb on my previous devices and that was far more than enough. Since the SGS3 is a "bigger" device, I've granted it 512Mb (since I can spare them), and so far I have not seen much more than ~230Mb being used by the system, so I will stick with that.

    But you're free to use more, it will work and be available as swap, even if I don't believe an Android system will take any benefit from this.

    I know that on computer systems, Linux recommends using a swap of 1x - 2x the RAM size, but let's not forget, we're talking mobile device, not computer/server system.

    If you can prove me wrong here, I'll galdly change my point of view on this ;)
What is the speed of my SD card ? Will it be fast enough ?

    Obviously, the faster, the better ;) I would recommend a class 4 card at least.

    If you want to benchmark your card, be carefull, you might wrongfully test the internal SD card thinking it's the external. The internal card is faster, so you might be mislead here...

    In his post Yusei recommends using AnTuTu and change the sdcard location in AnTuTu settings (set it to "/mnt/extSdCard").
If the internal sdcard is faster, why not use that one ?

    It is not advisable to try to repartition intermal memory, it most certainly can be done, but the risk to brick the device is rather high, plus I don't know if anything would still be working, since everything relies on the current partition table. As I said, you could modify it, making one partitions smaller, keeping the same partition table logic and adding one last partition at the end, but I don't advise going there !!

    You could then use a swap file rather that a swap partition, and put that file on the internal sd card, that would work, I've tested that already.

    But ... using a swap means generating a potentially high amount of I/O operation on the storage that holds it.

    Since flash memory is subjet to fail at certain amount of I/O operations (very high amount, but nontheless lifespam is limited somewhere), I had rather my sdcard fails, than the internal storage since sd cards are easy to replace and way cheaper than our phone.
Why is this supposed to be better than zram ?

    I'm coming from the htc HD2, which I had for quite a long time, then went to an htc Sensation for a few months and now just got the SGS3.

    RAM, zram and swap have been a topic I've spend countless hours on !

    There are quite a few things I learned going that path, so I'd like to share some of the conclusions I have come to hoping they might be of help and shed some light on this :

    1) if there is anything our devices lack most, it's RAM, CPU / GPU power is far better then the amount of RAM we're given which impeeds us from really using that power, EVEN in a device like the SGS3 !

    Hence even though I'm happy to have an intl SGS3, I somewhat envy those that have an operator specific device in the US because those come with 2Gb of RAM, and that would have been a VERY clever thing to have in the intl. version as well.

    2) Android uses swap very poorly if swappiness is set to anything less than the max (100). Why ? Well because Android Low Memory Killer starts kicking processes before RAM gets too low, which doesn't motivate Linux to swap that much. Plus the default settings when it is supposed to start kicking in are set way too high, which again increases the bad use of swap even more.

    To check swappiness, type "cat /proc/sys/vm/swappiness" in a terminal. To set it to 100, type "echo 100 > /proc/sys/vm/swappiness" (as root, won't stick at reboot !).

    You can verify swap usage after using your phone a little, by opening a terminal and typing free. You will not see that much swap used.

    3) Using zram has proven to be a not so good idea imho. It will give you swap, yes, but as stated above, it is poorly used, at the extreme cost of eating up real RAM to give you that swap (and RAM is what is lacking most and we're looking to free up). CPU overhead here is really no issue, as stated in the wiki posted and quoted above.

    So instead of helping, it merely does to opposite, which some here have already found out by increasing zram things get worse.

    4) So for me the only way to go is keep the RAM as RAM as much as possible, while adding real swap so everything that clutters real RAM can be put somewhere else and free real RAM for other processes that need it.

    To do this I use a Linux swap partition on an sdcard. And yes, this will wear the sd card faster, since it's going to generate more I/Os on that part of the card. But by experience it's not that bad, I never have had an issue so far and been using swap since like forever. Plus the sdcard is a not so expensive add-on, which on top can easily be replaced as well !
Can I use this mod with zram enabled ?

    Yes you can, but you miss the whole point of this mod in that case, you're wasting precious RAM to hold a virtual swap partition, plus you use a real swap partiton.

    The point of this mod is to keep as much RAM available as possible, so I'd recommend to disable zram to take fully advantage of this mod.
I seem to have set up everything fine, I can manually activate swap, but swap is not automatically enabled on boot, what can I do ?

  • Hardswap MOD relies on busybox symlinks to the necessary commands to be installed in /system/xbin, so please check if this is the case for the setup you're running.

    If it's not setup like this, or if you're unsure, I recommend to install Stephen Stericson's Busybox app available free on Play Store and let it update busybox to the latest version. This will also insure it to be installed properly in /system/xbin.
  • You may also want to check if your kernel/ROM handles the execution of init.d scripts (residing in /system/etc/init.d). Even though most combo's do, some don't, and if the script is not executed at boot, swap will most obviously not be enabled.
Nexus 5 (aka. Hammerhead) 32Gb
Note 3 SM-N9005 32Gb Proudly eFused
SGS3 GT-I9300 32Gb
HTC Sensation XE
HTC HD2
TF300TG 32Gb

Image
Credits FAdrums !
User avatar
yank555
-----------
 
Posts: 3520
Joined: 11 Mar 2013, 20:51
Has thanked: 924 times
Been thanked: 4608 times

Re: How to make and use a swap partition on the SD-card

Postby 5lack » 15 Mar 2013, 18:05

Hello Yank! This is my first post here :) Nice to meat you :-p
I have finally decided to make a swap partition on my sd card and have done everything as you Doc says.
Now:
I am trying to download the Hard-Swap-Yank555.lu* software but it's unavailable... can you plz fix the Links that I could finish my swap partition?

Danke im Voraus! ;)
5lack
 
Posts: 4
Joined: 13 Mar 2013, 19:02
Has thanked: 1 time
Been thanked: 1 time

Re: How to make and use a swap partition on the SD-card

Postby yank555 » 15 Mar 2013, 18:28

Oops, sorry, it's fixed now ;)

JP.

Sent from my Sotmax V15 / Yank555.lu SGS3 U8 kernel v3.3a (Linux 3.0.68) powered Galaxy S3 i9300 using Tapatalk 2
Nexus 5 (aka. Hammerhead) 32Gb
Note 3 SM-N9005 32Gb Proudly eFused
SGS3 GT-I9300 32Gb
HTC Sensation XE
HTC HD2
TF300TG 32Gb

Image
Credits FAdrums !
User avatar
yank555
-----------
 
Posts: 3520
Joined: 11 Mar 2013, 20:51
Has thanked: 924 times
Been thanked: 4608 times

Re: How to make and use a swap partition on the SD-card

Postby 5lack » 15 Mar 2013, 18:57

Thx Man! It works very well!
Fast service from your side ;) respect!
Keep up the good work!!
5lack
 
Posts: 4
Joined: 13 Mar 2013, 19:02
Has thanked: 1 time
Been thanked: 1 time

Swap File

Postby hussein.karimi » 17 Mar 2013, 10:26

Hi Yank!

i just tried this app:
Code: Select all
https://play.google.com/store/apps/details?id=com.swapit.expander.de&hl=en

You don't have to buy it, its available in (Aptoide) market!!! :D

this app, makes a Swapfile.swp in your external SD & uses it as a Swap Partition :)
my question!: is it possible to make Swap file in our internal storage instead of to partition our SD-card?

also This Mod does the same:
Code: Select all
http://forum.xda-developers.com/showthread.php?t=1888081

but swap size in latest version is'nt correct :(

Thanks for Your Great Kernel & hard Work ;)
hussein.karimi
 
Posts: 2
Joined: 14 Mar 2013, 07:12
Has thanked: 3 times
Been thanked: 0 time

Re: Swap File

Postby yank555 » 17 Mar 2013, 10:39

hussein.karimi wrote:Hi Yank!

i just tried this app:
Code: Select all
https://play.google.com/store/apps/details?id=com.swapit.expander.de&hl=en

You don't have to buy it, its available in (Aptoide) market!!! :D

this app, makes a Swapfile.swp in your external SD & uses it as a Swap Partition :)
my question!: is it possible to make Swap file in our internal storage instead of to partition our SD-card?

also This Mod does the same:
Code: Select all
http://forum.xda-developers.com/showthread.php?t=1888081

but swap size in latest version is'nt correct :(

Thanks for Your Great Kernel & hard Work ;)


Have a read of the FAQ a few posts above, you'll get my point of view about using internel memory for swap.

In short, swapfile on SD, why not, on internal memory, no thanks for me.

JP.
Nexus 5 (aka. Hammerhead) 32Gb
Note 3 SM-N9005 32Gb Proudly eFused
SGS3 GT-I9300 32Gb
HTC Sensation XE
HTC HD2
TF300TG 32Gb

Image
Credits FAdrums !
User avatar
yank555
-----------
 
Posts: 3520
Joined: 11 Mar 2013, 20:51
Has thanked: 924 times
Been thanked: 4608 times

Re: How to make and use a swap partition on SD-card (SGS3)

Postby djluis48 » 20 Mar 2013, 15:26

Hey JP, i need a little help here. I managed to create a swap partition on my ExtSD, and the scrip finds its when i flash it on recovery, and everything goes well on flashing, but when i reboot back to the system, and type "free" on terminal, in the swap partition line everything is 0s, not like the screenshot you posted on OP. Why is that? Am i missing something?
djluis48
 
Posts: 6
Joined: 16 Mar 2013, 05:16
Has thanked: 2 times
Been thanked: 0 time

Re: How to make and use a swap partition on SD-card (SGS3)

Postby yank555 » 20 Mar 2013, 15:33

djluis48 wrote:Hey JP, i need a little help here. I managed to create a swap partition on my ExtSD, and the scrip finds its when i flash it on recovery, and everything goes well on flashing, but when i reboot back to the system, and type "free" on terminal, in the swap partition line everything is 0s, not like the screenshot you posted on OP. Why is that? Am i missing something?


Can be due to several things...

Can you post the following files :

/data/swap.0.log
/data/hardswap.log

That will help me get a glimpse at what might be the issue...

JP.
Nexus 5 (aka. Hammerhead) 32Gb
Note 3 SM-N9005 32Gb Proudly eFused
SGS3 GT-I9300 32Gb
HTC Sensation XE
HTC HD2
TF300TG 32Gb

Image
Credits FAdrums !
User avatar
yank555
-----------
 
Posts: 3520
Joined: 11 Mar 2013, 20:51
Has thanked: 924 times
Been thanked: 4608 times

Next

Return to Mods

Who is online

Users browsing this forum: No registered users and 1 guest