跟踪位图内存分配
我正在开发一个使用大量位图分配的游戏。我自己也在回收位图。我想跟踪分配给位图的内存。有什么方法可以在 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;
}
}
I am developing a game which uses a lot of bitmap allocations. I am also recycling the bitmaps by himself. I want to track memory which is allocated to bitmaps. Is there any way to track them in Android? Here is example:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论