FORUMS
Remove All Ads from XDA

[TUTE][TIPS] Android RAM Management

306 posts
Thanks Meter: 239
 
By mrhnet, Senior Member on 14th May 2014, 08:42 PM
Post Reply Email Thread
13th November 2015, 05:11 AM |#41  
creative21's Avatar
Senior Member
Thanks Meter: 334
 
More
Quote:
Originally Posted by |>/\nte

Very nice thread. Just one thing: Will I be able to modify some parameters via the kernel adiutor app (since it has an option for modifying ram management parameters like hidden_app and such stuff manually) and benefit from not closing an app after loading another? Also, I'm open to recommendations (I just checked out the thread).

yes, kernel auditor will work fine and you can use it's recommend values
The Following User Says Thank You to creative21 For This Useful Post: [ View ] Gift creative21 Ad-Free
 
 
6th December 2015, 11:25 PM |#42  
leonardoroza's Avatar
Senior Member
Thanks Meter: 77
 
More
Thanks

Enviado de meu Moto G usando Tapatalk
11th January 2016, 04:49 PM |#43  
asmat98's Avatar
Junior Member
Thanks Meter: 1
 
More
Hello friends. I have a question. In settings>apps>running are running services and cached processes. But there is some space (see pictures) that not shows. So how I can see that "hidden" processes? I want to know what is there, because when I use my phone 5-6 hours after reboot, that space (I showed it in picture) becoming bigger. Thank you.
i.imgur.com/IpolDvM.jpg
i.imgur.com/5m2DTFq.jpg
(sorry, I'm new in xda, I cant post links)
The Following User Says Thank You to asmat98 For This Useful Post: [ View ] Gift asmat98 Ad-Free
11th January 2016, 06:59 PM |#44  
Tomoms's Avatar
Senior Member
Flag Trieste
Thanks Meter: 2,835
 
More
Quote:
Originally Posted by asmat98

Hello friends. I have a question. In settings>apps>running are running services and cached processes. But there is some space (see pictures) that not shows. So how I can see that "hidden" processes? I want to know what is there, because when I use my phone 5-6 hours after reboot, that space (I showed it in picture) becoming bigger. Thank you.
i.imgur.com/IpolDvM.jpg
i.imgur.com/5m2DTFq.jpg
(sorry, I'm new in xda, I cant post links)

It's normal. That's used space just like the other portion of the blue bar. That kind of blue curly bracket is misleading.
11th January 2016, 09:20 PM |#45  
NVardan's Avatar
Senior Member
Flag U.S.S. Enterprise
Thanks Meter: 271
 
More
Quote:
Originally Posted by Tomoms

It's normal. That's used space just like the other portion of the blue bar. That kind of blue curly bracket is misleading.

Aren't there any apps which would be able to show that space used by system processes and maybe even terminate if needed?
The Following User Says Thank You to NVardan For This Useful Post: [ View ] Gift NVardan Ad-Free
18th January 2016, 02:15 PM |#46  
Iqballoon's Avatar
Member
Flag Bekasi
Thanks Meter: 10
 
More
nice guide, will try later

might help my freezing problem *crossed finger*
31st August 2016, 11:49 PM |#47  
Senior Member
Thanks Meter: 21
 
More
Quote:
Originally Posted by mrhnet

Android RAM Management

What's this thread about?
This is a brief account of some useful aspects of android memory management and what could be done to make it better or to suit our needs. This is arranged in two parts; A) RAM Management Lesson. B) RAM Management Tips. Whoever is familiar with the Android RAM management concepts can skip to the second part (2nd post). Please read and understand carefully before applying anything to the phone. I'm not responsible for any unwanted effects thereby. Credits belong to respective authors of any MOD/APP discussed here.


A) RAM Management Lesson

Android uses a different way of handling processes. Instead of killing every process after its activity ended, processes are kept until the system needs more memory. The idea is to give speed improvements if you start that activity again. But how/when does Android kill a process if it needs more memory and and which process to kill first?

This is managed by the LMK (Low Memory Killer) driver of Android. You may already know that every app/process in Android is assigned an oom_adj value, which indicates the likelihood of it being killed when an out of memory (OOM) situation occurs. More higher it's value, the higher likelihood of it getting killed. Valid range is -17 to +15. (if in the -17 range means it won't get killed). According to that, there are six groups (OOM groups), into which apps/processes are categorised:

1. Foreground app
2. Visible app
3. Secondary server
4. Hidden app
5. Content provider
6. Empty app

Basically these could be described as..


These groups are defined by oom_adj value limits, and apps would fall into one of those groups according to the oom_adj value assigned to that particular app. "Foreground apps" usually have an oom_adj value of 0 or less (so they are the least killable; i.e High priority). "Empty apps" have a higher oom_adj (they are killed early; i.e Low priority). Also, oom_adj value changes according to the state of the user app; it's 0 when the app is active in the foreground and assigned a higher value when the app goes to the background.

