Android 中的内存跟踪/泄漏
存在一些内存问题,每次该块运行时,DDMS 中使用的内存百分比每次都会增加 2%,直到最终在位图分配时崩溃。
Uri U = null;
try {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
File path = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File file = new File(path, "image.jpg");
path.mkdirs();
FileOutputStream Os = new FileOutputStream(file);
getFullResFilteredImage().compress(Bitmap.CompressFormat.JPEG, 80, Os);
Os.flush();
Os.close();
Os = null;
U = Uri.fromFile(file);
file.deleteOnExit();
file = null;
} else {
Toast toast = Toast.makeText(PrintLabActivity.this.getApplicationContext(),
PrintLabActivity.this.getResources().getString(R.string.sd_image_error), Toast.LENGTH_SHORT);
toast.show();
return;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpg");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_STREAM, U);
i.putExtra(Intent.EXTRA_SUBJECT, this.getResources().getString(R.string.email_subject));
String message = this.getResources().getString(R.string.email_message, PNBApplication.MARKETPLACE_URL, PNBApplication.MARKETPLACE_URL);
i.putExtra(Intent.EXTRA_TEXT , Html.fromHtml(message));
U = null;
this.startActivity(Intent.createChooser(i,"Send Image To:"));
如果这段代码中没有任何内容可能泄漏,是否有办法知道每次分配了什么?
Having some memory issues where each time this block runs the memory %used in DDMS goes up by 2% each time until it finally crashes on bitmap allocation.
Uri U = null;
try {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
File path = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File file = new File(path, "image.jpg");
path.mkdirs();
FileOutputStream Os = new FileOutputStream(file);
getFullResFilteredImage().compress(Bitmap.CompressFormat.JPEG, 80, Os);
Os.flush();
Os.close();
Os = null;
U = Uri.fromFile(file);
file.deleteOnExit();
file = null;
} else {
Toast toast = Toast.makeText(PrintLabActivity.this.getApplicationContext(),
PrintLabActivity.this.getResources().getString(R.string.sd_image_error), Toast.LENGTH_SHORT);
toast.show();
return;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpg");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_STREAM, U);
i.putExtra(Intent.EXTRA_SUBJECT, this.getResources().getString(R.string.email_subject));
String message = this.getResources().getString(R.string.email_message, PNBApplication.MARKETPLACE_URL, PNBApplication.MARKETPLACE_URL);
i.putExtra(Intent.EXTRA_TEXT , Html.fromHtml(message));
U = null;
this.startActivity(Intent.createChooser(i,"Send Image To:"));
If there is nothing in this code that could leak is there a way to tell what's being allocated each time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建 Bitmap 时,可以使用 BitmapFactory.Options.inSampleSize 来减少内存消耗。
如需更详细的答案,请发布您的 getFullResFilteredImage() 方法。
You can use BitmapFactory.Options.inSampleSize to reduce memory consumption when creating the Bitmap.
For a more detailed answer, please post your getFullResFilteredImage() method.