我是android编程的新手,我已经完成了我的第一个程序。但是,程序在运行后冻结,log cat出现如下:
05-21 14:41:24.853: D/dalvikvm(5024): GC_FOR_ALLOC freed 496K, 70% free 8328K/27200K, paused 19ms, total 19ms
我在网上搜索过,他们说这是内存管理问题。但是我能做些什么来减少记忆?我在我的程序中使用加速度计和磁场传感器,每当我运行该活动时,它就会冻结。堆大小达到365M最高,但是我不运行该活动,它将正常工作,堆大小将只有1xxM。为什么这个活动占用了这么多内存?请给我一些建议。 下面是我在DDMS中的分配跟踪器和堆。
以下是导致冻结的活动:
package com.example.suntracking;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class Accelerometer extends Activity implements SensorEventListener{
Sensor accelerometer,magnetometer;
SensorManager sm;
private int samintval;
private float uxtemp,ux,uytemp,uy,ax,ay,axtemp ,aytemp ,sx,sy,axdiff,aydiff,latnew,latold,longnew,longold;
private TextView showax,showay,showaxtemp,showaytemp;
private float[] mLastAccelerometer = new float[3];
private float[] mLastMagnetometer = new float[3];
private boolean mLastAccelerometerSet = false;
private boolean mLastMagnetometerSet = false;
private float[] mR = new float[9];
private float[] mhold = new float[3];
private float yaw,roll,pitch,rollold,pitchold;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initialize();
sm.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_GAME);
sm.registerListener(this, magnetometer,SensorManager.SENSOR_DELAY_GAME);
loadsavedPref();
finish();
}
@Override
protected void onResume()
{
super.onResume();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
sm.unregisterListener(this);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
// sm.unregisterListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer=sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
private void loadsavedPref() {
// TODO Auto-generated method stub
SharedPreferences savedata= getSharedPreferences("savealldata",0);
samintval = savedata.getInt("samintval", 1000);
latold = savedata.getFloat("latval", 0);
longold = savedata.getFloat("longval", 0);
}
private void cal_displace() {
// TODO Auto-generated method stub
//setting axis
ax *= (-1);
ay *= (-1);
ax = (float) (ax * (Math.cos(Math.toRadians(roll))));
ay = (float) (ay * (Math.cos(Math.toRadians(pitch))));
//
ux = uxtemp + ax *(samintval / 1000);
uy = uytemp + ay * (samintval / 1000);
axdiff= (ax - axtemp)/(samintval / 1000);
aydiff = (ay - aytemp)/(samintval / 1000);
sx = (ux *(samintval / 1000)) + ((axdiff) * (samintval / 1000)* (samintval / 1000)) / 2;
sy = (uy *(samintval / 1000)) + ((aydiff) * (samintval / 1000)* (samintval / 1000)) / 2;
latnew = (float) (latold + (sy / 111229.0));
longnew = (float) (longold + (sx / 71695.8));
axtemp = ax;
aytemp = ay;
uxtemp = ux;
uytemp = uy;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometerSet = true;
ax = event.values[0];
ay = event.values[1];
}else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometerSet = true;
}
if ((mLastAccelerometerSet) && (mLastMagnetometerSet))
{
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mhold);
yaw = ((float)Math.toDegrees(mhold[0]));
pitch = ((float)Math.toDegrees(mhold[1]));
roll = ((float)Math.toDegrees(mhold[2]));
savePreferences();
}
cal_displace();
savePreferences();
}
private void savePreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata = getSharedPreferences("savealldata",0);
Editor editor=savedata.edit();
editor.putFloat("ax", ax);
editor.putFloat("ay", ay);
editor.putFloat("axtemp", axtemp);
editor.putFloat("aytemp", aytemp);
editor.putFloat("axdiff", axdiff);
editor.putFloat("aydiff", aydiff);
editor.putFloat("sx", sx);
editor.putFloat("sy", sy);
editor.putFloat("alatval", latnew);
editor.putFloat("alongval", longnew);
editor.putFloat("yaw", yaw);
editor.putFloat("pval", pitch);
editor.putFloat("rval", roll);
editor.commit();
}
}