Friday, April 1, 2011

Why swap?

Why not?  ;)  If you think with 768MB of RAM on the thunderbolt you'd have way more memory than you'd ever need, then think again.  A lot of the memory is taken up by cache and buffers, which are also used pretty heavily with my tweaks.

For those of you that are new to Linux (or other UNIX variants), you'd probably heard the term "virtual memory".  Swap is pretty much that - a way to extend your physical memory.  Your OS moves the less accessed memory pages to slower, and cheaper storage medium.  This frees up RAM for things that require the speed.  And guess what? On almost all android devices you'll find tons of flash memory which are going to provide near RAM access speed for your swap space.

But setting up swap alone isn't going to do much.  In fact, it could do more harm than good.  You'd also want to adjust some OS/kernel related settings so your phone/device can use swap effectively.

You want to primarily look at two settings: vm.swappiness and minfree.

vm.swappiness is a no brainer.  The lower the number less swap the OS is going to use, the higher the more swap.  If you set it to 0 it's not going to use swap at all, 100 it's going to use a lot of swap.  If you set it to say 1 it would use swap only when absolutely necessary.  I recommend 40 on a 200MB swap file.

minfree is another tricky one.  To really get the full benefit of swap, I think you want minfree to "work less", ie. kill active and semi-active apps less frequently.  This will allow all of your frequently used apps to stay in RAM.  Something like this would work well:

echo "100,200,20000,20000,20000,25000" > /sys/module/lowmemorykiller/parameters/minfree

Finally, you'd need a kernel that supports swap.  Unfortunately if your bootloader is locked (ie. DX/D2) you're not going to get custom kernels that will support swap.  I've tested swap on some custom kernels on the Tbolt and the Gtab and it works well.  If you're running a kernel that supports swap, download and flash my zip to create and enable swap; the zip will also adjust swappiness and minfree accordingly if swap is enabled.

One final note on this is some technical stuff on why I chose to use internal flash for swap rather than the sdcard - internal flash performs better:
sh-3.2# hdparm -Tt /dev/block/mmcblk0p26


/dev/block/mmcblk0p26:
Timing buffer-cache reads: hdparm: HDIO_DRIVE_CMD: Inappropriate ioctl for device
  326 MB in 0.51 seconds = 653840 kB/s
Timing buffered disk reads:   52 MB in 3.01 seconds = 17683 kB/s
hdparm: HDIO_DRIVE_CMD: Inappropriate ioctl for device
sh-3.2# hdparm -Tt /dev/block/vold/179:33


/dev/block/vold/179:33:
Timing buffer-cache reads: hdparm: HDIO_DRIVE_CMD: Inappropriate ioctl for device
  218 MB in 0.51 seconds = 437047 kB/s
Timing buffered disk reads:   15 MB in 3.03 seconds = 5054 kB/s
hdparm: HDIO_DRIVE_CMD: Inappropriate ioctl for device

sh-3.2# time busybox dd if=/dev/zero of=/mnt/sdcard/test bs=1k count=100000
100000+0 records in
100000+0 records out


real    0m17.159s
user    0m0.070s
sys    0m2.710s
sh-3.2# time busybox dd if=/dev/zero of=/data/test bs=1k count=100000     
100000+0 records in
100000+0 records out


real    0m12.202s
user    0m0.100s
sys    0m2.960s

