gpt4 book ai didi

Android 垃圾收集运行异常

转载 作者:行者123 更新时间:2023-11-30 04:15:40 25 4
gpt4 key购买 nike

我在 Activity 中有以下代码。最终,我正在尝试制作帧动画,但 GC 正在扼杀性能。我已经尝试过显式回收,但没有任何区别。

我也知道我可以通过 BitmapFactor.Options.inBitmap 重用位图,这确实解决了问题,但限制我使用 API >= 11,这不是一个选项。

这段代码只是说明了问题。它不是我的应用程序的一部分:

        Log.d("GC_badness", "0: " + SystemClock.uptimeMillis());
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
Log.d("GC_badness", "1: " + SystemClock.uptimeMillis());
image = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
Log.d("GC_badness", "2: " + SystemClock.uptimeMillis());
image = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
Log.d("GC_badness", "3: " + SystemClock.uptimeMillis());
image = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
Log.d("GC_badness", "4: " + SystemClock.uptimeMillis());
image = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
Log.d("GC_badness", "5: " + SystemClock.uptimeMillis());
image = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
Log.d("GC_badness", "6: " + SystemClock.uptimeMillis());

这会产生以下日志条目。每个从 jpg 资源加载的位图都会导致至少 3 次垃圾回收,最多可达 8 次。大多数情况下,它们什么都不回收或几乎什么都不回收。堆没有增长,即使有多条消息说它在增长。这也很令人困惑。

我被难住了。我找不到重用 API 11 之前的位图的方法,而且我无法忍受所有这些无用的垃圾回收造成的巨大延迟。

