[英]Android memory leak?
I think my android app is leaking memory.我认为我的 android 应用程序正在泄漏内存。 I'm not absolutely sure that this is the problem though.
不过,我不确定这就是问题所在。
Every so often the app crashes when opening, and logcat shows an "out of memory" exception trying to load a bitmap image.应用程序在打开时经常崩溃,并且 logcat 会在尝试加载位图图像时显示“内存不足”异常。
After crashing, I re-open the app and it works fine.崩溃后,我重新打开应用程序,它工作正常。 Logcat shows lots of "gc"s and every once in a while the JIT table is resized upwards, never downwards until the app crashes with the out of memory error.
Logcat 显示大量“gc”,并且每隔一段时间 JIT 表都会向上调整大小,直到应用程序因内存不足错误而崩溃为止。
Does this sound like a memory leak?这听起来像内存泄漏吗? If so, how do I go about locating and closing the leak.
如果是这样,我该如何定位和关闭泄漏点。
Here is my adb shell meminfo for my app.这是我的应用程序的 adb shell meminfo。
** MEMINFO in pid 2691 [com.example.deepcliff] **
native dalvik other total
size: 23264 8839 N/A 32103
allocated: 12503 3826 N/A 16329
free: 168 5013 N/A 5181
(Pss): 2512 1395 13815 17722
(shared dirty): 2088 1844 5008 8940
(priv dirty): 2412 224 11316 13952
Objects
Views: 0 ViewRoots: 0
AppContexts: 0 Activities: 0
Assets: 2 AssetManagers: 2
Local Binders: 55 Proxy Binders: 13
Death Recipients: 1
OpenSSL Sockets: 0
SQL
heap: 129 MEMORY_USED: 129
PAGECACHE_OVERFLOW: 9 MALLOC_SIZE: 50
DATABASES
pgsz dbsz Lookaside(b) Dbname
1 14 10 webview.db
1 6 18 webviewCache.db
Asset Allocations
zip:/data/app/com.example.deepcliff-2.apk:/resources.arsc: 17K
Here are a couple of articles and posts, which probably help you to get on the right track:这里有几篇文章和帖子,它们可能会帮助您走上正轨:
Allocation tracker , which comes with Android SDK is very useful. Android SDK 自带的Allocation tracker非常有用。 Read Romain Guy's articles.
阅读 Romain Guy 的文章。 It helped me to track down pretty nasty leaks.
它帮助我追踪了非常讨厌的泄漏。 It also helps you to write better software.
它还可以帮助您编写更好的软件。 Eg I learned to create less objects, use more StringBuilder, and cache a lot more:
例如,我学会了创建更少的对象,使用更多的 StringBuilder,并缓存更多:
What Android tools and methods work best to find memory/resource leaks? 哪些 Android 工具和方法最适合查找内存/资源泄漏?
Sometimes your app is just so messed up that you have to re-design it in the whole.有时您的应用程序非常混乱,以至于您必须从整体上重新设计它。 Here are official, good hints for that (my favourite is the Avoid Creating Unnecessary Objects ):
这是官方的好提示(我最喜欢的是避免创建不必要的对象):
http://developer.android.com/guide/practices/design/performance.html http://developer.android.com/guide/practices/design/performance.html
Here's an excellent article about attacking your memory issues:这是一篇关于解决内存问题的优秀文章:
http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html http://ttlnews.blogspot.com/2010/01/attacking-memory-problems-on-android.html
Official article about avoiding memory leaks:关于避免内存泄漏的官方文章:
http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
Read also this: tool to check memory leaks in android另请阅读: 在 android 中检查内存泄漏的工具
Others already pointed about bitmaps .其他人已经指出了位图。 Here's an article describing the issue: http://zrgiu.com/blog/2011/01/android-bitmaps-and-out-of-memory-errors/
这是一篇描述该问题的文章:http: //zrgiu.com/blog/2011/01/android-bitmaps-and-out-of-memory-errors/
This is not a memory leak.这不是内存泄漏。 Android devices just have a limited amount of memory and your bitmaps must just be too big.
Android 设备的内存有限,您的位图必须太大。 You need to find a way to reduce the size of your bitmaps.
您需要找到一种方法来减小位图的大小。 I really can't tell you much more because you haven't given us much to go on.
我真的不能告诉你更多,因为你没有给我们太多的东西。
A typical value of max application VM heap size is 24 MB.最大应用程序 VM 堆大小的典型值为 24 MB。 So, for instance, if your image is 10Mpx (3600 x 2400), then it would allocate 3600 x 2400 x 4 = 34'560'000 bytes which is an
OutOfMemoryError
case.因此,例如,如果您的图像是 10Mpx (3600 x 2400),那么它将分配 3600 x 2400 x 4 = 34'560'000 字节,这是
OutOfMemoryError
情况。
When dealing with Bitmaps in android, make sure you recycle the bitmap whenever you are done using it.在android中处理位图时,请确保在使用完位图时回收位图。 You can load a resized bitmap by setting the inSampleSize option.
您可以通过设置 inSampleSize 选项来加载调整大小的位图。 More details here: http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize
更多细节在这里: http : //developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.