How to tweak Ram memory for Android

In the Android (and Linux) philosophy, free RAM is wasted RAM. The operating system will try to use all available RAM to increase its throughput. By design, Android tends to keep activities in RAM after the user quits them. This way it can speed up reopening them if needed. This behavior though can easily get the system in a state of free memory. Today, we will see how we can tune the Android RAM killer according to our requirements.

How does Low Memory Killer work?

Android manages low memory states using a Kernel module called Low Memory Killer (LMK). LMK is a process killer, designed to work with the Android Framework. Each process is assigned a special value by the Framework at the start, the value oom_adj (oom_score_adj on newer kernels). This value determines how important the process is to the system (its priority) and therefore how easily it can be killed. The values ​​vary between -17 and +15 (0 to 1000 for oom_score_adj). Higher values ​​are given to less important processes that are killed first. Furthermore, the LMK defines 5 different categories for processes, each containing processes with oom_adj / oom_score_adj in a specific range of values:

  1. Application Foreground Application
    application is currently displayed on the screen and running.
  2. Background apps
    use can see Applications The application may not be visible on the screen but is still running. They can be hidden behind overlays or have a transparent window.
  3. Service secondary server runs in the background and is necessary for the Application to function properly. This category includes Google Play Services, for example.
  4. Apps that hide apps
    The application is hidden from the user, but is running in the background.
  5. Content provider
    These are services that provide content to the system such as contacts provider, location provider, etc.
  6. Empty apps
    This category contains Apps that the user has quit, but Android keeps RAM. They don’t steal any CPU time or cause any power drain.

The empty Application category contains the processes with the highest oom_adj/oom_score_adj values ​​while the App foreground category has the lowest value. Also, each of the above has its own memory threshold, and when the free RAM drops below this threshold, LMK will start killing processes inside this category, starting with the lower priority ones. .

Android Activity Manager is responsible for assigning oom_adj/oom_score_adj values ​​to each process at runtime. Furthermore, the system assigns memory thresholds to each process type at startup. However, the category threshold can be changed directly through the SysFS kernel after boot. This makes room for configuring the Android Memory Management Subsystem according to individual needs.

How to tweak Android Low Memory Killer (LMK)

Tweaking the LMK parameters will have a bigger impact on lower end devices with 1 GB RAM or less as these devices are really stressful with multitasking on Android. To change the LMK parameter, the device needs to have root support.

Changing the LMK parameters can be done through the console by directly modifying the SysFS entry /sys/modules/lowmemorykiller/parameters/minfree . This needs data to be provided in the form of x1, x2, x3, x4, x5, x6, where each (x) is the memory threshold and x1 represents Foreground App, Visible App x2, Secondary Server x3, Hidden Apps x4, x5 Content Providers, and x6 Empty Apps. So registering a new set of thresholds for the LMK is as simple as entering the Terminal Application and writing the following commands:

su
echo x1, x2, x3, x4, x5, x6> /sys/modules/lowmemorykiller/parameters/minfree

replace each x in the last command with the required values. The LMK needs the parameters to be given as memory pages. On Android, a memory page is 4 KB in size. So, to set a memory threshold of 50MB for a category, the value (x) assigned to it should be 12800 [(50 * 1024) / 4 = 12800].

Fortunately, changing LMK parameters can also be done fairly easily using a Kernel Tuning Application that supports changing LMK parameters, like the open source Kernel Adiutor App:

Tips for configuring LMK . parameters

  • Assigning very high values ​​to the Foreground and Display categories can make the system unstable.
  • There is no danger of killing critical system processes by any configuration. Processes that are not killed will be given a special oomj_adj value, which registers them as LMK non-killable.
  • The values ​​assigned to the LMK are lost on reboot, so they need to be reassigned every time the device boots. Some Kernel Tuning Applications may automatically re-apply the changes at boot time.
  • Most Android modulators only change the thresholds of the Empty Apps and Hidden Apps categories, as these have the greatest effect on the responsiveness and multitasking of the device.
  • The memory threshold should be registered in ascending order from the Foreground Application to the Empty category of Applications. So the memory threshold of Foreground Application
  • Finding the right configuration for the LMK is a trial and error process. Each user has different usage needs and should configure the LMK according to them. For example, a gamer should not increase the memory threshold of Foreground Apps and Visible Apps a lot. Games are memory demanding applications and would be easily killed (even while playing) in this situation. A gamer can instead set the Foreground threshold to about 10MB, ensuring that the Game App won’t be killed so easily.
  • Although some of the universal configurations are listed below, there is great variability in their performance across different devices.
  • To get real results from any tweak on LMK, any other Mission Killers should be disabled first, as they interfere with it. Essentially, LMK is a kernel-based solution that does exactly the same job as the Killer Killer Apps (and can replace them entirely when configured correctly) without any extra costs. any fee.

Universal LMK Profile Example

Because the LMK configuration is considered as a set of memory thresholds for each process type of the LMK. The following is a list of some profiles, which are known to work on most devices. They should be considered a starting point for more adjustment. All values ​​are given in memory pages.

  • Aggressive

    Foreground Application: 6400
    Visible application: 7680
    Secondary server: 11520
    Hidden Application: 25600
    Content provider: 35840
    Drum Application: 38400

    This configuration will actively initiate the kill process at 150 MB of free RAM. With 100 MB it will start killing Hidden Apps.

  • Play heavy games

    Foreground Application: 2560
    Visible Application: 5120
    Secondary server: 11520
    Hidden Application: 25600
    Content provider: 35840
    Drum Application: 38400

    This profile works just like the Aggressive Profile, except that the Foreground and Visible Application categories. It ensures that, if a game uses a large amount of memory, it will not be killed by the LMK while it is running on the screen.

  • Light

    Foreground Application: 2560
    Visible Application: 5120
    Secondary server: 7680
    Hidden applications: 8960
    Content provider: 10240
    Drum Application: 12800

    The lightweight configuration makes it ideal for users who don’t do heavy multitasking. On devices with a small number of apps installed, this profile will trigger the LMK less often, saving some CPU cycles and reducing battery drain.

Tags:

We will be happy to hear your thoughts

Leave a reply

App New
Logo
Reset Password