04-07 18:17:51.860: D/GC_badness(7510): 0: 360583998
04-07 18:17:51.900: D/dalvikvm(7510): GC_FOR_ALLOC freed 34K, 5% free 6320K/6595K, paused 32ms
04-07 18:17:51.900: I/dalvikvm-heap(7510): Grow heap (frag case) to 7.744MB for 1584016-byte allocation
04-07 18:17:51.950: D/dalvikvm(7510): GC_FOR_ALLOC freed <1K, 5% free 7867K/8199K, paused 27ms
04-07 18:17:52.000: D/dalvikvm(7510): GC_CONCURRENT freed <1K, 5% free 7868K/8199K, paused 2ms+3ms
04-07 18:17:52.030: D/dalvikvm(7510): GC_FOR_ALLOC freed <1K, 5% free 7868K/8199K, paused 31ms
04-07 18:17:52.030: I/dalvikvm-heap(7510): Grow heap (frag case) to 8.416MB for 704016-byte allocation
04-07 18:17:52.070: D/dalvikvm(7510): GC_FOR_ALLOC freed <1K, 4% free 8555K/8903K, paused 37ms
04-07 18:17:52.070: D/dalvikvm(8107): GC_FOR_ALLOC freed 1464K, 39% free 17183K/27911K, paused 62ms
04-07 18:17:52.100: D/GC_badness(7510): 1: 360584238
04-07 18:17:52.100: D/dalvikvm(8189): GC_CONCURRENT freed 286K, 8% free 6917K/7495K, paused 17ms+3ms
04-07 18:17:52.110: D/dalvikvm(7510): GC_CONCURRENT freed 1546K, 22% free 7009K/8903K, paused 2ms+2ms
04-07 18:17:52.150: D/dalvikvm(7510): GC_FOR_ALLOC freed <1K, 22% free 7008K/8903K, paused 32ms
04-07 18:17:52.150: I/dalvikvm-heap(7510): Grow heap (frag case) to 8.416MB for 1584016-byte allocation
04-07 18:17:52.200: D/dalvikvm(7510): GC_FOR_ALLOC freed 0K, 4% free 8555K/8903K, paused 37ms
04-07 18:17:52.250: D/dalvikvm(7510): GC_CONCURRENT freed <1K, 4% free 8555K/8903K, paused 2ms+2ms
04-07 18:17:52.280: D/dalvikvm(7510): GC_FOR_ALLOC freed <1K, 4% free 8555K/8903K, paused 30ms
04-07 18:17:52.280: I/dalvikvm-heap(7510): Grow heap (frag case) to 9.086MB for 704016-byte allocation
04-07 18:17:52.310: D/dalvikvm(7510): GC_FOR_ALLOC freed 0K, 4% free 9243K/9607K, paused 23ms
04-07 18:17:52.330: D/dalvikvm(8107): GC_CONCURRENT freed 588K, 34% free 18618K/27911K, paused 4ms+5ms
04-07 18:17:52.350: D/GC_badness(7510): 2: 360584482
04-07 18:17:52.350: D/dalvikvm(7510): GC_CONCURRENT freed 1546K, 20% free 7696K/9607K, paused 1ms+2ms
04-07 18:17:52.380: D/dalvikvm(7510): GC_FOR_ALLOC freed 688K, 28% free 7008K/9607K, paused 22ms
04-07 18:17:52.380: I/dalvikvm-heap(7510): Grow heap (frag case) to 8.416MB for 1584016-byte allocation
04-07 18:17:52.400: D/dalvikvm(7510): GC_FOR_ALLOC freed <1K, 11% free 8555K/9607K, paused 21ms
04-07 18:17:52.440: D/GC_badness(7510): 3: 360584577
04-07 18:17:52.450: D/dalvikvm(7510): GC_CONCURRENT freed <1K, 4% free 9243K/9607K, paused 1ms+2ms
04-07 18:17:52.470: D/dalvikvm(7510): GC_FOR_ALLOC freed 2234K, 28% free 7008K/9607K, paused 20ms
04-07 18:17:52.470: I/dalvikvm-heap(7510): Grow heap (frag case) to 8.416MB for 1584016-byte allocation
04-07 18:17:52.500: D/dalvikvm(7510): GC_FOR_ALLOC freed 0K, 11% free 8555K/9607K, paused 29ms
04-07 18:17:52.510: D/dalvikvm(8107): GC_CONCURRENT freed 1785K, 33% free 18832K/27911K, paused 2ms+5ms
04-07 18:17:52.530: D/GC_badness(7510): 4: 360584668
04-07 18:17:52.540: D/dalvikvm(7510): GC_CONCURRENT freed <1K, 4% free 9243K/9607K, paused 1ms+2ms
04-07 18:17:52.570: D/dalvikvm(7510): GC_FOR_ALLOC freed 2234K, 28% free 7008K/9607K, paused 28ms
04-07 18:17:52.570: I/dalvikvm-heap(7510): Grow heap (frag case) to 8.416MB for 1584016-byte allocation
04-07 18:17:52.590: D/dalvikvm(7510): GC_FOR_ALLOC freed <1K, 11% free 8555K/9607K, paused 22ms
04-07 18:17:52.620: D/dalvikvm(7510): GC_CONCURRENT freed <1K, 4% free 9243K/9607K, paused 3ms+2ms
04-07 18:17:52.630: D/GC_badness(7510): 5: 360584765
04-07 18:17:52.650: D/dalvikvm(7510): GC_FOR_ALLOC freed 2235K, 28% free 7008K/9607K, paused 21ms
04-07 18:17:52.650: I/dalvikvm-heap(7510): Grow heap (frag case) to 8.416MB for 1584016-byte allocation
04-07 18:17:52.680: D/dalvikvm(7510): GC_FOR_ALLOC freed 0K, 11% free 8555K/9607K, paused 27ms
04-07 18:17:52.710: D/GC_badness(7510): 6: 360584844

最佳答案

为什么要重复使用图像?

不要给垃圾收集器收集东西的机会。基本上,您在本地定义的每个对象迟早都会被收集。

不要重复使用您的图像变量。在你的动画代码之前制作一个图像数组和图像(最好是在创建 Activity 期间加载)。或者考虑使用 ObjectPool为您的图片。

这是一个很好的SO answer关于如何避免垃圾收集。

最后,您可以在谷歌搜索时找到大量有关如何避免垃圾收集的信息。 “如何避免垃圾回收 android”。

关于Android 垃圾收集运行异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10059545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com