我正在开发一款使用大量位图分配的游戏。我也是自己回收位图。我想跟踪分配给位图的内存。有没有办法在Android中跟踪它们?这是一个例子:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("MainActivity", "" + Debug.getNativeHeapAllocatedSize());
Bitmap bitmap = loadBitmap(this.getResources().getDrawable(R.drawable.bg1), Bitmap.Config.RGB_565);
Log.i("MainActivity", "" + Debug.getNativeHeapAllocatedSize());
bitmap.recycle();
bitmap = null;
System.gc();
Log.i("MainActivity", "" + Debug.getNativeHeapAllocatedSize());
}
public static Bitmap loadBitmap(Drawable drawable, Config bitmapConfig) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap mBitmap = Bitmap.createBitmap(width, height, bitmapConfig);
Canvas canvas = new Canvas(mBitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return mBitmap;
}
}