android - Activity 不会破坏以在 android 中释放堆?

标签 android android-activity heap-memory activity-finish ondestroy

我试图通过销毁当前 Activity 来释放堆大小,同时转到另一个 Activity 。

我在 backPreess() 上使用 finish();

但这并没有释放堆。

on setContentView()

堆大小增加了 16Mb。我想在进行另一项 Activity 后释放堆中的这种增加。任何人都可以帮助如何做到这一点?

我的代码如下:

    package com.stancil.levels;        
    public class PaintActivity extends ZebraActivity implements
            PaintView.LifecycleListener, PaintView1.LifecycleListener1 {

        private static final int REQUEST_PICK_COLOR = 1;

        ....
....
    public PaintActivity() {
            _state = new State();
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Constants.context = getApplicationContext();


            setContentView(R.layout.paint);
            ..................
    ...................
    ...............

        }


        public void onPreparedToLoad() {
            // We need to invoke InitPaintView in a callback otherwise
            // the visibility changes do not seem to be effective.
            new Handler() {
                @Override
                public void handleMessage(Message m) {
                        new InitPaintView();

                    Log.v("PaintActivity", "After InitPaintView Called");
                }
            }.sendEmptyMessage(0);
        }


        private class InitPaintView implements Runnable {
            private Bitmap _originalOutlineBitmap;
            private Handler _handler;

            public InitPaintView() {
                // Make the progress bar visible and hide the view

                _paintView.setVisibility(View.GONE);
                _progressBar.setProgress(0);
                _progressBar.setVisibility(View.VISIBLE);
                _state._savedImageUri = null;
                _state._loadInProgress = true;

                _originalOutlineBitmap=_imageBitmap;
                _handler = new Handler() {
                    @Override
                    public void handleMessage(Message m) {
                        switch (m.what) {
                        case Progress.MESSAGE_INCREMENT_PROGRESS:
                            // Update progress bar.
                            _progressBar.incrementProgressBy(m.arg1);
                            break;
                        case Progress.MESSAGE_DONE_OK:
                        case Progress.MESSAGE_DONE_ERROR:
                            // We are done, hide the progress bar 
                            // the paint view back on.
                            _state._loadInProgress = false;
                            _paintView.setVisibility(View.VISIBLE);
                            _progressBar.setVisibility(View.GONE);
                            initiatePopupWindow();
                            break;
                        }
                    }
                };

                new Thread(this).start();

            }

            public void run() {
                Log.v("Wasimmmmmmmmmmmmmmmm", "qqqqq 22");
                _paintView.loadFromBitmap(_originalOutlineBitmap, _handler);
            }

        }



        private static class State {
            // Are we just loading a new outline?
            public boolean _loadInProgress;

            // The resource ID of the outline we are coloring.
            //public int _loadedResourceId;
            //
            // If we have already saved a copy of the image, we store the URI here
            // so that we can delete the previous version when saved again.
            public Uri _savedImageUri;
        }







        @Override
            public void onBackPressed() {
                new AlertDialog.Builder(this)
                .setTitle("Exit")
                .setMessage("Do you want to go to Main Menu?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                         Constants.check_new=true;
                       Intent i=new Intent(PaintActivity.this,MainActivity.class);
                      // i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       startActivity(i);
                       finish();
                       overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Do nothing.
                    }
                }).show();
         }
}

    }

最佳答案

在 onDestroy 方法中释放 yoru 对象,无论如何,如果没有对被销毁 Activity 的引用,GC 将在需要时自动清理(它不需要在您关闭 Activity 后立即发生)。 或者,有一种方法可以强制运行 GC,但我什至不会写它,因为它不是典型应用程序应该使用的真正特性

关于android - Activity 不会破坏以在 android 中释放堆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20285340/

相关文章:

android - 在 Fresco 的 SimpleDraweeView 中加载可绘制图像资源

android - 无法在 Android Studio 2.3 中使用注释处理器

android - 在 greenDao 2.1.0 中加入两个以上的关系

android - 如何避免同一 Activity 的多个实例?

Android 浏览器和背面可见性问题

android - 有没有办法找出启动 Activity 的 startActivity Intent 标志?

Android 以 bundle 方式传递持久信息还是使用单例模式?

c++ - 在堆上分配巨大的数组;程序不会崩溃并且编译没有错误

Android 应用程序堆大小

android - 查询/system/build.prop 中的属性