Why their "killability" differ? Apps belonging to these different groups (that have different oom_adj's), start to get killed at different levels of free RAM. These triggering RAM limits are defined by the LMK minfree values. Above 6 categories correspond with 6 RAM limits which are set in the LMK minfree. Eg: Stock Android 4.3 in our SP comes with the minfree values of 58,68,78,88,98,118. (these are in MB; see below how to check it). Practically what it means is, Empty apps will get killed when ram goes below 118mb, Content providers when it goes below 98mb, Hidden apps when it goes below 88mb and so on.. lastly starts killing Foreground apps when ram goes below 58mb. You may notice that this last value (58mb) is not desirable when using memory intensive apps like heavy games. The app might shutdown while we interact with it. It won't be a surprise if RealRacing3 would shutdown in the middle of a race with these minfree settings!

Notes:
1. In our SP (and newer kernels), oom_score_adj is used instead of old oom_adj. (oom_score_adj valid range is -1000 to 1000). But oom_adj is also maintained for compatibility I think.

2. It is said that there are many OOM process categories that are assigned different oom_adj priorities by the ActivityManagerService, but eventually all of those would be considered under above six slots/groups (according to oom_limits), for the purpose of killing by the LMK minfree triggers. Therefore, those six are the importatnt ones for normal users like us.


Now, to the practically important part...
# We can check the minfree values (also change them) and see the OOM groupings of apps/processes with this Memory Manager app easily.

a) LMK Minfrees:................... ......................................b) OOM groupings:

....


If we click on an app in the list and select 'more info', we can see it's oom_adj value. In my case, System UI has -12 (foreground), Home Launcher has 1 (visible group) etc..


# We can check these manually in a terminal too..
a) LMK Minfrees:
Give the following command (without quotes) in a terminal emulator or adb shell: "cat /sys/module/lowmemorykiller/parameters/minfree"
Code:
$ cat /sys/module/lowmemorykiller/parameters/minfree
15000,17532,20065,22598,25131,30263
** These are in pages; 1 page=4K. Therefore, converting these in to MB results in.. 58,68,78,88,98,118. (e.g: 15000 x 4 /1024 = 58,5938)

b) OOM_adj value of an app:
*This is not much useful. But nice to know where to get these values from.
E.g. take home launcher. Find out it's PID (process ID) like this.. (command with output posted)
Code:
$ ps |grep home
 u0_a26 1653 721 471408 78076 ffffffff 00000000 S com.sonyericsson.home
It's pid is 1653. To see it's oom_adj value..
Code:
$ cat /proc/1653/oom_adj
 1
It's 1 (foreground). You might get 6 (hidden). So, your home is easily killed than my home . See below why..

At the same time we can see the new oom_score_adj..
Code:
$ cat /proc/1653/oom_score_adj
 58
* To convert old oom_adj value to newer oom_score_adj..
oom_score_adj = (oom_adj x 1000)/17 (truncate the decimals). So, (1x1000)/17=58.823


*There's another value (0-1000) of oom_score (cat /proc/1653/oom_score), which is THE actual indicator of how likely a process will get killed. It changes according to the tunable oom_score_adj and other factors..? something like that.. forget it!


## The above mechanism could also be described according to what is mentioned in kernel source files, as below. Can skip if it's boring ..
It's from 'drivers/misc/lowmemorykiller.c' of kernel sources (4.3; .266)

If we take our phone values, "cat /sys/module/lowmemorykiller/parameters/adj" command returns: "0,58,117,235,529,1000". These are in (new) oom_score_adj values. If we convert them to (old) oom_adj values, these become "0,1,2,4,9,15". (eg:117x17/1000=1.989=2; The last value becomes 17, but oom_adj range ends with 15, so we take 15). Now, with our minfrees of 58,68,78,88,98,118, what it means practically is to kill processes with a oom_adj value of 0 or higher when the free memory drops below 58mb and kill processes with a oom_adj value of 1 or higher when the free memory drops below 68mb and so on... Therefore, it's clear that the adj values "0,1,2,4,9,15" (or score_adj values "0,58,117,235,529,1000") define the limits of each of 6 OOM slot described above.

Another point to note is what mentioned above in the kernel source file "..driver considers memory used for caches to be free..", which is described below.


What is "Free RAM"?
What's reported in many apps as "free ram" is actually not free/empty. Linux/Android always tries to utilise the whole ram in some way, so the ram is not wasted. Ram which is not used by active apps, is used for caching apps and for some buffers. These caches and buffers can release memory for apps when needed. We can see the ram usage in detail with this command.. "cat /proc/meminfo" [giving "watch cat /proc/meminfo" would refresh the output every 2 seconds].
Code:
$ cat /proc/meminfo
 MemTotal: 859764 kB
 MemFree: 26380 kB
 Buffers: 2008 kB
 Cached: 136600 kB
 SwapCached: 0 kB
 Active: 557312 kB
 Inactive: 70520 kB
 ...blah.. ..blah...
 ....
Reported "free ram" was ~150mb when this was taken. Out of that, most (135mb) is already cached. ["Free RAM"=MemFree+Cached]. Actual free is very little. So, increasing "free ram" makes the phone much snappier because most of that "free" part of the RAM is used for caching things.

-->> RAM Management Tips

