What about the memory overflow when Android loads a large picture?

Source: Internet
Author: User
Tags config manual resource hosting

Try not to use Setimagebitmap or Setimageresource or Bitmapfactory.decoderesource to set a larger picture,

Since these functions finish decode, they are ultimately done through the createbitmap of the Java layer, which consumes more memory.

Instead, use the Bitmapfactory.decodestream method first to create a bitmap and then set it to ImageView source.

Decodestream's biggest secret is that it calls Jni>>nativedecodeasset () directly to complete decode,

There is no need to use the Java layer's CreateBitmap, which saves the Java layer's space.

If the config parameter of the picture is added to the read, it can be used to reduce the loaded memory effectively, thus preventing the throw out of the memory exception

In addition, Decodestream directly to the image to read the bytecode, will not be based on the machine's various resolutions to automatically adapt,

After using Decodestream, you need to configure the corresponding picture resources in hdpi and mdpi,ldpi.

Otherwise, in different resolution machines are the same size (number of pixels), the display of the size is not correct.

In addition, the following ways are helpful:

1. InputStream is = This.getresources (). Openrawresource (R.DRAWABLE.PIC1);

Bitmapfactory.options options=new bitmapfactory.options ();

Options.injustdecodebounds = false;

Options.insamplesize = 10; Width,hight is set to the original very

Bitmap BTP =bitmapfactory.decodestream (is,null,options);

2. if (!bmp.isrecycle ()) {

Bmp.recycle ()//Reclaim the memory occupied by the picture

System.GC ()//reminding system to be recycled in time

}

Here's the previous method:

Java code

1./**

2. * To read the picture of local resources in the most memory-saving way

3. * @param context

4. * @param resid

5. * @return

6. */

7. public static Bitmap Readbitmap (context context, int resid) {

8. Bitmapfactory.options opt = new bitmapfactory.options ();

9. opt.inpreferredconfig = Bitmap.Config.RGB_565;

Opt.inpurgeable = true;

Opt.ininputshareable = true;

12.//Get resource picture

InputStream is = Context.getresources (). Openrawresource (RESID);

Return Bitmapfactory.decodestream (is,null,opt);

15.}

================================================================================

Solution to the Android memory overflow

Turn from: http://www.cppblog.com/iuranus/archive/2010/11/15/124394.html?opt=admin

Yesterday in the simulator to put the picture on the gallery, java.lang.OutOfMemoryError:bitmap size exceeds VM budget exception, the image size exceeds the RAM memory.

Simulator RAM is relatively small, only 8 m memory, when I put in a lot of pictures (each more than 100 k), on the above reasons.

Because each picture is previously compressed, when put into the bitmap, the size will become larger, resulting in excess RAM memory, the specific solution is as follows:

Solve the problem of loading a picture memory overflow

Options Save picture size only, do not save picture to memory

Bitmapfactory.options opts = new Bitmapfactory.options ();

Scaling, scaling is difficult to scale by the prepared scale, its value indicates the magnification of the scale, the SDK recommended its value is 2 of the exponential value, the value of the assembly resulting in the picture is not clear

Opts.insamplesize = 4;

Bitmap bmp = null;

BMP = Bitmapfactory.decoderesource (Getresources (), mimageids[position],opts);

...

Recovery

Bmp.recycle ();

Solved by the way above, but this is not the perfect solution.

Through some understanding, learned as follows:

Optimizing heap memory allocations for Dalvik virtual machines

For the Android platform, the Dalvik JAVAVM used by its hosting layer can be optimized for the present performance, such as we might consider manual interference GC processing in the development of some large game or resource-consuming applications, The Settargetheaputilization method provided with the Dalvik.system.VMRuntime class enhances the processing efficiency of the program heap memory. Of course, we can refer to the specific principles of open source Engineering, here we only say the use of the method: private final static floattarget_heap_utilization = 0.75f; You can call Vmruntime.getruntime (). Settargetheaputilization (Target_heap_utilization) when the program is OnCreate.

Android heap memory can also be defined by its own size

For some Android projects, the main impact of performance bottlenecks is Android's own memory management mechanism, the current mobile phone manufacturers are stingy with RAM, for software fluency, RAM is sensitive to performance, in addition to optimizing the Dalvik virtual machine heap memory allocation, We can also force the definition of the memory size of our own software, and we use the Dalvik.system.VMRuntime class provided by Dalvik to set the minimum heap memory as an example:

