android - 在 C# 中使用 BitmapFactory.DecodeFile 时内存占用量较大(对于小图像占用大量内存)

标签 android image memory bitmap xamarin.android

我只是想看看有没有其他人注意到,当将图像加载到内存中时,完全相同的 C# 代码使用更多的内存来保存图像(约为 Java 的 1.5 倍)。

此 Java 代码导致总内存大小为 105.5 MB:

Bitmap[] bitmaps = new Bitmap[100];

for (int i = 0; i < 100; i++)
{
    String root = Environment.getExternalStorageDirectory().getAbsolutePath();
    String imagePath = "/evolution/threesixty/216/Edaphosaurus_001.jpg";

    bitmaps[i] = BitmapFactory.decodeFile(root + imagePath);
}

此 C# 代码产生 148.1 MB:

Bitmap[] bitmaps = new Bitmap[100];

for (int i = 0; i < 100; i++)
{
    string root = Environment.ExternalStorageDirectory.AbsolutePath;
    string imagePath = "/evolution/threesixty/216/Edaphosaurus_001.jpg";

    bitmaps[i] = BitmapFactory.DecodeFile(root + imagePath);
}  

有人知道为什么吗?我知道 .NET 包装器可能会使用更多的东西来保存额外的绑定(bind)数据或东西,但肯定不会那么多?

另外,作为附带问题: 每个图像在磁盘上只有 60 KB,为什么内存版本是 1 MB?我知道我的加载方法不进行任何采样/密度检查,但这不是目的。确定加载 <100KB 图像不应超过 100KB 内存空间?

这是我使用的图像(994x748px @ 300dpi):

the image

最佳答案

100 张图像的 148.1 MB 数字正是我所期望的数字。 994 x 748 给出 743,512 像素;假设您使用的格式每像素使用 2 个字节(这对于图像质量非常低,FWIW - RGB565 格式应该在几十年前在国际上被禁止),每张图像为 1.481 MB。这当然比磁盘上的图像大小大得多,因为图像文件是压缩的。

我不知道为什么 Java 版本只消耗 105.5 MB;据我所知,没有一种位图格式使用每像素 1.333 字节。很可能您读取的内存使用情况有误,或者 Java 运行时将一些图像缓存到磁盘而不是将它们保存在内存中。

关于android - 在 C# 中使用 BitmapFactory.DecodeFile 时内存占用量较大(对于小图像占用大量内存),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11081806/

相关文章:

java - 将 Canvas 转换为 WritableImage 的替代方法

ios - 如何提高包含大量小图像的 UCollectionView 的性能?

java.lang.OutOfMemory错误: Java heap space when reading from a txt file and writing to an xlsx file

c++ - C++ 分配的奇怪减速

android - 是否可以在Honeycomb ActionBar的MenuItem中显示Text右侧的图标?

android - android中的模拟位置

android - Libgdx - 使用 MipMaps

android - 无法创建媒体播放器(使用 url)

android - 如何将图像调整为按钮?

python - 如何在 Python 中快速显示和更新位图?