跟踪位图内存分配

发布于 2024-10-30 13:06:17 字数 1085 浏览 1 评论 0原文

我正在开发一个使用大量位图分配的游戏。我自己也在回收位图。我想跟踪分配给位图的内存。有什么方法可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文