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

232 posts
Thanks Meter: 149
By lpy, Senior Member on 23rd June 2011, 06:59 PM
Post Reply Subscribe to Thread Email Thread
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


# Loopy Smoothness Tweak v1.0.2 - 02/02/2013 (
# 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.

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:
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.

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
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
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
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):


###### 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}
    sleep 5

    ### Phone dialer app ###
    renice -20 `pidof`

    ### Launcher apps ###
    renice -19 `pidof` # TouchWiz Launcher
    renice -19 `pidof org.adwfreak.launcher` # ADW Launcher Ex
    renice -19 `pidof org.zeam.core` # Zeam launcher

    ### System apps ###
    renice -19 `pidof` # Text message app
    renice -19 `pidof` # Swype keyboard
    renice -15 `pidof` # Task manager
    renice -15 `pidof` # Status bar
    renice -9 `pidof` # Settings menu
    renice -9 `pidof` # Browser app
    renice -9 `pidof` # Market app
    renice -6 `pidof` # Camera app
    renice -6 `pidof` # FM Radio app
    renice -6 `pidof` # YouTube app
    renice -6 `pidof` # Maps
    renice -3 `pidof android.process.acore`
    renice -3 `pidof`

    ### Memory management kernel thread ###
    renice 19 `pidof kswapd0` # please give credit and thanks to loopy for this line if it works
##### 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: # Phone # Dialer # TouchWiz Launcher # Messaging # Swype keyboard # Task manager # Status bar # Settings menu # Internet Browser # Google Play # Camera # FM Radio # YouTube # Google Maps # Phone

Common SGS3 apk's:

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:
C:\>adb root
restarting adbd as root

C:\>adb remount
remount succeeded
C:\>adb shell rm /system/etc/init.d/S00loopyst

For the Test versions (prior to v1.0.0):

C:\>adb shell rm /system/etc/init.d/S_loopy_smoothness_tweak

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.


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

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

  • 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").

  • "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).

  • 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 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)
Last edited by lpy; 14th November 2013 at 11:19 AM.
The Following 93 Users Say Thank You to lpy For This Useful Post: [ View ]
23rd June 2011, 07:08 PM |#2  
petsasj's Avatar
Retired Recognized Developer
Flag Athens
Thanks Meter: 1,566
Donate to Me
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.
The Following User Says Thank You to petsasj For This Useful Post: [ View ]
23rd June 2011, 07:11 PM |#3  
Overlord Pain's Avatar
Senior Member
Flag Ottawa
Thanks Meter: 38
Very interesting idea

Sent from my GT-I9000 using XDA Premium App
23rd June 2011, 07:20 PM |#4  
lpy's Avatar
OP Senior Member
Thanks Meter: 149
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.
The Following User Says Thank You to lpy For This Useful Post: [ View ]
23rd June 2011, 07:22 PM |#5  
Thanks Meter: 51
Donate to Me
this is something to be looked at
23rd June 2011, 10:53 PM |#6  
explodingboy70's Avatar
Recognized Developer
Flag Southwest Florida
Thanks Meter: 1,578
Donate to Me
I'd personally like to see more discussion on this and input from more experienced people as well so...

23rd June 2011, 11:04 PM |#7  
simone201's Avatar
Recognized Developer
Flag Lecco
Thanks Meter: 4,933
Donate to Me
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
24th June 2011, 12:44 AM |#8  
zacharias.maladroit's Avatar
Retired Recognized Developer
Flag Vienna
Thanks Meter: 2,228
Donate to Me
interesting approach lpy !


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 !




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.

Last edited by zacharias.maladroit; 24th June 2011 at 12:53 AM.
The Following User Says Thank You to zacharias.maladroit For This Useful Post: [ View ]
24th June 2011, 10:20 AM |#9  
Senior Member
Thanks Meter: 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..
25th June 2011, 04:37 PM |#10  
lttldvl's Avatar
Senior Member
Flag Inverurie
Thanks Meter: 255
Donate to Me
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.
The Following User Says Thank You to lttldvl For This Useful Post: [ View ]
25th June 2011, 04:49 PM |#11  
DAGr8's Avatar
Recognized Developer
Thanks Meter: 6,324
Donate to Me
Originally Posted by lttldvl

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
The Following User Says Thank You to DAGr8 For This Useful Post: [ View ]

Read More
Post Reply Subscribe to Thread

breakthrough idea, smoothness, tweak
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes