简体   繁体   English

如何在Android上计算位图的Java哈希图的大小

[英]How to calculate size of a java hashmap of bitmaps on android

I have created a hahsmap of bitmap objects in my android service, using an int as key. 我已经在Android服务中使用int作为键创建了位图对象的hahsmap。 To this, I am adding different bitmaps created dynamically, as per requirement. 为此,我根据需要添加了动态创建的不同位图。 Can I find the RAM/heap memory footprint added to my service because of this hashmap (including the size of bitmaps), using some tool, or programatically. 我能否找到使用此工具或以编程方式将此哈希图(包括位图的大小)添加到服务中的RAM /堆内存占用量。

If anybody has worked in this area before, please help ! 如果以前有人在此领域工作过,请帮助!

Well with bitmaps it's pretty simple because they're bitmaps, huge swaths of memory, if you're using rgb8 format (1 byte per colour) then it is height*width*3 bytes big + Java's overhead (~24 bytes is a safe assumption)+ other stuff in class (8*method count at least) 对于位图,它非常简单,因为它们是位图,占用大量内存,如果您使用的是rgb8格式(每种颜色1字节),则它是height * width * 3字节大+ Java的开销(〜24字节是安全的假设)+课堂上的其他内容(至少8 *方法计数)

Anyway the bitmap will be the big thing. 无论如何,位图将是一件大事。

The hasmap itself will be somewhere around 16*number of things in it big + Java's overhead + other stuff in class. hasmap本身将有16处左右的东西+ Java的开销+类中的其他东西。

It's hard with Java because there's no concept of ownership, nor size (everything is a reference) so you need to be careful not to count stuff twice.... 使用Java很难,因为没有所有权的概念,也没有大小的概念(所有内容都是参考),因此您需要注意不要重复计算。

Anyway the killer is the bitmap the rest is sufficiently small to neglect. 无论如何,杀手is是位图,其余的足够小,可以忽略不计。

As I thought though: 正如我所想:

final int   getAllocationByteCount()
Returns the size of the allocated memory used to store this bitmap's pixels.
final int   getByteCount()
Returns the minimum number of bytes that can be used to store this bitmap's pixels.

From

http://developer.android.com/reference/android/graphics/Bitmap.html http://developer.android.com/reference/android/graphics/Bitmap.html

You can run through values in Map and compute each bitmap's memory with 您可以遍历Map中的值并使用以下命令计算每个位图的内存

HashMap<?, Bitmap> bitmaps;

long total = 0;

for (Bitmap bmp : bitmaps.values()) {
    total += bmp.getRowBytes() * bmp.getHeight() * 4; // 4 is bytes per pixel in ARGB_8888 mode
}

您可以转储堆并使用Eclipse内存分析器(MAT)来获取您感兴趣的所有对象的大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM