[]在HPROF后,我的应用程序的巨大字节我的、字节、应用程序、巨大

2023-09-06 02:17:31 作者:我手寫我心

随着时间的时候,在我的应用程序实现新功能尤其是当我使用DDMS + HPROF分析内存和堆的使用。由于应用程序似乎并没有什么表现,ANR问题,一切工作进展顺利,一开始我并没有在意它 - 但现在,当我看到它的大致总是相同的大小,我不知道什么该死的可以在可以。

From time to time, especially when implementing new functionalities in my app, I use DDMS + HPROF to analyze memory and heap use. As the App doesn't seem to have any performance-ANR issues and everything works smoothly, initially I didn't care about it - but now, as I see it's approximately always the same size, I'm wondering what the damn can it be.

每次我运行一个HPROF,我检查的泄漏嫌疑人的标签。总有一个 android.graphics.Bitmap 实例通吃使用的堆的大约25%。

Everytime I run a HPROF, I check the Leak suspects tab. There's always an android.graphics.Bitmap instance that takes approximately 25% of all the used heap.

我想知道远一点那是什么什么,所以我打开的支配树的,看到这样的:

I wanted to know a bit further what's that about, so I opened the dominator tree, and saw this:

所以esentially有一个巨大的字节[] 实例是保留了大量的堆,和从来没有被释放。根据这个,我复制了该值字节[] ,抛投它变成一个 .DATA 文件,打开瘸子,这里面的结果:

So esentially there's a huge byte[] instance that is retaining a lot of heap, and never gets released. According to this, I copied the value of that byte[], dumped it into a .data file, opened with Gimp, and there's the result:

因此​​,基本上,它看起来像的阿尔法(0)的PNG图像的一部分。有考虑到以下事实:

So basically, it looks like the "alpha(0)" part of a PNG image. Having in consideration the following facts:

在我所有的图像文件是&LT;大小8K 只是其中的一部分是png - 其余的我能够转换为JPG,我做了 没有问题,如果我添加更多的图像,那大小字节[] 一直是他们aproximately从应用程序的一开始就相同(4个月前) 要调试它,我试图删除任何图片从绘制绘制-XXX 文件夹和运行,没有任何可绘制资源的应用程序,而字节[] 还在那里 在我删除了几乎所有的布局,让只是基本的funcionality,和同样的结果 在支配树的,根类是 android.graphics.Bitmap All my image files are <8K in size Just some of them are PNG - the remaining I was able to convert to JPG, I did No matters if I add further images, the size of that byte[] has been always aproximately the same from the beggining of the app (4 months ago) To debug it, I tried to remove any image file from the drawable and drawable-xxx folders and run the app without any drawable resources, and the byte[] was still there I removed almost all layouts and let just the basic funcionality, and same result In the Dominator tree, the root class is android.graphics.Bitmap

任何人都知道什么该字节[]如果我做任何事情来释放它呢?

Anyone knows what it this byte[] and if I should do anything to free it up?

任何帮助AP preciated!

Any help appreciated!

推荐答案

只是为了澄清几件事情:

Just to clarify a few things:

图片/你的应用程序可绘制用于内存在的形式 android.graphics.Bitmap 。 作为的Andr​​oid 3.0(API级别11),像素数据存储在Dalvik的堆以及相关的位图。 (管理位图内存) 在巨大的byte []可能言过其实,因为它是刚刚超过堆空间不足1MB。 Images/drawables in your app are used in memory in the form of android.graphics.Bitmap. As of Android 3.0 (API level 11), the pixel data is stored on the Dalvik heap along with the associated bitmap. (Managing Bitmap Memory) "huge byte[]" is probably an overstatement as it is just over 1MB of heap space.

在泄漏嫌疑人报告中可能会有所帮助,但在这种情况下,它不会告诉你太多考虑它的最大嫌疑人是刚刚超过1MB的内存。现代的设备,提供64MB +堆。

The Leak Suspects report can be helpful, but in this case it isn't telling you much considering that its largest suspect is just over 1MB of memory. Modern devices are offering 64MB+ heaps.

让我们做数学题针对此位的内存需求。该位图占用堆1127584字节。如果我们假设该位图使用ARGB_8888配置中,每个像素使用4个字节,这意味着你的图像包含281896个像素(或约530x530)。这听起来不合理的你在做什么?

Let's do the math for the memory requirements for this bitmap. This bitmap is taking up 1,127,584 bytes on the heap. If we were to assume this bitmap is configured using ARGB_8888, each pixel is using 4 bytes, which means your image contains 281,896 pixels (or roughly 530x530). Does this sound unreasonable for what you are doing?

另外,还要考虑的方式在不同的水桶的可绘制的Andr​​oid秤:MDPI,华电国际,xhdpi,等等。比方说,你有在 MDPI 桶一个200x200的图像,并且正在打开一个 xhdpi 设备上的应用程序。此图片将被调整为两倍大,将有一个基于设备的分辨率400×400。因此,尽管200x200的图像可能不会花费太多的堆空间(200×200×4 = 160 KB ),在400×400的图像将需要一个相对较大的堆空间(400×400×4量(4X) = 640 KB )。有关这方面的详细信息,请参阅 支持多画面

Also, consider the way Android scales across the different "buckets" for drawables: mdpi, hdpi, xhdpi, etc. Let's say you have a 200x200 image in the mdpi bucket and you are opening the app on a xhdpi device. This image will be scaled to be twice as large and will have a on-device resolution of 400x400. So while the 200x200 image may not take much heap space (200 x 200 x 4 = 160 kb), the 400x400 image will require a relatively larger amount (4x) of heap space (400 x 400 x 4 = 640 kb). For more information on this, see Supporting Multiple Screens.

一个很好的工具,可以快速计算的差​​异与图像水桶: Android的DPI计算器

A nice tool for quickly computing differences with the image buckets: Android DPI Calculator

你说你删除了一些你可绘制的,但还剩下什么?你有没有考虑可绘制可从外部库来了?

You said you removed some of your drawables, but what is left? Have you considered drawables that may be coming from external libraries?

要回答你的最后一个问题:?任何人都知道它这个字节[],如果我做任何事情来释放它的

To answer your final question: Anyone knows what it this byte[] and if I should do anything to free it up?

我会说:这对你堆少量的内存是没有什么可担心的。如果是困扰着你,留意它并确保它没有增长超出了似乎实用。如果你仍然怀疑有内存泄漏,屏幕间浏览和观看,看堆的持续增长。假设你不缓存位图,堆应航行时来回两个屏幕之间保持一致的/ predictable尺寸

作为一个侧面说明,DDMS使得它很容易监测的飞行堆大小。无需HPROF转储,直到你准备好了吗。看一看的的使用DDMS 。以原因GC按钮特别注意,因为这将可能需要启动初始堆大小的更新。

As a side note, DDMS makes it very easy to monitor heap size on the fly. No need for HPROF dumps until you are ready to dive in. Have a look at Using DDMS. Take special note of the "Cause GC" button as it will be needed to trigger the update of the initial heap size.

- 更新 -

要进一步回答这个问题,一是不支持的猜疑我是,一些应用程序的资产(系统资产/纹理?)被加载到你的应用程序的内存空间。看看幻灯片64浏览:什么新的Andr​​oid 4.4系统。

To further answer this, one unsupported suspicion I have is that some of the app's assets (system assets/textures?) are loaded into your app's memory space. Have a look at slide 64 here: What's new in Android 4.4.

Android 4.4系统现在可以生成一个单独的贴图包含所有的框架资产,由所有进程共享。这样可以节省一点内存在每一个过程,但它也有助于配料和合并绘图操作,自动优化的应用程序。

Android 4.4 now generates a single texture containing all the framework assets, shared by all processes. This saves a bit of memory in every process but it also helps batching and merging drawing operations to optimize applications automatically.

这似乎在暗示,内存用于系统的位图/每个应用程序中可绘制运行的版本早于4.4。如果是这样的话,我会质疑,如果这是1MB的空间。我想知道,如果你能在4.4设备/模拟器上运行你的应用程序,看看是否相同的内存使用。

This seems to imply that memory is used for system bitmaps/drawables in each app running a version prior to 4.4. If this is the case, I would question if this 1MB is that space. I wonder if you could run your app on a 4.4 device/emulator and see if the same memory is used.

作为另一项测试,你有没有尝试过检查内存使用上的准系统应用(所有可绘删除等)?

As another test, have you tried inspecting memory usage on a barebones app (all drawables removed, etc)?