本文介绍了为什么我的 160kb 应用程序后台在运行时变成 49 MB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定调查我的应用程序的内存使用情况,并查看了 Android Studio 的内存监视器,我的内存使用量约为 68 MB.对我来说它看起来太高了.

I decided to investigate my app's memory usage and I looked at Android Studio's Memory Monitor and my memory usage was around 68 MB. It looks too high to me.

我打开内存分配器并从应用程序的开头开始跟踪.我看到有一个 NonMovableArray 的 49 MB 分配,它是一个 Bitmap.

I opened memory allocator and started tracking from the beginning of the application. I saw that there is a 49 MB allocation of a NonMovableArray, which is a Bitmap.

我调试了应用程序,发现它是我使用的背景.下面几行来自 PhoneWindow.java 文件,我相信这是 Android 为屏幕分配背景的地方.背景对象的大小为 49 MB,分辨率为 2625x4669.

I debugged the application and found out that it was the background I was using. The lines below are from PhoneWindow.java file, that's where Android assigns the background to the screen I believe. The background object had a size of 49 MB and 2625x4669 resolution.

我的应用程序中没有透支,并且我有一个应用于整个主题的背景.

I have no overdraw in my app, and I have a single background that is applied to entire theme.

我在 drawable 文件夹中有一个 JPG 格式的背景可绘制对象,分辨率为 750x1,334.

I have a background drawable in drawable folder in JPG format with 750x1,334 resolution.

PhoneWindow.java

if (mBackgroundResource != 0) {
    background = getContext().getDrawable(mBackgroundResource);
} else {
    background = mBackgroundDrawable;
}

我正在摩托罗拉 Nexus 6 设备上进行测试,该设备的密度为 560,分辨率为 1440 x 2560.

I am testing this on a Motorola Nexus 6 device which has 560 density with a resolution of 1440 x 2560.

有两点我不明白.

  1. 如果设备的分辨率为 1440x2560,为什么我的背景会转换为 2625x4669?
  2. 即使这种转换是应用的最佳方案,160 KB 的文件怎么会变成 49 MB?

如果你们能向我解释这一点,那就太好了.谢谢!

If you guys can explain this to me that would be great. Thanks!

推荐答案

您将图像放在 res/drawable/ 中.这不是一个好的选择,因为它是 res/drawable-mdpi/ 的同义词.因此,Android 正在重新采样您的图像,认为您的目标是 -mdpi 设备(~160dpi),因此图像在 Nexus 6 上的物理尺寸大致相同(密度的 3.5 倍).

You put the image in res/drawable/. This is not a good choice, as that is a synonym for res/drawable-mdpi/. Hence, Android is resampling your image, thinking that you were aiming for -mdpi devices (~160dpi), so the image is about the same physical size on the Nexus 6 (3.5x the density).

res/drawable-nodpi/ 还是 res/drawable-anydpi/ 是正确的选择,这在一定程度上取决于您对该资源的替代版本,尽管 两者都可能工作.

Whether res/drawable-nodpi/ or res/drawable-anydpi/ is the right choice depends a bit on your alternative versions of this resource, though either will probably work.

为什么 160 KB 的文件最终会变成 49 MB?

图像在磁盘上占用 160KB.内存占用是解码后的图像.这将是 ARGB_8888 图像的图像分辨率(重采样后)乘以 4 字节/像素.2625x4669x4 ~= 49MB.

The image takes up 160KB on disk. The memory footprint is the decoded image. That will be the image resolution (post-resampling) times 4 bytes/pixel for ARGB_8888 images. 2625x4669x4 ~= 49MB.

这篇关于为什么我的 160kb 应用程序后台在运行时变成 49 MB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:24
查看更多