Hey nice article but I want to know something I have a note 5 you know this machine is packing a 4gb I'm also running marsmallow custom rom but the problem is that I can only open 20 apps after that lkm minfree start killing it but I know this thing can handle more apps I also made build prop changes you mentioned but nothing happened and I have tried to change minfree on sys/module/parameters/minfree but after I change it when I try to apply the changes the values revert to the stock values .. I have downloaded a tons 9f minfree memory apps but none of then worked out for me!! Any suggestions my friend would be great

---------- Post added at 10:49 PM ---------- Previous post was at 10:42 PM ----------

Quote:
Originally Posted by creative21

yes, kernel auditor will work fine and you can use it's recommend values

For me doesn't work I have a note 5 running latest marsmallow firmware and when I try to change it , the values revert to the stock one I don't know why!!
5th December 2016, 12:02 PM |#48  
Senior Member
Flag Android
Thanks Meter: 322
 
More
Small addendum
First not all devices have the same OOM configuration. Post boot maybe some scripts may change the default Android settings.
Therefore you need to check some things to make sure what you are doing.
Android 2.x had OOM but different config. This is for Android 4.x Please correct me if I am wrong

First get your OOM ADJ levels right:

dumpsys activity oom
Code:
  OOM levels:
    SYSTEM_ADJ: -16
    PERSISTENT_PROC_ADJ: -12
    FOREGROUND_APP_ADJ: 0
    VISIBLE_APP_ADJ: 1
    PERCEPTIBLE_APP_ADJ: 2
    HEAVY_WEIGHT_APP_ADJ: 3
    BACKUP_APP_ADJ: 4
    SERVICE_ADJ: 5
    HOME_APP_ADJ: 6
    PREVIOUS_APP_ADJ: 7
    SERVICE_B_ADJ: 8
    HIDDEN_APP_MIN_ADJ: 9
    HIDDEN_APP_MAX_ADJ: 15
OOM ADJ levels 10,11,12,13,14 are also cached/hidden apps. Levels with "-" ADJ will never be killed.

So be sure to configure the OOM ADJ file correctly:

echo "0,1,2,4,9,15" > /sys/module/lowmemorykiller/parameters/adj
echo "6144,8192,10240,12288,14336,16384" > /sys/module/lowmemorykiller/parameters/minfree

Code:
0   FOREGROUND_APP    24MB
1   VISIBLE_APP           32MB
2   PERCEPTIBLE_APP    40MB
3
4   BACKUP_APP           48MB
5
6
7
8
9   HIDDEN_APP_MIN     56MB
10
11
12
13
14
15 HIDDEN_APP_MAX    64MB
All other levels take the value of the above defined level. So ADJ level 6 gets the MB value of ADJ 9.

(note: There can only be 6 OOM ADJ levels in the adj file?)

The ADJ file can contain OOM SCORE instead of the written OOM ADJ values: writing 0,1,2,4,9,15 will read 0,58,117,235,529,1000
OOM_SCORES go up with the OOM_ADJ level. So apps in the foreground (ADJ 0) will have OOM_SCORE like 50,
apps with ADJ 15 will have OOM_SCORE up to 1000.

Lets check when the launcher will be killed.

Situation A

With dumpsys activity oom we find
Code:
    Proc #23: adj=fore /FA trm= 0 742:com.anddoes.launcher/u0a130 (top-activity)
        oom: max=15 hidden=10 curRaw=0 setRaw=0 cur=0 set=0
        keeping=true hidden=false empty=false hasAboveClient=false
PID of launcher is 742 with OOM_ADJ of 0 (not home adj but 0 since it is foreground app at this moment).
Now see what the real OOM SCORE value is with cat /proc/742/oom_score That gives a value of 69.
(or use ps -o command)
It is foreground app now with score 69. So if allocatable mem < 24MB it might get killed.

Situation B

When launcher is not the foreground app we see: ps 742 -o

USER PID PPID VSIZE RSS OOM_ADJ OOM_SCORE WCHAN PC NAME
u0_a130 742 151 413800 60108 6 422 ffffffff 4020da70 S com.anddoes.launcher

Now lancher has HOME_APP adj of 6 with an OOM_SCORE of 422. So if allocatable mem < 56MB it is a candidate to get killed.

Note: OOM_ADJ 6 is not listed in adj config file. But ADJ 9 is. So ADJ 6 will take its level from 9 which is 56MB.

What process is killed first?

For every process (with a specific OOM_ADJ level) the OOM_SCORE is calculated/adjusted. Processes with higher OOM_SCORE will be killed sooner when allocatable (not free) RAM drop below given level.
Check all processes with ps -o and watch the OOM_SORE column.


Note: if you configure OOM options to minimal you can experience high cpu usage due to kswapd0 deadlock on no swap file device


Cheers
8th December 2016, 03:17 AM |#49  
Senior Member
Thanks Meter: 21
 
More
Hey nice article very interesting but I want to know something I have a s7 edge I want to take advantage of the 4gb capabilities what would be the best combination of the 6 states of ram ???
Post Reply Subscribe to Thread

Tags
android, fix, management, memory, ram

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes