Drawables 本身会泄漏内存?
我正在读这个问题因为我必须将大量下载的资源加载到我正在编写的应用程序中,并且很好奇这样做是否会对性能造成巨大影响[与其他应用程序相比]。将它们放在 .apk 中],问题的“正确”答案意味着虽然性能没有下降,但您必须担心在使用完毕后释放内存,以免泄漏。
谁能自信地证实或否认这一点?我的印象是,当 Activity 被清理时,加载的 Drawable 会像其他所有东西一样被 GC。我非常想知道这是否不是真的,以及在所述实例中手动收集内存的最可靠方法是什么。
另外,有谁知道从 SDCard 加载图像与从手机内存加载图像相比,性能是否会受到明显影响。我不是电气工程师,所以,直觉上,似乎因为这都是固态存储器,所以应该以大致相同的速度读取,但我很想得到一个明确的答案。
I'm reading this question because I have to load a ton of downloaded resources into an app I'm writing, and was curious if there was a dramatic performance hit in doing so [vs. having them in the .apk], and the "correct" answer to the question implies that while there is no performance degradation, you have to worry about releasing the memory back when you're done with it, lest it leak.
Can anyone confidently confirm or deny this? My impression was that a loaded Drawable was GCed just like everything else when the Activity it was cleaned up. I'd very much like to know if that's not true, and what the most reliable way to manually collect the memory in said instance is.
Also, does anyone know if there's a noticeable performance hit in loading images from the SDCard, vs. from the phone's memory. I'm not an electrical engineer, so, intuitively, it seems like since this is all solid state memory, it should all get read at about the same pace, but I'd love to get a definitive answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速回答:
位图需要两次垃圾收集器来清理。第一遍释放 Java 对象,第二遍释放本机像素数据。它们不会泄漏,但在清空指针和 GC 对其进行第二次遍历之间可能会耗尽内存。无论它们来自什么资源,都是如此。当您确定您和系统都已完成处理位图时,在位图上调用 recycle() 总是一个好主意。
由于 Dalvik VM 中的错误,Gingerbread 在处理内存不足问题和位图方面尤其糟糕。
根据我的经验,从 apk 加载图像比从 SD 卡加载图像要快得多。
1)它们在apk中是对齐的(如果你对齐你的apk,你应该这样做)
2)不同手机对SD卡的访问时间不同。一般规则是,如果它位于 SD 卡上,则加载速度会很慢。您可以在主线程上从内部存储器加载可绘制对象(尽管这是一个坏主意)。您无法在主线程上从 SD 卡加载任何内容。曾经:-\
如果我是你,加载图像时我会尽可能懒惰,如果可能的话,我会将它们保留在 apk 中。
Quick answer:
Bitmaps take two passes of the garbage collector to clean up. The first pass releases the Java object, the second pass the native pixel data. They don't leak, but you can run out of memory between when you null the pointers and the GC hits its second pass over them. This is true no matter what resource they come from. It's always a good idea to call recycle() on a bit map when you're sure both you, and the system, are done with them.
Gingerbread is particularly bad in dealing with out of memory issues and bitmaps due to a bug in the Dalvik VM.
In my experience, loading images out of the apk is MUCH faster than off the SD card.
1) They're zip aligned in the apk (if you align your apk, which you should)
2) Different phones have different access times to the SD card. The general rule is, if it's on the sd card, it's going to load SLOWLY. You can get away with loading drawables from the internal memory on the main thread (even though it's a bad idea). You cannot load anything from the SD card on the main thread. Ever :-\
If I were you, I'd be as lazy as possible when loading images, I'd keep them in the apk if possible.