Android - 确定指南针中的内存泄漏(SensorManager)

时间:2015-03-01 20:19:25

标签: java android memory-leaks sensor

我使用单例模式实现了一个简单的罗盘类。一旦我使用传感器管理器注册了加速度计和磁力计监听器,内存消耗就会增加。我的GPS类非常类似于罗盘类,我在内存泄漏方面没有任何问题。我在onSensorChanged方法中放置了一个断点,并且每5次调用分配的内存增加了0.01 MB。我将在下面添加相应的代码。也许有人知道出了什么问题。也许没有问题和它的正常,内存略有增加?

主要活动

public class MainActivity extends ActionBarActivity {
    [...]
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        this.mGPS = GPSTracker.getInstance();
        this.mGPS.start( MainActivity.this );
        this.mLocation = this.mGPS.getLatLng();

        this.mCompass = CompassTracker.getInstance();
        // uncomment the following line will stop the memory leak
        this.mCompass.start( MainActivity.this );
    }
    [...]
}

指南针课程

public class CompassTracker implements SensorEventListener {
    [...]
    /**
     * The minimum time between updates in milliseconds.
     */
    private static final int MIN_TIME_BW_UPDATES = 500; //  1 * 60 * 1000 -> 1 Minute

    private void start( Context context ) {
        this.mSensorManager = (SensorManager) context.getSystemService( Context.SENSOR_SERVICE );

        this.mAccelerometer = this.mSensorManager.getDefaultSensor( Sensor.TYPE_ACCELEROMETER );
        this.mMagnetometer = this.mSensorManager.getDefaultSensor( Sensor.TYPE_MAGNETIC_FIELD );

        boolean enAcc = this.mSensorManager.registerListener( this, this.mAccelerometer, MIN_TIME_BW_UPDATES );
        boolean enMag = this.mSensorManager.registerListener( this, this.mMagnetometer, MIN_TIME_BW_UPDATES );

        if ( enAcc && enMag ) {
            this.mStatus = States.ENABLED;
        } else {
            this.mStatus = States.NOT_AVAILABLE;
            this.stop();
        }
    }

    @Override
    public void onSensorChanged( SensorEvent event ) {
        final float alpha = 0.97f;

        if ( event.sensor.getType() == Sensor.TYPE_ACCELEROMETER ) {
            this.mGravity[0] = alpha * this.mGravity[0] + ( 1 - alpha ) * event.values[0];
            this.mGravity[1] = alpha * this.mGravity[1] + ( 1 - alpha ) * event.values[1];
            this.mGravity[2] = alpha * this.mGravity[2] + ( 1 - alpha ) * event.values[2];
        }

        if ( event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD ) {
            this.mGeomagnetic[0] = alpha * this.mGeomagnetic[0] + ( 1 - alpha ) * event.values[0];
            this.mGeomagnetic[1] = alpha * this.mGeomagnetic[1] + ( 1 - alpha ) * event.values[1];
            this.mGeomagnetic[2] = alpha * this.mGeomagnetic[2] + ( 1 - alpha ) * event.values[2];
        }

        float R[] = new float[9];
        float I[] = new float[9];

        if ( SensorManager.getRotationMatrix( R, I, mGravity, mGeomagnetic ) ) {
            this.mOrientation = SensorManager.getOrientation( R, this.mOrientation );
        }

        // inform the listener
        if( this.orientationChangedListener != null )
            this.orientationChangedListener.onOrientationChanged( this.mOrientation );
    }


    /**
     * Orientation listener
     * @param orientationChangedListener
     */
    public void setOrientationChangedListener( OrientationChangedListener orientationChangedListener ) {
        this.orientationChangedListener = orientationChangedListener;
    }
    [...]
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

每次

this.mSensorManager = (SensorManager) context.getSystemService( Context.SENSOR_SERVICE );

被调用,当前MainActivity的实例与您的单例类绑定。可能你的GPS类会导致相同的内存泄漏,但不会像这一样快。

你在这里做的是糟糕的设计。特别是在Activity中使用单例类。像这样重构你的代码:

  1. 了解Androids文档中的Service
  2. 将传感器内容放入服务中。
  3. 在您的服务的onCreate方法中,使用其上下文获取GPS和指南针的系统服务
  4. 使用服务的onDestroy方法取消绑定系统服务
  5. LocalBroadcastManager一样使用广播将数据传递给其他组件,例如您的活动