Android is loaded Solution picture memory overflow

Android loading solution picture memory overflow

Change the image sizes

bitmap image size settings, optimization methods to avoid memory overflow of OutOfMemoryError

When using bitmap android readily memory overflow, an error message follows: Java.lang.OutOfMemoryError: bitmap size exceeds VM budget

 

InputStream is = this.getResources().openRawResource(R.drawable.pic1);

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

options.inJustDecodeBounds = false;

options.inSampleSize = 10; // width, hight set a very original

Bitmap btp =BitmapFactory.decodeStream(is,null,options);

 

Activity exit when recovery picture memory

if(!bmp.isRecycle() ){

         bmp.recycle () // Recycle image occupies memory

         system.gc () // timely reminder system recovery

}

 

Android define heap memory size

For some large projects or Android games no problem in processing algorithm, the main performance bottleneck is affecting Android own memory management problem, there are more mobile phone manufacturers skimp on RAM for the fluency of software for the performance of RAM sensitive to impact, in addition to the heap memory allocation optimized Dalvik last mentioned Android open Source virtual machine, we can also define your own software to force the memory size, we use Dalvik provided dalvik.system.VMRuntime class to set the minimum heap memory, for example:

private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;

VMRuntime.getRuntime () setMinimumHeapSize (CWJ_HEAP_SIZE);. // set the minimum size of the heap memory is 6MB. Of course, for it can also interfere with tight memory GC manually to deal with, we will refer to the specific application next time.

Optimization of heap memory allocation Dalvik virtual machine

For Android platforms, Dalvik its hosting layer used JavaVM from current performance, there are many places you can optimize the process, such as we have in the development of some of the big game or application resource-consuming manual intervention may consider GC treatment, use dalvik. setTargetHeapUtilization system.VMRuntime class provides methods may be enhanced processing efficiency program memory heap. Of course, we can refer to specific principles of open source projects, where we only say when Approach: private final static floatTARGET_HEAP_UTILIZATION = 0.75f; onCreate in the program can call VMRuntime.getRuntime () setTargetHeapUtilization (TARGET_HEAP_UTILIZATION); you can.

 

Use thumbnails

After piecemeal picture of the size of the display, click on the picture display, and then display the original picture in a new interface based on a new path information. Each time you exit the time of the original bitmap pictures for recycling.

Guess you like

Origin www.cnblogs.com/kexinxin/p/11722948.html