Android 监视器显示我的应用程序中内存不断增加

Android Monitor shows me a constant memory increment in my application

我正在开发 Android 的应用程序,我正在使用 Surface 读取二维码。使用 Monitor 检查资源,我看到内存从 ~7 MB 不断增加到 ~20 MB,然后释放内存,行为重新开始。请问这是正常现象还是我遗漏了什么。

我的代码

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    TextView barCodeInfo;
    SurfaceView cameraSurface;
    Button scan;

    BarcodeDetector barcodeDetector;
    CameraSource cameraSource;

    static final int REQUEST_CAMERA_PERMISSION = 1001;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cameraSurface = (SurfaceView) findViewById(R.id.cameraSurface);
        barCodeInfo = (TextView) findViewById(R.id.barCodeResult);
        scan = (Button) findViewById(R.id.scan);

        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
            } else {
                showCameraPreview();
            }
            }
        });

        barcodeDetector = new BarcodeDetector.Builder(this)
                .setBarcodeFormats(Barcode.QR_CODE)
                .build();

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barCodes = detections.getDetectedItems();
                if (barCodes.size() != 0) {
                    barCodeInfo.post(new Runnable() {    // Use the post method of the TextView
                        public void run() {
                            barCodeInfo.setText(    // Update the TextView
                                    barCodes.valueAt(0).displayValue
                            );
                        }
                    });
                }
            }
        });

        cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setRequestedPreviewSize(200, 200)
                .build();

        cameraSurface.getHolder().addCallback(this);
    }

    private void showCameraPreview() {
        try {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                cameraSource.start(cameraSurface.getHolder());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if(cameraSurface != null){
            cameraSurface = null;
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            showCameraPreview();
        } else {
            Toast.makeText(this, "Camera permission was denied", Toast.LENGTH_LONG).show();
        }
    }
}

Android 监控

只有当我关闭应用程序时,内存才会恢复。

是的,这是完全正常的。在app运行ning的过程中,资源在内存中分配,因此内存使用量增加(如android监视器所示)。系统不断检查可用内存以启动 垃圾收集器 以释放任何未引用的内存。

顺便说一句,JVM 直到必要时才会 运行 垃圾收集器,因为垃圾收集器 会减慢速度 很多,因此不应该 运行 非常频繁。当 需要 内存被释放时,它 运行s。尽可能延迟 GC 总是首选。

下图中,由于GC事件导致内存突然释放。

看到没有太多 GC 事件是一件好事,因为这会降低应用程序性能。

因此,如果您继续使用您的应用程序并且内存占用量不断增加,那么 GC 会不时地释放它们。