GC太多会影响设备电池吗?

时间:2015-08-09 09:37:54

标签: android bitmap garbage-collection

在Android应用程序中我写的(关于国际象棋),我在Singleton类中使用LruCache作为位图。但是每次我从一个活动切换到另一个活动时,adb logcat似乎报告了许多GC,这两个活动都以较低的数量保存了棋子图像。 (或者当我离开/重新进入申请时调用GC)。用户可能不得不经常在这些活动之间切换。

这是logcat输出:

08-09 11:20:26.161    1583-1583/? D/AndroidRuntime﹕ >>>>>>     AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-09 11:20:26.161    1583-1583/? D/AndroidRuntime﹕ CheckJNI is ON
08-09 11:20:26.244    1583-1583/? D/AndroidRuntime﹕ Calling main entry     com.android.commands.pm.Pm
08-09 11:20:26.251      878-895/? W/ActivityManager﹕ No content     provider found for permission revoke:     file:///data/local/tmp/com.loloof64.android.chess_positions_archiver
08-09 11:20:26.291      878-895/? W/ActivityManager﹕ No content     provider found for permission revoke:     file:///data/local/tmp/com.loloof64.android.chess_positions_archiver
08-09 11:20:26.343      878-879/? D/dalvikvm﹕ GC_CONCURRENT freed     299K, 13% free 11370K/12935K, paused 1ms+1ms
08-09 11:20:26.421      878-879/? D/dalvikvm﹕ GC_CONCURRENT freed     574K, 13% free 11378K/12935K, paused 1ms+2ms
08-09 11:20:26.496      878-895/? I/PackageManager﹕ Removing non-    system package:com.loloof64.android.chess_positions_archiver
08-09 11:20:26.496      878-892/? I/ActivityManager﹕ Force stopping     package com.loloof64.android.chess_positions_archiver uid=10041
08-09 11:20:26.581      878-879/? D/dalvikvm﹕ GC_CONCURRENT freed     415K, 12% free 11424K/12935K, paused 1ms+1ms
08-09 11:20:26.581      878-895/? I/PackageManager﹕ Package     com.loloof64.android.chess_positions_archiver codePath changed from     /data/app/com.loloof64.android.chess_positions_archiver-1.apk to     /data/app/com.loloof64.android.chess_positions_archiver-2.apk; Retaining     data and using new
08-09 11:20:26.581      878-895/? I/PackageManager﹕ Running dexopt on:     com.loloof64.android.chess_positions_archiver
08-09 11:20:26.742    1595-1595/? D/dalvikvm﹕ DexOpt: load 11ms,     verify+opt 80ms
08-09 11:20:26.751      878-892/? I/ActivityManager﹕ Force stopping     package com.loloof64.android.chess_positions_archiver uid=10041
08-09 11:20:26.751      878-895/? W/PackageManager﹕ Code path for pkg     : com.loloof64.android.chess_positions_archiver changing from     /data/app/com.loloof64.android.chess_positions_archiver-1.apk to     /data/app/com.loloof64.android.chess_positions_archiver-2.apk
08-09 11:20:26.751      878-895/? W/PackageManager﹕ Resource path for     pkg : com.loloof64.android.chess_positions_archiver changing from     /data/app/com.loloof64.android.chess_positions_archiver-1.apk to /data/app/com.loloof64.android.chess_positions_archiver-2.apk
08-09 11:20:26.771      878-895/? D/PackageManager﹕ New package     installed in /data/app/com.loloof64.android.chess_positions_archiver-2.apk
08-09 11:20:26.791      878-895/? I/ActivityManager﹕ Force stopping package com.loloof64.android.chess_positions_archiver uid=10041
08-09 11:20:26.801      990-990/? D/dalvikvm﹕ GC_EXPLICIT freed 230K,     8% free 11366K/12295K, paused 1ms+5ms
08-09 11:20:26.811    1025-1025/? D/dalvikvm﹕ GC_EXPLICIT freed 162K, 4% free 9374K/9735K, paused 1ms+1ms
08-09 11:20:26.861    1113-1114/? D/dalvikvm﹕ GC_CONCURRENT freed 177K, 3% free 9298K/9543K, paused 2ms+1ms
08-09 11:20:26.861    1113-1129/? I/AccountTypeManager﹕ Loaded meta-data for 1 account types, 0 accounts in 9ms(wall) 0ms(cpu)
08-09 11:20:26.922    1113-1129/? I/AccountTypeManager﹕ Loaded meta-data for 1 account types, 0 accounts in 1ms(wall) 0ms(cpu)
08-09 11:20:26.961      878-891/? D/dalvikvm﹕ GC_EXPLICIT freed 499K, 14% free 11235K/12935K, paused 1ms+1ms
08-09 11:20:26.972      878-891/? D/PackageManager﹕ generateServicesMap(android.accounts.AccountAuthenticator): 2 services unchanged
08-09 11:20:26.981      878-891/? D/PackageManager﹕ generateServicesMap(android.content.SyncAdapter): 4 services unchanged
08-09 11:20:26.981      878-891/? D/BackupManagerService﹕ Received broadcast Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:com.loloof64.android.chess_positions_archiver flg=0x10000010 (has extras) }
08-09 11:20:26.981      878-891/? D/PackageManager﹕ generateServicesMap(android.accounts.AccountAuthenticator): 2 services unchanged
08-09 11:20:26.981      878-891/? D/PackageManager﹕ generateServicesMap(android.content.SyncAdapter): 4 services unchanged
08-09 11:20:26.991      878-891/? D/BackupManagerService﹕ Received broadcast Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.loloof64.android.chess_positions_archiver flg=0x10000010 (has extras) }
08-09 11:20:26.991      878-891/? V/BackupManagerService﹕ updatePackageParticipantsLocked: #1
08-09 11:20:27.022      878-891/? W/RecognitionManagerService﹕ no available voice recognition services found
08-09 11:20:27.061      878-891/? D/BackupManagerService﹕ Received broadcast Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:com.loloof64.android.chess_positions_archiver flg=0x10000010 (has extras) }
08-09 11:20:27.061      878-891/? V/BackupManagerService﹕ updatePackageParticipantsLocked: #1
08-09 11:20:27.092      878-895/? D/dalvikvm﹕ GC_EXPLICIT freed 413K, 13% free 11375K/12935K, paused 1ms+6ms
08-09 11:20:27.128    1583-1583/? D/AndroidRuntime﹕ Shutting down VM
08-09 11:20:27.128    1583-1588/? D/dalvikvm﹕ GC_CONCURRENT freed 96K, 79% free 441K/2048K, paused 0ms+0ms
08-09 11:20:27.411    1604-1604/? D/AndroidRuntime﹕ >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-09 11:20:27.411    1604-1604/? D/AndroidRuntime﹕ CheckJNI is ON
08-09 11:20:27.461    1604-1604/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am
08-09 11:20:27.471      878-886/? I/ActivityManager﹕ START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.loloof64.android.chess_positions_archiver/.ChessPositionsArchiverActivity} from pid 1604
08-09 11:20:27.471      878-886/? W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21005
08-09 11:20:27.481    1604-1604/? D/AndroidRuntime﹕ Shutting down VM
08-09 11:20:27.491    1604-1609/? D/dalvikvm﹕ GC_CONCURRENT freed 98K, 78% free 470K/2048K, paused 1ms+0ms
08-09 11:20:27.491    1615-1615/? D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
08-09 11:20:27.491      878-962/? I/ActivityManager﹕ Start proc com.loloof64.android.chess_positions_archiver for activity com.loloof64.android.chess_positions_archiver/.ChessPositionsArchiverActivity: pid=1615 uid=10041 gids={}
08-09 11:20:27.511      878-915/? W/NetworkManagementSocketTagger﹕ setKernelCountSet(10041, 1) failed with errno -2
08-09 11:20:27.551      785-785/? D/dalvikvm﹕ GC_EXPLICIT freed 35K, 4% free 9038K/9347K, paused 1ms+0ms
08-09 11:20:27.551      785-785/? D/dalvikvm﹕ GC_EXPLICIT freed <1K, 4% free 9038K/9347K, paused 0ms+1ms
08-09 11:20:27.561      785-785/? D/dalvikvm﹕ GC_EXPLICIT freed <1K, 4% free 9038K/9347K, paused 1ms+1ms
08-09 11:20:27.640    1615-1619/? D/dalvikvm﹕ GC_CONCURRENT freed 243K, 4% free 9237K/9607K, paused 1ms+1ms
08-09 11:20:27.711    1615-1615/? D/dalvikvm﹕ GREF has increased to 201
08-09 11:20:27.774    1615-1619/? D/dalvikvm﹕ GC_CONCURRENT freed 367K, 6% free 9304K/9799K, paused 1ms+1ms
08-09 11:20:27.861    1615-1615/? I/ChessPositionsArchiver﹕ Application positions directory already present
08-09 11:20:27.891    1615-1619/? D/dalvikvm﹕ GC_CONCURRENT freed 398K, 6% free 9338K/9863K, paused 1ms+1ms
08-09 11:20:28.051      878-891/? I/Process﹕ Sending signal. PID: 1615 SIG: 3
08-09 11:20:28.051    1615-1620/? I/dalvikvm﹕ threadid=3: reacting to signal 3
08-09 11:20:28.051    1615-1620/? I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
08-09 11:20:28.111    1615-1615/? D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
08-09 11:20:28.121      878-905/? I/ActivityManager﹕ Displayed com.loloof64.android.chess_positions_archiver/.ChessPositionsArchiverActivity: +657ms
08-09 11:20:28.141      878-915/? W/NetworkManagementSocketTagger﹕ setKernelCountSet(10004, 0) failed with errno -2
08-09 11:20:38.051    1615-1615/? D/dalvikvm﹕ GC_FOR_ALLOC freed 154K, 4% free 9554K/9927K, paused 3ms
08-09 11:20:38.061    1615-1615/? D/dalvikvm﹕ GC_FOR_ALLOC freed 52K, 4% free 9828K/10183K, paused 3ms
08-09 11:20:45.862      878-886/? I/ActivityManager﹕ START {cmp=com.loloof64.android.chess_positions_archiver/.PositionEditorActivity (has extras)} from pid 1615
08-09 11:20:45.862      878-886/? W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21015
08-09 11:20:45.891    1615-1619/? D/dalvikvm﹕ GC_CONCURRENT freed 451K, 6% free 9865K/10439K, paused 0ms+1ms
08-09 11:20:45.941      878-905/? I/ActivityManager﹕ Displayed com.loloof64.android.chess_positions_archiver/.PositionEditorActivity: +74ms
08-09 11:21:00.651      878-885/? W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21015
08-09 11:21:02.571    1615-1615/? D/dalvikvm﹕ GC_FOR_ALLOC freed 132K, 4% free 10111K/10439K, paused 4ms
08-09 11:21:14.021     878-1018/? W/InputManagerService﹕ Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@b5b4dce8
08-09 11:21:19.231     878-1051/? I/ActivityManager﹕ START {cmp=com.loloof64.android.chess_positions_archiver/.PositionEditorActivity (has extras)} from pid 1615
08-09 11:21:19.231     878-1051/? W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21015
08-09 11:21:19.252    1615-1619/? D/dalvikvm﹕ GC_CONCURRENT freed 591K, 7% free 9927K/10631K, paused 0ms+0ms
08-09 11:21:19.292      878-905/? I/ActivityManager﹕ Displayed com.loloof64.android.chess_positions_archiver/.PositionEditorActivity: +65ms
08-09 11:21:44.382      878-885/? W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21015
08-09 11:22:08.213    1615-1619/? D/dalvikvm﹕ GC_CONCURRENT freed 107K, 4% free 10210K/10631K, paused 1ms+5ms
08-09 11:22:14.974     878-1050/? I/ActivityManager﹕ START {cmp=com.loloof64.android.chess_positions_archiver/.PositionEditorActivity (has extras)} from pid 1615
08-09 11:22:14.974     878-1050/? W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21015
08-09 11:22:15.053      878-905/? I/ActivityManager﹕ Displayed com.loloof64.android.chess_positions_archiver/.PositionEditorActivity: +70ms
08-09 11:22:24.142     878-1049/? W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21015
08-09 11:22:31.112     878-1048/? I/ActivityManager﹕ START {cmp=com.loloof64.android.chess_positions_archiver/.PositionEditorActivity (has extras)} from pid 1615
08-09 11:22:31.112     878-1048/? W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21015
08-09 11:22:31.142    1615-1619/? D/dalvikvm﹕ GC_CONCURRENT freed 632K, 8% free 9999K/10759K, paused 1ms+0ms
08-09 11:22:31.203      878-905/? I/ActivityManager﹕ Displayed com.loloof64.android.chess_positions_archiver/.PositionEditorActivity: +85ms
08-09 11:22:31.203      878-879/? D/dalvikvm﹕ GC_CONCURRENT freed 503K, 13% free 11336K/12935K, paused 1ms+0ms
08-09 11:22:38.042      878-886/? W/WindowManager﹕ Failure taking screenshot for (180x300) to layer 21015