30 comments:

  1. Hey there Imoseyon!
    Great work as always; love it all. Few quick questions for ya:
    How's your script going to work on Droid X Gingerbread?
    And how would it work on my Nook Color? Similar hardware, yes?
    Thanks for doin' what you do.

    ReplyDelete
  2. hi frank, my mods should work on DX GB fine. It will only enable tweaks that it can though. For instance, it won't enable swap...

    As for Nook color, the zip probably won't work. If i have a unit to test, I can probably get it to work without much difficulty but I don't so there's that. ;)

    ReplyDelete
  3. So, what would I get if I ran your tweaks on my og Droid runing project elite 5.02 (gingerbread)?

    ReplyDelete
  4. Mainism, good question. Flash the zip and run "bash /data/imoseyon/checkimosey.sh" and see what it tells you. It probably won't hurt.

    ReplyDelete
  5. HOLY CRAP! IT WORKS ON MY RIG GREAT!!!

    I had decided to try before you responded so I fixed permissions from rom manager, did a complete nandroid b-up (overdue anyway), wiped dalvik, batery and cache from within rz recovery, flashed your zip, booted, re-fixded permissions, re-booted, re-wiped d,b,&c, and re-booted again.

    Probably more than I had to do, but that's what I did.

    This is the smoothest my d1 has ran on gingerbread!


    THANK YOU!!!

    I hope you don't mind, I am linking to this from the Project Elite thread!

    Oh, and did I say...

    THANK YOU!!!

    ReplyDelete
  6. Not at all. Glad to hear I can add D1 to the list. Thanks. :)

    ReplyDelete
  7. Well sick! I will shoot this on to my Droid 1 too. We'll see how well it works on ol' Gingy DX-- it's been giving me hell thus far without tweaking, so I don't even know what will happen.
    And sir if I had many dollars I would get you a Nook Color so fast.
    Unfortunately, I'm a college student. but I'm buying you a Nook Color in spirit ;)

    ReplyDelete
  8. how do you know if you're running a kernel that supports swap? the kernel im using is a chevyno1 kernel (2.6.32-lowV-1.1Ghz-ChevyNo1).

    ReplyDelete
  9. 5b934180-5e6e-11e0-95a0-000bcdcb5194, I think if /proc/swaps file exists then you're good. Don't forget that you need to create a swap file in order for my mods to enable it.

    ReplyDelete
  10. could you please tell me how to create a swap file? i just thought if i flashed the zip file then the mod would work. thanks in advance for your help! btw, i'm coming from a OG droid.

    ReplyDelete
  11. Try running:

    sh /data/imoseyon/swap.sh create

    ReplyDelete
  12. should i type "sh /data/imoseyon/swap.sh create" into terminal emulator? i tried doing that and got the error message "can't open /data/imoseyon/swap.sh"

    ReplyDelete
  13. i tried that command after i flashed your zip file.

    ReplyDelete
  14. hmmm you're on OG Droid? do you see any files in /data/imoseyon? Feel free to ping me directly at imoseyon@gmail.com

    ReplyDelete
  15. i actually dont see /data/imoseyon anywhere. could it be that i didn't properly flash the zip file? i renamed it to update.zip to be safe and flashed it using Clockworkmod recovery. papoose34328@gmail.com

    ReplyDelete
  16. imoseyon:

    pregp -f crond returns a process id, i assume that works

    /bash /data/imoseyon/checkimosey.sh returns "Swap is on (195.3M), so that means i created the swap file correctly i suppose :)

    is there anything else i need to do, or is the swapping enabled? i can also locate the 195.3M swap file in /data/imoseyon/swap

    ReplyDelete
  17. Loving the kernel so far. Running v0.9 (extreme). I was wondering how you can verify that the swap partition has in fact been enabled and running? Also, just to clarify, your enable zip both creates and enables the swap partition? I simply flash it through rom manager? Thanks for the work!

    ReplyDelete
  18. Patrick,

    "free" command will tell you whether and how much swap is being used.

    The enable zip will do both yup.

    ReplyDelete
  19. Hi imoseyon,
    Is your universal V 5.2 zip work on Droid 2 global?

    ReplyDelete
  20. pyae, honestly i'm not sure.. Give it a try and see what happens. I'd try the latest v6 though.

    ReplyDelete
  21. Im on 595 gb ran the v.6 checked the terminal emulator. "Bash/data...." and got not found I have jrummys busy box and I am on most recent version what should I do? Thanks.

    ReplyDelete
  22. Kyle, I should have v7.1 out this weekend. That should fix issues with GB leaks.

    ReplyDelete
  23. Can I use the swap on the imoseyon_leanKernel_v2.8.2GBtest4?

    Thx 4 the gr8 read...

    ReplyDelete
  24. Imoseyon. I am in love with your work. Not to mention your explinations. You are easily...one of he best developers I have ever had the privlige to come across. Thank you.

    ReplyDelete
  25. OK. I set up swap. But how do I access the minfree and VM settings?

    ReplyDelete
  26. I have been having issues applying the minfree settings with the last few ROMs I have been flashing. Everything goes OK but when I run bash /data/imoseyon/checkimosey.sh it keeps saying minfree tweaks not applied. I then run the scrpit that it wants me too and still nothing. Any thoughts?

    ReplyDelete