Android 中的内存跟踪/泄漏

发布于 2024-11-28 16:24:30 字数 1730 浏览 5 评论 0原文

存在一些内存问题,每次该块运行时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2024-12-05 16:24:31

创建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文