因此,如此多的GC通话,我是否一定要担心用户的电池性能?或者这不是很糟糕,所以我可以把它留下来?

顺便说一下,这一行到底意味着什么呢?

GC_CONCURRENT freed 402K, 6% free 9529K/10055K, paused 1ms+1ms

这意味着这6%的免费是什么意思(在gc之前它是免费的剩余内存吗?) 如何解释暂停时间(1ms + 1ms)?

1 个答案:

答案 0 :(得分:1)

GC 是一项昂贵的操作,然后我认为你应该关心那些很多日志消息。

请参阅Managing Bitmap memory以查看Android的提示。

另请参阅this answer以了解GC日志消息。

  

GC_FOR_MALLOC表示已触发GC,因为堆上没有足够的内存来执行分配。在创建新对象时可能会触发。

     

GC_EXPLICIT表示已明确要求收集垃圾收集器,而不是由堆中的高水位标记触发。发生在整个地方,但很可能是在一个线程被杀死或者绑定器通信被取消时。

     

还有其他几个:

     

GC_CONCURRENT当堆已达到要收集的特定数量的对象时触发。

     

GC_EXTERNAL_ALLOC表示虚拟机正在尝试减少可收集对象使用的内存量,以便为更多不可收集的内容腾出空间。

     

更新:在Android的更高版本中,第一个事件发生了名称更改。它现在称为“GC_FOR_ALLOC”。还有一个新的事件,虽然在现代手机中非常罕见:GC_BEFORE_OOM意味着系统内存运行非常低,并且执行了最终的GC,以避免调用低内存杀手。