How to break through the 24 m memory limit and allocate more memory to the android Program

Source: Internet
Author: User

Android: Process

Define the name of the process where the activity is running. Generally, all components of an application run in the default process created for the application. The default process has the same name as the application package name. You can specify a different default process for all components by defining the "process" attribute of the <Application> element. However, any component can override the default process for multi-process operations.

If the specified name starts with ":", a new process dedicated to the application will be created. If the process name starts with a lowercase letter, the activity is granted permissions to run in a global process. In this way, different components of multiple applications can share one process to save resources.

 

Android supports multiple processes. The memory usage limit for each process is generally 24 mb. Therefore, when some memory-consuming operations such as processing high-resolution images are completed, A separate process is required to execute this operation (the preceding configuration can be used to perform this operation ). Even so, developers do not need to open multiple processes at will to consume user resources. (The memory limit is 16 MB, 24 MB, and 32 MB. The memory limit for very old models is 16 Mb. Please search for more details ..)

 

In addition, there are some other ways to bypass the memory limit and use more resources to complete your tasks, as shown in the following (to be practiced ):

How to work around Android's
24 MB memory limit

The android framework enforces a per-process 24 MB memory limit. On some older devices, such as the G1, the limit is even lower at 16 Mb.

What's more, the memory used by bitmaps is stored in the limit. For an application manipulating images it is pretty easy to reach this limit and get the process killed with an OOM exception:

E/dalvikvm-heap (12517): 1048576-byte external allocation too large for this process.
E/graphicsjni (12517): VM won't let us allocate 1048576 bytes
D/androidruntime (12517): Shutting Down VM
W/dalvikvm (12517): threadid = 1: thread exiting with uncaught exception (group = 0x4001d7f0)
E/androidruntime (12517): Fatal exception: Main
E/androidruntime (12517): Java. Lang. outofmemoryerror: bitmap size exceeds VM budget

This limit is ridiculously low. for a device, like the Nexus One, with 512 MB of physical Ram, setting the per-process memory limit for the foreground activity to only 5% of the Ram is a silly mistake. but anyway, that's how things are and we have to live
It-I. e. find how to work around it.

There are two ways to allocate much more memory than the limit:

One way is to allocate memory from native code. using the ndk (Native Development Kit) and JNI, it's possible to allocate memory from the C level (e.g. malloc/free or new/delete), and such allocations are not counted towards the 24 MB limit. it's true, allocating
Memory from native code is not as convenient as from Java, but it can be used to store some large amounts of data in RAM (even image data ).

Another way, which works well for images, is to use OpenGL textures-the texture memory is not counted towards the limit.

To see how much memory your app has really allocated you can use Android. OS. Debug. getnativeheapallocatedsize ().

Using either of the two techniques presented above, on a Nexus One, I cocould easily allocate 300 MB for a single foreground process-more than 10 times the default 24 MB limit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.