preface

This is a moot point, as users are eager to use applications that take up as little memory as possible. Although domestic Android devices are now more and more large memory. The low memory kill problem of ancient times is more difficult to reproduce now.

nature

But it’s not impossible to get more. Let’s start with the essentials. First, in Android, each device is already indicated in the /system/build.prop file how much memory the VIRTUAL machine can use. As follows:

Dalvik. Vm. Heapstartsize = 16 m dalvik vm. Heapgrowthlimit = 192 m dalvik vm. Heapsize = 512 m dalvik vm. Heaptargetutilization = 0.75 dalvik.vm.heapminfree=512k dalvik.vm.heapmaxfree=8mCopy the code

You can use the cat/vi command to open it in the shell to view your own device information. Notice Su permission is required.

In Android, a process belongs to a virtual machine, so the virtual machine is dynamically allocated memory in Android. Here are three key messages to note:

  • 1. Heapstartsize refers to the memory size of the application when it is started.
  • 2. Heapgrowthlimit specifies the maximum memory size that can be dynamically allocated.
  • 3. Heapsize refers to the maximum space available after the largeHeap attribute is configured for an application. (If you exceed this value, you will receive OOM.)

We concluded that if we need more memory on the device we need to configure Android in the application :largeHeap is true.

Viewing the application memory allocation:

In shell, enter dumpsys meminfo -a package name

The following questions arise:

  • 1. The maximum heapsize is 512M, what if I need to spend 1024m?
  • 2. If it’s dynamic allocation, what about other applications that steal all the memory?

How to solve:

Through the analysis, it is found that the domestic super app has been using a way, is to make the application multi-process. Sometimes it is quite common for an app to have as many as three or four processes. If you are interested, check the schedule for yourself.

How to write multiprocess applications and how to use interprocess communication is not the scope of this article, we will only talk about applying more memory. In essence, an Android application is one or more processes running in different virtual machines, and a process corresponds to its own virtual machine. The limit given by the ROM vendor is the maximum memory configuration for a single virtual machine. If we are multi-process, we are equivalent to multiple virtual machines. Naturally, more memory is allocated.

When will it be used?

When we have only one process, we suddenly need a lot of memory in a component or page. The performance of the situation is stalled, frame drop, OOM, etc. (Because you are allocating memory dynamically and freeing unnecessary resources)

Common scenarios are as follows:

WebView independent process, multimedia playback independent process, IO read and write, push service, long link service, etc

Note:

You don’t need to deliberately add multiple processes to your App, it’s not necessary.

The problem of multi-process is the communication between applications, that is, cross-process communication, which has been solved by available frameworks on the market.