Prior to doing a memory optimization, the use of memoryfile, which found some features of the memoryfile and a very trickly use of the method, so here to record
What's It
Memoryfile is a set of frameworks that Android introduced at the very beginning, which actually encapsulates the unique memory-sharing mechanism of Android Ashmem Anonymous shared memory, which simply means that Ashmem is registered as a special character device in the Android kernel, The Ashmem driver initializes a memory area in a custom slab buffer in the kernel, and then maps the requested memory to the user's process space through the mmap (via TMPFS), which allows the memory requested here to be used in the user process, in addition, One of the features of Ashmem is the ability to reclaim memory that is labeled "Unpin" when the system is out of memory, which is described later, and memoryfile can also be used by binder to share a memory area across processes for two processes. Since the entire process of applying for memory is no longer on the Java layer, it is clear that the memory used by the Memoryfile application does not actually consume Java heap memory.
Memoryfile exposed user interface can be said with his name, basically with our usual file read and write basically consistent, can also use InputStream and outputstream to read and write the operation:
| 1234 |
memoryfile memoryfile = new memoryfile(null, inputstream. Available()); memoryfile. Allowpurging(false); outputstream outputstream = memoryfile. Getoutputstream(); outputstream. Write(1024x768); |
Above can see allowpurging this call, this is said before the "pin" and "unpin", after setting the allowpurging to False, this memoryfile corresponding ASHMEM will be labeled "pin", This memory is not recycled even when the Android system is running out of memory. In addition, because the ASHMEM default is "Unpin", so the requested memory at a certain point in time may be recycled, this time is not allowed to read and write
Tricks
Memoryfile is a very trickly thing, because it does not occupy the Java heap memory, we can save some objects with Memoryfile to avoid GC, in addition, there may be a bug on Android:
In a system of 4.4 and above, if Memoryfile is used in the application, you can see the value of one more ashmem when Dumpsys Meminfo:
It can be seen that although the memory applied by Memoryfile does not count toward the Java heap, it does not count toward the native heap, but consumes ashmem memory, which is actually counted in the memory currently occupied by the app.
However, in a machine under 4.4, the memory requested using Memoryfile is not counted in the app's memory:
And here I also counted, is not counted into the native heap, in addition, this time to the system setup inside to see the process of memory consumption, can also be seen in fact does not count into Ashmem memory
This should be a bug of android, but I searched and did not search the corresponding issue, it may be a feature
In the famous fresco, they also use this bug to avoid the decode bitmap, the file bytes read into the Java heap, using the memoryfile, and take advantage of this bug and this part of the memory is not counted in the app, These correspond to the Gingerbreadpurgeabledecoder and Kitkatpurgeabledecoder in fresco, respectively. Fresco will use these two different decoder in the decode of the system below 4.4 and 4.4 in the picture.
From this place can be seen, using Memoryfile, under 4.4 of the system, can help our app to "steal" some memory, and can not be counted into the memory of the app
Summary
Here is a brief introduction to the basic principles and usage of memoryfile, and a memoryfile in a way to help developers "steal" memory, this is a very trickly method, Although the use of this block of memory under 4.4 does not count toward the process, but is not recommended for use, because when the allowpurging is set to False, the corresponding Ashmem memory area is "pin", then when the Android system memory is not enough, is not able to recover this area of memory, if not released for a long time, this is equivalent to the non-use of a large number of mobile phone memory and can not be recycled, which will certainly affect the stability of the system
Ask--One-click Programmer to answer the question artifact, one-man service for cattle, developer Programming Essentials official website: www.wenaaa.com
QQ group 290551701 gathers a lot of Internet elite, technical director, architect, project Manager! Open source technology research, Welcome to the industry, Daniel and beginners are interested in engaging in IT industry personnel to enter!
How to "Steal"android's memory? "