由img.setBackgroundResource引起的内存泄漏

时间:2012-08-08 09:50:00

标签: java android animation garbage-collection android-animation


我有一个想要运行的动画,当我为它设置drawable时,会出现内存泄漏。我不知道它是否重要,但框架只在drawable文件夹中,而不是在特定的dpi文件夹中。甚至在我开始动画之前就会发生内存泄漏。当我设置动画的资源时会发生这种情况。 谢谢!

电话:

logoAnimation.setBackgroundResource(R.drawable.logo_animation);

logo_animation.xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">

    <item android:drawable="@drawable/logo0" android:duration="35" />
    <item android:drawable="@drawable/logo1" android:duration="35" />
    <item android:drawable="@drawable/logo2" android:duration="35" />
    <item android:drawable="@drawable/logo3" android:duration="35" />
    <item android:drawable="@drawable/logo4" android:duration="35" />
    <item android:drawable="@drawable/logo5" android:duration="35" />
    <item android:drawable="@drawable/logo6" android:duration="35" />
    <item android:drawable="@drawable/logo7" android:duration="35" />
    <item android:drawable="@drawable/logo8" android:duration="35" />
    <item android:drawable="@drawable/logo9" android:duration="35" />
    <item android:drawable="@drawable/logo10" android:duration="35" />
    <item android:drawable="@drawable/logo11" android:duration="35" />
    <item android:drawable="@drawable/logo12" android:duration="35" />
    <item android:drawable="@drawable/logo13" android:duration="35" />
    <item android:drawable="@drawable/logo14" android:duration="35" />
    <item android:drawable="@drawable/logo15" android:duration="35" />
    <item android:drawable="@drawable/logo16" android:duration="35" />
    <item android:drawable="@drawable/logo17" android:duration="35" />
    <item android:drawable="@drawable/logo18" android:duration="35" />
    <item android:drawable="@drawable/logo19" android:duration="35" />
    <item android:drawable="@drawable/logo20" android:duration="35" />
    <item android:drawable="@drawable/logo21" android:duration="35" />
    <item android:drawable="@drawable/logo22" android:duration="35" />
    <item android:drawable="@drawable/logo23" android:duration="35" />
    <item android:drawable="@drawable/logo24" android:duration="35" />
    <item android:drawable="@drawable/logo25" android:duration="35" />
    <item android:drawable="@drawable/logo26" android:duration="35" />
    <item android:drawable="@drawable/logo27" android:duration="35" />
    <item android:drawable="@drawable/logo28" android:duration="35" />
    <item android:drawable="@drawable/logo29" android:duration="35" />
    <item android:drawable="@drawable/logo30" android:duration="35" />
    <item android:drawable="@drawable/logo31" android:duration="35" />
    <item android:drawable="@drawable/logo32" android:duration="35" />
    <item android:drawable="@drawable/logo33" android:duration="35" />
    <item android:drawable="@drawable/logo34" android:duration="35" />
    <item android:drawable="@drawable/logo35" android:duration="35" />
    <item android:drawable="@drawable/logo36" android:duration="35" />
    <item android:drawable="@drawable/logo37" android:duration="35" />
    <item android:drawable="@drawable/logo38" android:duration="35" />
    <item android:drawable="@drawable/logo39" android:duration="35" />
    <item android:drawable="@drawable/logo40" android:duration="35" />
    <item android:drawable="@drawable/logo41" android:duration="35" />
    <item android:drawable="@drawable/logo42" android:duration="35" />
    <item android:drawable="@drawable/logo43" android:duration="35" />
    <item android:drawable="@drawable/logo44" android:duration="35" />
    <item android:drawable="@drawable/logo45" android:duration="35" />
    <item android:drawable="@drawable/logo46" android:duration="35" />
    <item android:drawable="@drawable/logo47" android:duration="35" />
    <item android:drawable="@drawable/logo48" android:duration="35" />
    <item android:drawable="@drawable/logo49" android:duration="35" />
    <item android:drawable="@drawable/logo50" android:duration="35" />
    <item android:drawable="@drawable/logo51" android:duration="35" />
    <item android:drawable="@drawable/logo52" android:duration="35" />
    <item android:drawable="@drawable/logo53" android:duration="35" />
    <item android:drawable="@drawable/logo54" android:duration="35" />
    <item android:drawable="@drawable/logo55" android:duration="35" />
    <item android:drawable="@drawable/logo56" android:duration="35" />
    <item android:drawable="@drawable/logo57" android:duration="35" />
    <item android:drawable="@drawable/logo58" android:duration="35" />
    <item android:drawable="@drawable/logo59" android:duration="35" />

</animation-list>

Logcat输出:

08-08 12:42:10.710: D/dalvikvm(6122): GC_CONCURRENT freed <1K, 3% free 10269K/10567K, paused 2ms+1ms
08-08 12:42:13.135: D/dalvikvm(6122): GC_FOR_ALLOC freed 115K, 3% free 10490K/10759K, paused 10ms
08-08 12:42:13.160: D/dalvikvm(6122): GC_FOR_ALLOC freed 130K, 4% free 10781K/11143K, paused 9ms
08-08 12:42:13.215: D/dalvikvm(6122): GC_FOR_ALLOC freed 195K, 4% free 11155K/11527K, paused 25ms
08-08 12:42:13.265: D/dalvikvm(6122): GC_FOR_ALLOC freed 130K, 4% free 11512K/11911K, paused 20ms
08-08 12:42:13.295: D/dalvikvm(6122): GC_FOR_ALLOC freed 195K, 4% free 11951K/12423K, paused 10ms
08-08 12:42:13.325: D/dalvikvm(6122): GC_FOR_ALLOC freed 260K, 4% free 12535K/12999K, paused 9ms
08-08 12:42:13.360: D/dalvikvm(6122): GC_FOR_ALLOC freed 325K, 4% free 13266K/13767K, paused 9ms
08-08 12:42:13.400: D/dalvikvm(6122): GC_FOR_ALLOC freed 391K, 4% free 14143K/14727K, paused 9ms
08-08 12:42:13.450: D/dalvikvm(6122): GC_FOR_ALLOC freed 521K, 5% free 15313K/16071K, paused 10ms
08-08 12:42:13.510: D/dalvikvm(6122): GC_FOR_ALLOC freed 587K, 5% free 16628K/17415K, paused 10ms
08-08 12:42:13.570: D/dalvikvm(6122): GC_FOR_ALLOC freed 587K, 5% free 17942K/18759K, paused 10ms

1 个答案:

答案 0 :(得分:0)

你正在加载大量的图像,大约60个。如果这些图像也比较大,这让我觉得内存泄漏是由OutOfMemoryException.

引起的

Here's a great article如何处理它。
简而言之,我们的想法是在将图像加载到内存之前缩小图像。