我有一个设备(三星 Galaxy Fit)。当我打开设备时,我无法进入主屏幕,它一直在加载。当我连接设备并看到 logcat 时,我的 logcat 错误显示 -
"FATAL EXCEPTION IN SYSTEM PROCESS: ActivityManager and java.lang.OutOfMemoryError: [memory exhausted]"
如何处理?
我有一个设备(三星 Galaxy Fit)。当我打开设备时,我无法进入主屏幕,它一直在加载。当我连接设备并看到 logcat 时,我的 logcat 错误显示 -
"FATAL EXCEPTION IN SYSTEM PROCESS: ActivityManager and java.lang.OutOfMemoryError: [memory exhausted]"
如何处理?
有一些方法可以解决内存不足错误
首先像这样回收位图
bmp.recycle();
bmp = null;
第二次使用垃圾收集器
System.runFinalization();
Runtime.getRuntime().gc();
System.gc();
但是如果您在这里发布一些代码会更好,这样我和其他人可以为您提供更多帮助:)
使用以下代码,您必须检查 OutOfMemory 错误
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try
{
// We need to recyle unused bitmaps
if (Main_bitmap != null)
{
Main_bitmap.recycle();
}
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor.moveToFirst())
{
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds=true;
BitmapFactory.decodeFile(filePath,bmpFactoryOptions);
if ( checkBitmapFitsInMemory(bmpFactoryOptions.outWidth,bmpFactoryOptions.outHeight, 2))
{
BitmapFactory.Options options4 = new BitmapFactory.Options();
options4.inSampleSize = 1;
Main_bitmap = BitmapFactory.decodeFile(filePath, options4);
iv_set_image.setImageBitmap(Main_bitmap);
}
else
{
Toast.makeText(this,"java.lang.OutOfMemoryError:", 1).show();
}
cursor.close();
}
catch (Exception e)
{
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
public static boolean checkBitmapFitsInMemory(long bmpwidth,long bmpheight, int bmpdensity )
{
long reqsize=bmpwidth*bmpheight*bmpdensity;
long allocNativeHeap = Debug.getNativeHeapAllocatedSize();
final long heapPad=(long) Math.max(4*1024*1024,Runtime.getRuntime().maxMemory()*0.1);
if ((reqsize + allocNativeHeap + heapPad) >= Runtime.getRuntime().maxMemory())
{
return false;
}
return true;
}