Private final static int cwj_heap_size = 6* 1024* 1024;

Vmruntime.getruntime (). Setminimumheapsize (Cwj_heap_size); Set minimum heap memory to 6MB size. Of course, for memory crunch, it can be handled by a manual interference GC.

Bitmap set picture size, avoid memory overflow outofmemoryerror optimization method

It is easy to overflow memory when using bitmap in ★android, reported as following error: Java.lang.OutOfMemoryError:bitmap size exceeds VM budget

The main addition is this paragraph:

Bitmapfactory.options Options = new Bitmapfactory.options ();

Options.insamplesize = 2;

EG1: (Take pictures by URI)

Private ImageView Preview;

Bitmapfactory.options Options = new Bitmapfactory.options ();

Options.insamplesize = 2;//Picture width is the original One-second, that is, the picture is the original One-fourth

Bitmap Bitmap = Bitmapfactory.decodestream (cr

. Openinputstream (URI), null, options);

Preview.setimagebitmap (bitmap);

The above code can optimize the memory overflow, but it simply changes the size of the picture and does not completely resolve the memory overflow.

EG2: (Through the path to the picture)

Private ImageView Preview;

Private String filename= "/sdcard/dcim/camera/2010-05-14 16.01.44.jpg";

Bitmapfactory.options Options = new Bitmapfactory.options ();

Options.insamplesize = 2;//Picture width is the original One-second, that is, the picture is the original One-fourth

Bitmap B = Bitmapfactory.decodefile (fileName, Options);

Preview.setimagebitmap (b);

Filepath.settext (FileName);

★android also has some performance optimization methods:

First in memory, you can refer to the Android heap memory can also define the size and optimize the Dalvik virtual machine heap memory allocation

On the underlying type, because Java does not have an actual pointer, it still needs to be done with the help of NDK in terms of sensitive operations. Android123 prompts game developers, this is more interesting is that Google launched NDK may be to help game developers, such as OpenGL ES support has significantly improved, local code to operate a graphical interface is necessary.

Graphics object optimization, here is the Android Bitmap object destruction, you can use the Recycle () method to show the GC to reclaim a bitmap object, usually to a needless bitmap can be used in the following way, such as

if (bitmapobject.isrecycled () ==false)//If no recycle

Bitmapobject.recycle ();

At present, the system of the animation support more than the weak for the general application of the transition effect can, but for the game general art may be accustomed to the unified GIF processing, the current Android system can only preview the first frame of GIF, Resources in the GIF89 format can be read by using threads and writing parsers in J2ME.

For most Android phones that don't have too many physical buttons, maybe we need to imagine a gesture recognition gesturedetector and a gravitational induction to manipulate. Usually we also consider the problem of misoperation of noise reduction processing.

Android heap memory can also be defined by its own size

For some large Android projects or games in the algorithm to deal with the problem, the impact of performance bottlenecks are mainly Android's own memory management mechanism, the current mobile phone manufacturers are stingy with RAM, for software fluency, the impact of RAM on the performance is very sensitive, In addition to the last Dalvik virtual machine heap memory allocation mentioned by the Android Development Network, we can also enforce the memory size of our own software, and we use the Dalvik.system.VMRuntime class provided by Dalvik to set the minimum heap memory as an example:

Private final static int cwj_heap_size = 6* 1024* 1024;

Vmruntime.getruntime (). Setminimumheapsize (Cwj_heap_size); Set minimum heap memory to 6MB size. Of course, for the memory crunch can also be handled by manual interference GC, we will mention the specific application next time.

Optimizing heap memory allocations for Dalvik virtual machines

For the Android platform, the DALVIKJAVAVM used by its hosting layer can be optimized for the present performance, such as we might consider manual interference GC processing in the development of some large game or resource-consuming applications, The Settargetheaputilization method provided with the Dalvik.system.VMRuntime class enhances the processing efficiency of the program heap memory. Of course, we can refer to the specific principles of open source Engineering, here we only say the use of the method: Private Final staticfloattarget_heap_utilization = 0.75f; You can call Vmruntime.getruntime (). Settargetheaputilization (Target_heap_utilization) when the program is OnCreate.

Related Article

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.