[SCRIPT/DISCUSSION] Loopy Smoothness Tweak (Android's priorities) [02-02-2013]

Search This thread

lpy

Senior Member
Aug 12, 2010
250
147
LATEST UPDATE: 02.02.2013

A question that I'm often asked is:
"Can I have your permission to use your script in my ROM etc...?"

That answer to that is:
"
Yes it is ok to use the code. I don't really believe it's useful anymore, but if you can find a use, and possibly improve the code to make it work better, go for it."

Version 1.0.2

Download available for ClockworkMod: requires Root (Superuser/Busybox), and a kernel that supports "init.d" scripts (script files that are stored in /system/etc/init.d/).


Always have a backup before installing any modification. Even though this script is rather simple, I can't guarantee it will work on every ROM! It appears to freeze custom ROMs such as CM, I don't know why. Maybe someone else knows.

NOTE: This script appears to be less and less useful as newer ROMs and phones become more sophisticated in dealing with any sort of lag. Tested on I9000XWJW9 and it doesn't seem to help. This ROM is quite smooth anyway.

What this script hopes to achieve:

- Entering your PIN with minimal/no delay/lag
- Minimising delay with lock screen pattern/unlock (not the time it takes to wakeup, but the touchscreen response lag)
- Having smoother notifications on the status bar, and smoother pulldown menu response
- Minimising Launcher lag while Media Scanner is running (can scroll through screens without heavy pauses)
- Minimising lag when scrolling through the market while icons are still loading
- Installing/Uninstalling apps while continuing other tasks
- Minimising/eliminating intermittent lag spikes in gaming
- No delay in being able to answer phone calls
... you get the idea


#!/system/bin/sh
#
# Loopy Smoothness Tweak v1.0.2 - 02/02/2013 (http://xdaforums.com/showthread.php?t=1137554).
#
# An attempt at keeping the UI running a little smoother.
# Optimised for Galaxy S1 stock ROM, but can be modded for other Android devices. Backup recommended before running scripts.
# Not recommended for custom ROMs, such as CM. The "sleep" command appears to halt the OS, rendering the script useless.
# Feel free to edit how you wish, but please leave at least the first comment above as is.
# Any editing must be saved in UNIX (LF) format. Google "text editor unix lf" to read more.
#
However, as phones get faster, this script will probably have less of an effect.

Even with 2.3.6 and ext4, RAM scripts, governor tweaks, other tweaks, etc. the occasional lag still appears to be there, especially noticable when installing/unistalling, gaming, or even just scrolling through the launcher screens and menus. I decided to investigate. Being a linux newbie, I had to also source the appropriate commands.

The following has been discussed a little here and there on other Android forums, but surprisingly I couldn't find anything about it in the I9000 section...

Thinking of how processes in Windows can be run with six different priorities (Low, Below Normal, Normal, Above Normal, High, Realtime), I investigated to see what the deal was with Linux/Android. Turns out there are forty settings, ranging from -20 (Highest) to 0 (Default) to 19 (Lowest).

The next step was to see which process was causing the CPU bottleneck as it was happening. The top command is handy for this. I used this in adb shell (these tests were performed quite some time after reboot when there was craploads of stuff in the memory - the impact is not very noticeable after a fresh boot, or with very few apps installed/running.

UPDATE: The reason behind the majority of the kswapd0 issue was due to modified code of a custom kernel I was once using. I do however still find the script of benefit to me, and maybe you will too. You can still read below if you wish, but it may be irreverent.


Code:
adb shell
top -n 1
At the top of the list was:
18 16% R 1 0K 0K fg root kswapd0
kswapd0 is a kernel thread responsible for managing the memory.

I know Angry Birds can show a little lag from time to time, so I performed a little testing while playing that.

To change the priority of a running process, I used the renice command as such:
Code:
su
renice -20 `pidof kswapd0`
where -20 equals highest priority and `pidof kswapd0` returns the PID of the process, which is required by renice. In this case, 18.

To confirm the change, the ps command will give you what you need to know. Interesting to note "krfcommd" with a value of -10, and "mediaserver", -8.

Code:
ps -p kswapd0
If you enter "ps -p" all processes in memory are shown, along with their "nice" value.


It shouldn't take long before the game becomes barely playable at times (for a split second), as kswapd0 completely takes over the CPU until it performs its task. Setting the priority to
Code:
renice 0 `pidof kswapd0`
returns kswapd0 back to its default, and Angry Birds with the seldom lag. Let's go the other way now with
Code:
renice 19 `pidof kswapd0`
setting kswapd0 to its lowest priority, it should now have barely any impact on the game, if at all. Will this have negative impact on Android, though? Not sure yet.

We can even go one better and give Angry Birds all the CPU it needs via
Code:
renice -20 `pidof com.rovio.angrybirds`
With these settings in place, you could virtually play this all day, and your battery would probably be flat way before any lag becomes noticeable.
One problem though, when you close and reopen the game - assuming it's left the memory - it will have a new PID and the priority is set back to 0 (default).
Now, with my very limited knowledge of Linux/Android/Busybox, I'm not sure if there's a way to tell an app to open with a priority other than 0. Well, there is the nice command, but I couldn't get that working.

A common problem is the phone dialer app taking too long to popup when someone calls. Luckily, processes such as this one enter the memory from boot and seem to remain there. Setting its priority to -20 will basically tell the CPU to hold everything with less priority and open the phone dialer ASAP so you can take that call. And it works. I was on Angry Birds when a call came in, and was able to answer it on the first ring. I tried then calling myself without the -20 phone or the 19 kswapd0 settings, and there was an extra 2-3 second delay before I was able to answer. A few other apps that usually never leave the memory are default launcher (in my case, ADW Launcher EX), SMS/MMS app, Swype, Status Bar, and some others.

Even with apps launching at the default priority, with kswapd0 at 19, it still allows them more CPU, and may just be enough to combat this lag that people have tried to rid of with RAM scripts, Overclocking, and what not.

The script shown below is not the current init.d download script.

And the result (still a work in progress):

Code:
#!/system/bin/sh

###### Loopy Smoothness Tweak for Galaxy S (Experimental) #######

sleep 3
renice 18 `pidof kswapd0` # please give credit and thanks to loopy for this line if it works

##### Set nice levels for smoothness loop BEGIN #####
### Let's loop this in the background for a bit
for i in {1..20}
do
    sleep 5

    ### Phone dialer app ###
    renice -20 `pidof com.android.phone`

    ### Launcher apps ###
    renice -19 `pidof com.sec.android.app.twlauncher` # TouchWiz Launcher
    renice -19 `pidof org.adwfreak.launcher` # ADW Launcher Ex
    renice -19 `pidof org.zeam.core` # Zeam launcher

    ### System apps ###
    renice -19 `pidof com.android.mms` # Text message app
    renice -19 `pidof com.swype.android.inputmethod` # Swype keyboard
    renice -15 `pidof com.sec.android.app.controlpanel` # Task manager
    renice -15 `pidof com.android.systemui` # Status bar
    renice -9 `pidof com.android.settings` # Settings menu
    renice -9 `pidof com.android.browser` # Browser app
    renice -9 `pidof com.android.vending` # Market app
    renice -6 `pidof com.sec.android.app.camera` # Camera app
    renice -6 `pidof com.sec.android.app.fm` # FM Radio app
    renice -6 `pidof com.google.android.youtube` # YouTube app
    renice -6 `pidof com.google.android.apps.maps` # Maps
    renice -3 `pidof android.process.acore`
    renice -3 `pidof android.process.media`

    ### Memory management kernel thread ###
    renice 19 `pidof kswapd0` # please give credit and thanks to loopy for this line if it works
done
##### Set nice levels for smoothness loop END #####


What would be ideal is if programs such as launchers (TouchWiz Launcher etc..) had the ability to set its "nice" level upon launch, and have kswapd0 set real low, assuming it's safe to do so. If Android isn't doing this, why not? This isn't a good idea? Maybe it's only Samsung? If so, then... well, I won't ask why not... :p


NOTE: These commands/parameters vary depending on the su/busybox version installed.

Common SGS1 apk's:

com.android.phone # Phone
com.sec.android.app.dialertab # Dialer
com.sec.android.app.twlauncher # TouchWiz Launcher
com.android.mms # Messaging
com.swype.android.inputmethod # Swype keyboard
com.sec.android.app.controlpanel # Task manager
com.android.systemui # Status bar
com.android.settings # Settings menu
com.android.browser # Internet Browser
com.android.vending # Google Play
com.sec.android.app.camera # Camera
com.sec.android.app.fm # FM Radio
com.google.android.youtube # YouTube
com.google.android.apps.maps # Google Maps
com.android.phone # Phone


Common SGS3 apk's:

com.android.phone
com.android.contacts
com.sec.android.app.controlpanel
com.android.browser
com.android.mms
com.android.vending
com.android.settings
mediaserver
com.sec.android.app.camera
com.sec.android.app.videoplayer
com.sec.android.app.myfiles
com.android.browser
com.sec.android.app.camera
Quick FAQ:

I wanna see how it works without it again, how do I remove the script?
To remove, delete the file /system/etc/init.d/S00loopyst. If using Root Explorer, use Mount R/W before you attempt to delete. For adb, use the commands at the DOS prompt:
Code:
C:\>adb root
restarting adbd as root

C:\>adb remount
remount succeeded
Code:
[SIZE=2][SIZE=2]C:\>adb shell rm /system/etc/init.d/[/SIZE][/SIZE][SIZE=2]S00loopyst
[/SIZE][SIZE=2]

[/SIZE][SIZE=2]C:\>
[/SIZE]
For the Test versions (prior to v1.0.0):

Code:
[SIZE=2][SIZE=2]C:\>adb shell rm /system/etc/init.d/S_loopy_smoothness_tweak[/SIZE][/SIZE][SIZE=2]
[/SIZE][SIZE=2]

[/SIZE][SIZE=2]C:\>
[/SIZE]
How can I make this script run first/last?
Scripts in the init.d are run in alphabetical/numerical order, following the letter "S". For example, script "S_ramboost" would run AFTER "S00loopyst", because "r" is after "l". Renaming it to "S_1ramboost" will now run that script BEFORE "S00loopyst".

How do I edit the script and add apps I use?

Open the .zip file and extract the file S00loopyst from /system/etc/init.d/. Alternatively, if installed, you can edit the file directly from the phone's init.d folder. File must be saved in UNIX format.

So I can just edit the script and add apps I use?
You can, but there is no guarantee this will actually work. Very few processes remain in memory...there is a lot of chopping and changing going on in the background. You could alter the script's timing variables, but even then results are unpredictable with anything other than what I call the resident system processes. If you're able to improve the script in any way, let us know. :)

Will this work on a custom ROM such as CM/MIUI?
Probably not. The "sleep" command within the screen appears to run in the foreground as opposed to the background in a stock ROM, hence making the script useless. Not to mention it may prevent anything else loading.

Are the settings permanant?
Usually. Most apps remain in the memory, although some do come and go. If you use the Task Manager to clear apps from the memory, or use the "Clear memory" option, the apps will have lose their priority when removed from the memory.

Changelog:

v1.0.2a:

  • Added option for "low priority" tasks.
  • Early script finish if 2nd loop is complete before "number of checks" is complete.
  • More detailed log file.

v1.0.1:

  • Cleaned up some invalid code.
  • Tested OK on rooted Samsung GT-I9000 2.3.6 ROM.
  • Tested OK on rooted Samsung GT-I9305T 4.1.1 ROM.
  • Tested NOT OK on CM 9.1 GT-I9000 ROM. Phone freezes on boot until loop is complete.

v1.0.0:
  • User friendly - a lot easier to configure.
  • Total number of processes automatically calculated (used to shorten 1st loop if all apps are in memory).
  • Debug file created for troubleshooting (includes "renice" pass or fail test) - can be saved to /data partition or sdcard.
  • Simple "Aggressive" option to increase priority of resident and other apps.
  • In addition to kswapd0 checking before continuing, launcher app is also checked in a loop before continuing (usually no more than ~15 seconds after start-up).
  • oom_adj & oom_score settings for launcher to remain resident (doesn't seem to work too well... settings don't appear to "stick").

T2:
  • "Smarter" script.
  • Checks for kswapd0 every 3 seconds until found, and then 'nice' level is set.
  • 1st loop: checks for apps 60 times, at an interval of 3 seconds. Once an app is found in memory, 'nice' level is set, and it is not checked again during the loop. (Some apps enter and then leave the memory, which ain't helpful.)
  • 2nd loop: checks again 6 times at an interval of 5 seconds and sets 'nice' level.
  • Settings are a little more user-friendly.
  • Debug option (if kernel stores a log file).

T1:
  • Sets kswapd0 'nice' level after 3 seconds.
  • Pauses for 15 seconds, sets 'nice' levels for other apps (many of which don't remain in memory).


Download:

  • Download file and transfer to SD card
  • Enter ClockworkMod Recovery via Vol+Up/Menu/Power button combo
  • Select zip and install (remove any "Test" versions before using Version 1 or above)
 

Attachments

  • loopy_smoothness_tweak-T1-CWM.zip
    157.5 KB · Views: 2,873
  • loopy_smoothness_tweak_T2-CWM.zip
    158.4 KB · Views: 8,723
  • Screenshot_2012-10-14-22-16-46.jpg
    Screenshot_2012-10-14-22-16-46.jpg
    48.1 KB · Views: 4,512
  • loopy_smoothness_tweak-v1-0-1-STOCK-CWM.zip
    127.5 KB · Views: 1,401
  • loopy_smoothness_tweak-v1-0-2a-GT-I9000-STOCK-CWM.zip
    127.7 KB · Views: 580
  • loopy_smoothness_tweak-v1-0-2a-GT-I9300-STOCK-CWM.zip
    127.7 KB · Views: 2,543
Last edited:

petsasj

Retired Recognized Developer
Mar 22, 2010
1,605
1,501
35
Athens
I'm not a Linux/Android master myself, but this actually is food for thought. I'll try and do some reading, and i would really like to hear more experienced users's opinion about it.

Thanks for the explanatory post.
 
  • Like
Reactions: jader13254

lpy

Senior Member
Aug 12, 2010
250
147
Thanks for the feedback so far. I too would like to hear what more experienced users have to say, as I've only been using linux for a few days now and have quite a lot to learn.

The other good thing about this is, if it turns out to be a good idea, I'm guessing it can be used on basically any Android device.
 
  • Like
Reactions: joeyzbg

simone201

Inactive Recognized Developer
Sep 21, 2010
4,756
4,896
Lecco
A good place to start from...
Will take a look more....i am very interested on this and if u need help in scripts i can help u....

Sent from my GT-I9000 using XDA Premium App
 

zacharias.maladroit

Recognized Developer
interesting approach lpy !


FYI:

looping should be fine since it is also done, e.g. on the Galaxy Spica and CM7

but with different parameters



I have some ideas back in the back of my mind that might help with this issue (kernel-patches and other stuff)


Thanks !





edit:


related:

On Saturday, May 07, 2011 09:35:48 PM Ali Ahsan wrote:
> Hi All
>
> I have xeon server with 16 Gb Ram and no Swap memory.I am running
> cassandra server on two node in cluster.When there is high load on
> server kswapd0 kicks inn and take 100% cpu and make machine very slow
> and we need to restart out cassandra server.I have latest kernel
> 2.6.18-238.9.1.el5.Please let me know how can i fix this issue .Its
> hurting us badly this our production server any quick help will be
> appreciated.

There is more than one bug that causes this behaviour. A few related memory
managent situations (possibly responsible) may actually be avoided if you add
some swap (even if it's not used). My suggestion would be to add some swap,
set swappiness to 0 and see what happens.

/Peter
_______________

http://www.linux-archive.org/centos/523508-kswapd-taking-100-cpu-no-swap-system.html
 
Last edited:
  • Like
Reactions: burakgon

Chief_Death

Senior Member
Dec 31, 2010
163
18
Tried the script with go contacts,go sms, go launcher (edited ofcourse)
it seems to make them open instantly even when they aren't running in the background..

if some1 could make an app which make the app you use as -20 or something and if you close it or move to another one make it back to 0..
 

lttldvl

Senior Member
Feb 17, 2011
647
251
Inverurie
It would be really interesting to see this being controlled at Kernel level (if possible.)

I know that it contains the govenors to control the CPU and UV settings (smartass, conservative, etc.), but wouldn't it be awesome if you could determine a priority level for apps installed on your phone so that it removed a load of junk from memory?

I mean I know app developers would all start setting their apps "attention level" to -20, but if there could be an intelligent manager for this, it would make things alot faster I would think.
 
  • Like
Reactions: jerkysh

DAGr8

Inactive Recognized Developer
Feb 27, 2010
3,713
6,309
Montreal
It would be really interesting to see this being controlled at Kernel level (if possible.)

I know that it contains the govenors to control the CPU and UV settings (smartass, conservative, etc.), but wouldn't it be awesome if you could determine a priority level for apps installed on your phone so that it removed a load of junk from memory?

I mean I know app developers would all start setting their apps "attention level" to -20, but if there could be an intelligent manager for this, it would make things alot faster I would think.

that s the goal , ultimately all these task killers try to achieve problem as of today is it usually creates more problems then it solves ..

having a script in init.d folder or using gsript (market app ) to run script manually on a regular basis to ensure your apk have the proper level ..



@ OP : good thread :p
 
  • Like
Reactions: Nitro_123

matematic

Senior Member
May 17, 2006
149
12
that's really interesting approach but it has also some drawbacks. With nice you give a process more CPU time and in case that the application is not written properly you could end up with frozen system.
 

corgar

Senior Member
Oct 12, 2010
1,062
118
Rome
looping this script will eat my battery? i see it like a permament recursive process running in background.

phone will sleep properly after? cpu usage?
 

mr_at_sgs

Senior Member
Sep 27, 2010
552
25
Indeed there is a speedup if I use parts of your script. This is an interresting approach to gain your most important task faster. I often recognized a large latency if I opened phone or sms/mms. Is their a way to preload this apps and prevent its swapping out of ram?

Edit: recognized a reliable process when updating a lot of apps at once -> no update failed message any more
 
Last edited:

ykkfive

Inactive Recognized Developer
Jul 25, 2010
3,218
3,016
ykkfive.blogspot.com
using scripts should be fine and the resource consumption should not be a problem, if it is used properly

while using nice/renice will gain some benefits in controlling the priorities (i use nice too in some cases), i doubt the effectiveness if all thoese processes are set to low priorities

also, on my phone, the kswapd0 only consumes very little resources and it's not even in the top 10 in the top results but somewhere near 20
 

lpy

Senior Member
Aug 12, 2010
250
147
All good responses so far, and definitely making a lot of sense.

@simone201 many thanks re the script offer. There is no doubt this script can be improved, for example, having an 'if ... then' condition where once an app enters the memory, the "`pidof" changes from 0 (false) to a PID (true), and then the nice value can be set.

that's really interesting approach but it has also some drawbacks. With nice you give a process more CPU time and in case that the application is not written properly you could end up with frozen system.

Very true. -20 can be quite a dangerous setting if an app decides to play up.

I mean I know app developers would all start setting their apps "attention level" to -20, but if there could be an intelligent manager for this, it would make things alot faster I would think.

haha. I can see "priority wars" happening between developers and apps.

If Android doesn't take advantage of nice, I don't see why we can't. It's possible that in 12 months from now when many phones are dual core (or more?), this may have little impact, but for now this should prove very handy.

using scripts should be fine and the resource consumption should not be a problem, if it is used properly

while using nice/renice will gain some benefits in controlling the priorities (i use nice too in some cases), i doubt the effectiveness if all thoese processes are set to low priorities

also, on my phone, the kswapd0 only consumes very little resources and it's not even in the top 10 in the top results but somewhere near 20

I agree with all that, however, I don't really see it as a matter of "how much" it uses in terms of resources, but rather "when". My example with Angry Birds and a stock ROM, you will notice little glitches here and there, due to the work kswapd0 does. Lowering its priority still enables it to complete its task/s, but without interfering with real-time action.

Here are examples of some improvements I've noticed:

- Entering your PIN minimal/no delay
- Minimising delay with lock screen pattern/unlock (not the time it takes to wakeup, but the touchscreen response lag)
- Having smoother notifications on the status bar, and smoother pulldown menu response
- Minimising Launcher lag while Media Scanner is running (can scroll through screens without heavy pauses)
- Minimising lag when scrolling through the market while icons are still loading
- Installing/Uninstalling apps while continuing other tasks
- Minimising/eliminating intermittent lagging in gaming
- you get the idea..


The way I see it, at the very least, lowering the priority of kswapd0 already helps with the way everything runs. Setting an app's nice/renice value (if managed in a practical way) will be a bonus.
 
Last edited:

whaave

Senior Member
Mar 11, 2011
210
72
Just a quick note (uncertain if this is even related):

A couple of kernels ago (yes, i switch a lot) i noticed a large battery consumption by "Android System". This was running JVP, and DarkCore 2.7.3 i think... But it wasn't until I read this thread that I remembered that the reason I switched kernels was because of a "laggy" system. After a while the phone seemed a lot slower, and I had to reboot to get the initial speed.

Now with Galaxian kernel I have an extremely snappy phone, and the "Android System" drain is gone aswell....
 

.:Crack:.

Senior Member
Oct 13, 2010
1,504
381
How do you get the process name of an application (to edit the script)?

Edit\\ open app in app manager (astro)
 
Last edited:
  • Like
Reactions: Thunder22

h4m74ro

Senior Member
Aug 26, 2009
220
31
ドイツ
I think this is an important topic, but don't see much feedback from developers.

My question as a non developer is:
is there a way for me to use this? seems like the script is already there, so it could be possible to have a flashable zip or a tutorial because different apps/versions require a different script.
 

SDeba

Retired Recognized Developer
Feb 11, 2010
379
207
Warsaw
Cool thing. I remember something very similar to this when I was having HTC Dream. There was scheduling app which runs similar script like this ;) Will try it and post my opinion ;)
 

Top Liked Posts

  • There are no posts matching your filters.
  • 92
    LATEST UPDATE: 02.02.2013

    A question that I'm often asked is:
    "Can I have your permission to use your script in my ROM etc...?"

    That answer to that is:
    "
    Yes it is ok to use the code. I don't really believe it's useful anymore, but if you can find a use, and possibly improve the code to make it work better, go for it."

    Version 1.0.2

    Download available for ClockworkMod: requires Root (Superuser/Busybox), and a kernel that supports "init.d" scripts (script files that are stored in /system/etc/init.d/).


    Always have a backup before installing any modification. Even though this script is rather simple, I can't guarantee it will work on every ROM! It appears to freeze custom ROMs such as CM, I don't know why. Maybe someone else knows.

    NOTE: This script appears to be less and less useful as newer ROMs and phones become more sophisticated in dealing with any sort of lag. Tested on I9000XWJW9 and it doesn't seem to help. This ROM is quite smooth anyway.

    What this script hopes to achieve:

    - Entering your PIN with minimal/no delay/lag
    - Minimising delay with lock screen pattern/unlock (not the time it takes to wakeup, but the touchscreen response lag)
    - Having smoother notifications on the status bar, and smoother pulldown menu response
    - Minimising Launcher lag while Media Scanner is running (can scroll through screens without heavy pauses)
    - Minimising lag when scrolling through the market while icons are still loading
    - Installing/Uninstalling apps while continuing other tasks
    - Minimising/eliminating intermittent lag spikes in gaming
    - No delay in being able to answer phone calls
    ... you get the idea


    #!/system/bin/sh
    #
    # Loopy Smoothness Tweak v1.0.2 - 02/02/2013 (http://xdaforums.com/showthread.php?t=1137554).
    #
    # An attempt at keeping the UI running a little smoother.
    # Optimised for Galaxy S1 stock ROM, but can be modded for other Android devices. Backup recommended before running scripts.
    # Not recommended for custom ROMs, such as CM. The "sleep" command appears to halt the OS, rendering the script useless.
    # Feel free to edit how you wish, but please leave at least the first comment above as is.
    # Any editing must be saved in UNIX (LF) format. Google "text editor unix lf" to read more.
    #
    However, as phones get faster, this script will probably have less of an effect.

    Even with 2.3.6 and ext4, RAM scripts, governor tweaks, other tweaks, etc. the occasional lag still appears to be there, especially noticable when installing/unistalling, gaming, or even just scrolling through the launcher screens and menus. I decided to investigate. Being a linux newbie, I had to also source the appropriate commands.

    The following has been discussed a little here and there on other Android forums, but surprisingly I couldn't find anything about it in the I9000 section...

    Thinking of how processes in Windows can be run with six different priorities (Low, Below Normal, Normal, Above Normal, High, Realtime), I investigated to see what the deal was with Linux/Android. Turns out there are forty settings, ranging from -20 (Highest) to 0 (Default) to 19 (Lowest).

    The next step was to see which process was causing the CPU bottleneck as it was happening. The top command is handy for this. I used this in adb shell (these tests were performed quite some time after reboot when there was craploads of stuff in the memory - the impact is not very noticeable after a fresh boot, or with very few apps installed/running.

    UPDATE: The reason behind the majority of the kswapd0 issue was due to modified code of a custom kernel I was once using. I do however still find the script of benefit to me, and maybe you will too. You can still read below if you wish, but it may be irreverent.


    Code:
    adb shell
    top -n 1
    At the top of the list was:
    18 16% R 1 0K 0K fg root kswapd0
    kswapd0 is a kernel thread responsible for managing the memory.

    I know Angry Birds can show a little lag from time to time, so I performed a little testing while playing that.

    To change the priority of a running process, I used the renice command as such:
    Code:
    su
    renice -20 `pidof kswapd0`
    where -20 equals highest priority and `pidof kswapd0` returns the PID of the process, which is required by renice. In this case, 18.

    To confirm the change, the ps command will give you what you need to know. Interesting to note "krfcommd" with a value of -10, and "mediaserver", -8.

    Code:
    ps -p kswapd0
    If you enter "ps -p" all processes in memory are shown, along with their "nice" value.


    It shouldn't take long before the game becomes barely playable at times (for a split second), as kswapd0 completely takes over the CPU until it performs its task. Setting the priority to
    Code:
    renice 0 `pidof kswapd0`
    returns kswapd0 back to its default, and Angry Birds with the seldom lag. Let's go the other way now with
    Code:
    renice 19 `pidof kswapd0`
    setting kswapd0 to its lowest priority, it should now have barely any impact on the game, if at all. Will this have negative impact on Android, though? Not sure yet.

    We can even go one better and give Angry Birds all the CPU it needs via
    Code:
    renice -20 `pidof com.rovio.angrybirds`
    With these settings in place, you could virtually play this all day, and your battery would probably be flat way before any lag becomes noticeable.
    One problem though, when you close and reopen the game - assuming it's left the memory - it will have a new PID and the priority is set back to 0 (default).
    Now, with my very limited knowledge of Linux/Android/Busybox, I'm not sure if there's a way to tell an app to open with a priority other than 0. Well, there is the nice command, but I couldn't get that working.

    A common problem is the phone dialer app taking too long to popup when someone calls. Luckily, processes such as this one enter the memory from boot and seem to remain there. Setting its priority to -20 will basically tell the CPU to hold everything with less priority and open the phone dialer ASAP so you can take that call. And it works. I was on Angry Birds when a call came in, and was able to answer it on the first ring. I tried then calling myself without the -20 phone or the 19 kswapd0 settings, and there was an extra 2-3 second delay before I was able to answer. A few other apps that usually never leave the memory are default launcher (in my case, ADW Launcher EX), SMS/MMS app, Swype, Status Bar, and some others.

    Even with apps launching at the default priority, with kswapd0 at 19, it still allows them more CPU, and may just be enough to combat this lag that people have tried to rid of with RAM scripts, Overclocking, and what not.

    The script shown below is not the current init.d download script.

    And the result (still a work in progress):

    Code:
    #!/system/bin/sh
    
    ###### Loopy Smoothness Tweak for Galaxy S (Experimental) #######
    
    sleep 3
    renice 18 `pidof kswapd0` # please give credit and thanks to loopy for this line if it works
    
    ##### Set nice levels for smoothness loop BEGIN #####
    ### Let's loop this in the background for a bit
    for i in {1..20}
    do
        sleep 5
    
        ### Phone dialer app ###
        renice -20 `pidof com.android.phone`
    
        ### Launcher apps ###
        renice -19 `pidof com.sec.android.app.twlauncher` # TouchWiz Launcher
        renice -19 `pidof org.adwfreak.launcher` # ADW Launcher Ex
        renice -19 `pidof org.zeam.core` # Zeam launcher
    
        ### System apps ###
        renice -19 `pidof com.android.mms` # Text message app
        renice -19 `pidof com.swype.android.inputmethod` # Swype keyboard
        renice -15 `pidof com.sec.android.app.controlpanel` # Task manager
        renice -15 `pidof com.android.systemui` # Status bar
        renice -9 `pidof com.android.settings` # Settings menu
        renice -9 `pidof com.android.browser` # Browser app
        renice -9 `pidof com.android.vending` # Market app
        renice -6 `pidof com.sec.android.app.camera` # Camera app
        renice -6 `pidof com.sec.android.app.fm` # FM Radio app
        renice -6 `pidof com.google.android.youtube` # YouTube app
        renice -6 `pidof com.google.android.apps.maps` # Maps
        renice -3 `pidof android.process.acore`
        renice -3 `pidof android.process.media`
    
        ### Memory management kernel thread ###
        renice 19 `pidof kswapd0` # please give credit and thanks to loopy for this line if it works
    done
    ##### Set nice levels for smoothness loop END #####


    What would be ideal is if programs such as launchers (TouchWiz Launcher etc..) had the ability to set its "nice" level upon launch, and have kswapd0 set real low, assuming it's safe to do so. If Android isn't doing this, why not? This isn't a good idea? Maybe it's only Samsung? If so, then... well, I won't ask why not... :p


    NOTE: These commands/parameters vary depending on the su/busybox version installed.

    Common SGS1 apk's:

    com.android.phone # Phone
    com.sec.android.app.dialertab # Dialer
    com.sec.android.app.twlauncher # TouchWiz Launcher
    com.android.mms # Messaging
    com.swype.android.inputmethod # Swype keyboard
    com.sec.android.app.controlpanel # Task manager
    com.android.systemui # Status bar
    com.android.settings # Settings menu
    com.android.browser # Internet Browser
    com.android.vending # Google Play
    com.sec.android.app.camera # Camera
    com.sec.android.app.fm # FM Radio
    com.google.android.youtube # YouTube
    com.google.android.apps.maps # Google Maps
    com.android.phone # Phone


    Common SGS3 apk's:

    com.android.phone
    com.android.contacts
    com.sec.android.app.controlpanel
    com.android.browser
    com.android.mms
    com.android.vending
    com.android.settings
    mediaserver
    com.sec.android.app.camera
    com.sec.android.app.videoplayer
    com.sec.android.app.myfiles
    com.android.browser
    com.sec.android.app.camera
    Quick FAQ:

    I wanna see how it works without it again, how do I remove the script?
    To remove, delete the file /system/etc/init.d/S00loopyst. If using Root Explorer, use Mount R/W before you attempt to delete. For adb, use the commands at the DOS prompt:
    Code:
    C:\>adb root
    restarting adbd as root
    
    C:\>adb remount
    remount succeeded
    Code:
    [SIZE=2][SIZE=2]C:\>adb shell rm /system/etc/init.d/[/SIZE][/SIZE][SIZE=2]S00loopyst
    [/SIZE][SIZE=2]
    
    [/SIZE][SIZE=2]C:\>
    [/SIZE]
    For the Test versions (prior to v1.0.0):

    Code:
    [SIZE=2][SIZE=2]C:\>adb shell rm /system/etc/init.d/S_loopy_smoothness_tweak[/SIZE][/SIZE][SIZE=2]
    [/SIZE][SIZE=2]
    
    [/SIZE][SIZE=2]C:\>
    [/SIZE]
    How can I make this script run first/last?
    Scripts in the init.d are run in alphabetical/numerical order, following the letter "S". For example, script "S_ramboost" would run AFTER "S00loopyst", because "r" is after "l". Renaming it to "S_1ramboost" will now run that script BEFORE "S00loopyst".

    How do I edit the script and add apps I use?

    Open the .zip file and extract the file S00loopyst from /system/etc/init.d/. Alternatively, if installed, you can edit the file directly from the phone's init.d folder. File must be saved in UNIX format.

    So I can just edit the script and add apps I use?
    You can, but there is no guarantee this will actually work. Very few processes remain in memory...there is a lot of chopping and changing going on in the background. You could alter the script's timing variables, but even then results are unpredictable with anything other than what I call the resident system processes. If you're able to improve the script in any way, let us know. :)

    Will this work on a custom ROM such as CM/MIUI?
    Probably not. The "sleep" command within the screen appears to run in the foreground as opposed to the background in a stock ROM, hence making the script useless. Not to mention it may prevent anything else loading.

    Are the settings permanant?
    Usually. Most apps remain in the memory, although some do come and go. If you use the Task Manager to clear apps from the memory, or use the "Clear memory" option, the apps will have lose their priority when removed from the memory.

    Changelog:

    v1.0.2a:

    • Added option for "low priority" tasks.
    • Early script finish if 2nd loop is complete before "number of checks" is complete.
    • More detailed log file.

    v1.0.1:

    • Cleaned up some invalid code.
    • Tested OK on rooted Samsung GT-I9000 2.3.6 ROM.
    • Tested OK on rooted Samsung GT-I9305T 4.1.1 ROM.
    • Tested NOT OK on CM 9.1 GT-I9000 ROM. Phone freezes on boot until loop is complete.

    v1.0.0:
    • User friendly - a lot easier to configure.
    • Total number of processes automatically calculated (used to shorten 1st loop if all apps are in memory).
    • Debug file created for troubleshooting (includes "renice" pass or fail test) - can be saved to /data partition or sdcard.
    • Simple "Aggressive" option to increase priority of resident and other apps.
    • In addition to kswapd0 checking before continuing, launcher app is also checked in a loop before continuing (usually no more than ~15 seconds after start-up).
    • oom_adj & oom_score settings for launcher to remain resident (doesn't seem to work too well... settings don't appear to "stick").

    T2:
    • "Smarter" script.
    • Checks for kswapd0 every 3 seconds until found, and then 'nice' level is set.
    • 1st loop: checks for apps 60 times, at an interval of 3 seconds. Once an app is found in memory, 'nice' level is set, and it is not checked again during the loop. (Some apps enter and then leave the memory, which ain't helpful.)
    • 2nd loop: checks again 6 times at an interval of 5 seconds and sets 'nice' level.
    • Settings are a little more user-friendly.
    • Debug option (if kernel stores a log file).

    T1:
    • Sets kswapd0 'nice' level after 3 seconds.
    • Pauses for 15 seconds, sets 'nice' levels for other apps (many of which don't remain in memory).


    Download:

    • Download file and transfer to SD card
    • Enter ClockworkMod Recovery via Vol+Up/Menu/Power button combo
    • Select zip and install (remove any "Test" versions before using Version 1 or above)
    3
    It is, but I don't think it helps anymore. I was playing around with I9000XWJW9 (and mngb-0.5.8 kernel), but I couldn't get it smoother than what it already is. To be honest, even though the phone is a lot slower than my GT-I9305, the animations seem smoother.. go figure.

    Anyway, if anyone can learn anything from the code, it's there for anyone to use, or share, which many have. Apart from that, it seems Samsung finally produced a smooth I9000 ROM, even if it took a couple of years :)
    2
    I will give it a try. This should work on cm10 with sema, right?
    Do I have to flash this right after updating a nightly- is it necessary?

    Edit: just installed it. But manually(copied to init.d folder) because flashing with cwm did not work. Guess i will have to do this after every rom update, but it is worth it. Faster media scanning, launcher usable from the first second, notification centre is also faster, Play store does not lag. Coool. Thh for this!
    2
    Hmm... kswapd should be irrelevant on most phones. It's responsible for paging memory in/out of the swap partition (pagefile in Windows terminology) - VERY few Android phones have any swap enabled at all.

    I should check my Infuse when I'm more awake.

    Changing priority of kernel processes can be dangerous... Plus I'm surprised kswapd is using much CPU at all unless you're using a ps/top variant that is including iowait percentages (time CPU is spending waiting for an I/O resource, NAND flash in the case of almost any Android phone). kswapd typically uses little to no CPU, but if you include iowait in its usage it'll be much higher apparent CPU usage.
    2
    Nice idea mate.

    Why you didn't decrease providers priority?

    I tested.

    When calendar.provider works = dungeon defenders start lagging.

    Than maybe we can decrease tham to 19. Background sync things can lag. We won't see them at all.

    And games will run fine.

    Sent from my GT-I9000 using xda app-developers app