我一直在研究以下代码,它在Android平板电脑上打开存储的图像,将其解码为Bitmap,然后将其转换为base64字符串,以便它可以存储在SQLite数据库中。
据我所知,从这个事务中泄漏了2MB的数据,多次调用此函数会占用越来越多的内存,而这些内存无法正确收集垃圾。
一旦填充了origin_photo,它就占据了大约49kb,一旦转换为base64,它就占据了大约65kb。
photo.insert函数是base64的简单SQLite插入以及一些小信息。我不认为这是问题的一部分。
此功能完成后,我还会在log cat中收到一条消息“Skipped 57 frames!应用程序可能在其主线程上做了太多工作”(这没有任何断点减慢代码的速度)
我可能会弄错,并且这是一个与此代码接近的不同部分导致泄漏,但这似乎是最有可能的候选人。任何帮助都非常感谢。
更新:base64.encodeBytes函数取自
public void savePhoto()
{
try
{
String[] projection = { MediaStore.Images.Media.DATA };
//Get last captured image in db
String capturedImageFilePath = null;
try
{
//NOTE: The warning on activity.managedQuert states you must NOT close the cursor it creates yourself
Cursor cursor = activity.managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//Get file path from last stored photo
capturedImageFilePath = new String(cursor.getString(column_index_data));
//cursor.close();
}
catch(IllegalArgumentException e)
{
ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_GET_IMAGE_FILE_PATH, this.getActivity());
}
catch(NullPointerException e)
{
ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_GET_IMAGE_FILE_PATH, this.getActivity());
}
catch(Exception e)
{
ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_GET_IMAGE_FILE_PATH, this.getActivity());
}
int orientation = -1;
//Get Exif data from current image and store orientation
//Note exif data will be stripped when this filepath is turned
//into a bitmap
try
{
ExifInterface e = new ExifInterface(capturedImageFilePath);
orientation = e.getAttributeInt("Orientation", -1);
}
catch (IOException ioe)
{
ioe.printStackTrace();
ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_EXIF_DATA_IO, this.getActivity());
}
catch(Exception e) { ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_EXIF_DATA_GENERAL, this.getActivity()); }
//Decode current photo into a Bitmap
Bitmap b = BitmapFactory.decodeFile(capturedImageFilePath);
if(b == null)
{
Log.d("activitycrossellcalls", "error open " + capturedImageFilePath);
ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_NULL_BITMAP, this.getActivity());
}
else
{
int width = -1;
int height = -1;
width = b.getWidth();
height = b.getHeight();
Log.d("activitycrossellcalls", "w: "+width+", h:"+height);
//Scale down if too big
int max = (width > height)?width:height;
float ratio = 1;
if(max > MAX_IMAGE_SIZE)
ratio = (float)max/(float)MAX_IMAGE_SIZE;
width /= ratio;
height /= ratio;
b = Photos.getResizedBitmap(b, height, width);
Log.i("activitycrossellcalls", "new w: " + width + ", h: " + height);
// Encode
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 70, baos);
b.recycle();
byte[] origin_photo = null;
origin_photo = baos.toByteArray();
// Insert
Photo photo = null;
try
{
photo = new Photo();
photo.base64 = Base64.encodeBytes(origin_photo);
photo.call = DailyCallsDetailsActivity.call.id;
photo.tag_id = TaggingActivity.currentTag.id;
photo.orientation = orientation;
}
catch(Exception e) { ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO_INIT, this.getActivity()); }
photo.insert();
}
}
catch(Exception e) { ErrorCodes.CreateError(ErrorCodes.DCDF_SAVE_PHOTO, this.getActivity()); }
}
public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
try
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// RECREATE THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
catch(Exception e) { ErrorCodes.ReportError(ErrorCodes.PHOTS_GET_RESIZED_BITMAP); return null; }
}
答案 0 :(得分:3)
我想我找到了它
Bitmap b = BitmapFactory.decodeFile(capturedImageFilePath);
...
b = Photos.getResizedBitmap(b, height, width);
您没有回收原始b。这不应该是一个永久性的泄漏,但它肯定会导致GC捶打。
您也可以考虑使用LRUCache自动循环进出位图。
答案 1 :(得分:2)
您解码可能很大的图像。在savePhoto()中检查此行之后的位图b的大小:
Bitmap b = BitmapFactory.decodeFile(capturedImageFilePath);
之后你缩小它,所以当你保存到数据库时它会显得更小。我的猜测是你的源文件很大,导致的内存比你预期的要多。
此外,对于来自logcat的“跳过帧”消息,通常在UI线程上执行长时间运行操作时会发生这种情况。确保从UI线程中调用您的方法,可能在AsyncTask
。
答案 2 :(得分:0)
您没有关闭光标。可能不是唯一的问题,但它是其中之一。