看似CPU高实则内存高的ANR问题

有时候遇到一个ANR,可能单看ANR报错还有点无从下手,那么还需要更深入的分析一下日志,从logcat、traces.txt等,如果trace.txt没看出来啥,就多啃啃logcat信息,一定能有所发现。记一次看似CPU消耗,实则是内存不足引起的ANR:

  1. 遇到ANR,logcat报错点如下
E ActivityManager: ANR in com.xin.rx
E ActivityManager: PID: 22280
E ActivityManager: Reason: Broadcast of Intent { act=android.intent.action.TIME_TICK flg=0x50000014 (has extras) }
E ActivityManager: Load: 17.51 / 15.34 / 12.68
#这里能看到CPU消耗高太高
E ActivityManager: CPU usage from 0ms to 10918ms later (2019-05-16 20:24:11.724 to 2019-05-16 20:24:22.642) with 99% awake:
E ActivityManager:   135% 3802/businessxin: 98% user + 37% kernel / faults: 15878 minor 463 major
#打印内存交换
E ActivityManager:   83% 173/kswapd0: 0% user + 83% kernel
E ActivityManager:   26% 1619/system_server: 4.4% user + 21% kernel / faults: 27401 minor 1591 major
E ActivityManager:   21% 376/mmc-cmdqd/0: 0% user + 21% kernel
E ActivityManager:   16% 22280/com.xin.rx: 1.7% user + 14% kernel / faults: 23542 minor 2243 major
E ActivityManager:   10% 26976/com.android.deskclock: 0.4% user + 9.7% kernel / faults: 17079 minor 1641 major
E ActivityManager:   9.9% 833/mm-qcamera-daemon: 6% user + 3.9% kernel / faults: 7351 minor 10 major
E ActivityManager:   7.4% 26972/app_process: 1.8% user + 5.5% kernel / faults: 22889 minor 1612 major
E ActivityManager:   5.1% 2572/.dataservices: 0.4% user + 4.6% kernel / faults: 5551 minor 1004 major
E ActivityManager:   4.3% 821/netd: 1.1% user + 3.1% kernel / faults: 942 minor
E ActivityManager:   3.8% 3958/networkxin 2.5% kernel / faults: 6581 minor 150 major
E ActivityManager:   3.2% 2448/VosRXThread: 0% user + 3.2% kernel
E ActivityManager:   3% 2031/com.android.phone: 0% user + 3% kernel / faults: 10635 minor 514 major
E ActivityManager:   2.9% 1889/com.android.systemui: 0.3% user + 2.6% kernel / faults: 7938 minor 316 major
E ActivityManager:   2.8% 2447/VosTXThread: 0% user + 2.8% kernel
E ActivityManager:   2.7% 404/logd: 0.2% user + 2.4% kernel / faults: 1720 minor 9 major
E ActivityManager:   2.6% 9183/kworker/u16:1: 0% user + 2.6% kernel
E ActivityManager:   2.4% 30214/kworker/u16:2: 0% user + 2.4% kernel
E ActivityManager:   2.4% 21442/kworker/u16:7: 0% user + 2.4% kernel
E ActivityManager:   2.1% 4023/business2: 0.8% user + 1.3% kernel / faults: 5682 minor 88 major
E ActivityManager:   1.7% 22277/kworker/u16:3: 0% user + 1.7% kernel
E ActivityManager:   1.2% 387/kworker/5:1H: 0% user + 1.2% kernel
E ActivityManager:   1.1% 50/ksoftirqd/5: 0% user + 1.1% kernel
E ActivityManager:   1.1% 2612/com.qualcomm.qti.services.secureui:sui_service: 0.3% user + 0.8% kernel / faults: 2156 minor 200 major
E ActivityManager:   0% 2602/com.oma.drm.server: 0% user + 0% kernel / faults: 2467 minor 129 major
E ActivityManager:   0% 2580/com.qualcomm.qti.loadcarrier: 0% user + 0% kernel / faults: 2221 minor 129 major
E ActivityManager:   0.5% 6/kworker/u16:0: 0% user + 0.5% kernel
E ActivityManager:   0.5% 2559/com.android.nfc: 0.2% user + 0.2% kernel / faults: 2000 minor 97 major
E ActivityManager:   0.4% 822/rild: 0% user + 0.4% kernel / faults: 571 minor 51 major
E ActivityManager:   0.4% 1943/sdcard: 0% user + 0.4% kernel / faults: 714 minor 3 major
E ActivityManager:   0.4% 4005/streamaxfcgi: 0.2% user + 0.1% kernel / faults: 1959 minor 20 major
E ActivityManager:   0.3% 380/usb-storage: 0% user + 0.3% kernel
E ActivityManager:   0.2% 7/rcu_preempt: 0% user + 0.2% kernel
E ActivityManager:   0.2% 61/rcuop/6: 0% user + 0.2% kernel
E ActivityManager:   0.2% 66/ksoftirqd/7: 0% user + 0.2% kernel
E ActivityManager:   0.2% 706/jbd2/dm-0-8: 0% user + 0.2% kernel
E ActivityManager:   0.2% 26127/kworker/5:2: 0% user + 0.2% kernel
E ActivityManager:   0.1% 45/rcuop/4: 0% user + 0.1% kernel
E ActivityManager:   0.1% 69/rcuop/7: 0% user + 0.1% kernel
E ActivityManager:   0.1% 381/mmcqd/1: 0% user + 0.1% kernel
E ActivityManager:   0% 414/debuggerd: 0% user + 0% kernel / faults: 427 minor 41 major
E ActivityManager:   0.1% 415/debuggerd64: 0% user + 0.1% kernel / faults: 413 minor 16 major
E ActivityManager:   0.1% 546/surfaceflinger: 0% user + 0.1% kernel / faults: 426 minor 20 major
E ActivityManager:   0.1% 570/logcat: 0.1% user + 0% kernel / faults: 1055 minor 1 major
E ActivityManager:   0% 2444/VosWDThread: 0% user + 0% kernel
E ActivityManager:   0.1% 3742/logcat: 0% user + 0% kernel / faults: 1062 minor 1 major
E ActivityManager:   0.1% 22285/kworker/4:1: 0% user + 0.1% kernel
E ActivityManager:   0.1% 26585/kworker/0:0: 0% user + 0.1% kernel
E ActivityManager:   0% 3/ksoftirqd/0: 0% user + 0% kernel
E ActivityManager:   0% 10/rcuop/0: 0% user + 0% kernel
E ActivityManager:   0% 18/ksoftirqd/1: 0% user + 0% kernel
E ActivityManager:   0% 29/rcuop/2: 0% user + 0% kernel
E ActivityManager:   0% 34/ksoftirqd/3: 0% user + 0% kernel
E ActivityManager:   0% 37/rcuop/3: 0% user + 0% kernel
E ActivityManager:   0% 42/ksoftirqd/4: 0% user + 0% kernel
E ActivityManager:   0% 53/rcuop/5: 0% user + 0% kernel
E ActivityManager:   0% 337/irq/162-spdm_bw: 0% user + 0% kernel
E ActivityManager:   0% 405/kworker/0:1H: 0% user + 0% kernel
E ActivityManager:   0% 416/vold: 0% user + 0% kernel / faults: 418 minor 11 major
E ActivityManager:   0% 422/kworker/6:1H: 0% user + 0% kernel
E ActivityManager:   0% 545/servicemanager: 0% user + 0% kernel / faults: 778 minor 8 major
E ActivityManager:   0% 703/kworker/1:1H: 0% user + 0% kernel
E ActivityManager:   0% 722/ptt_socket_app: 0% user + 0% kernel / faults: 192 minor 8 major
E ActivityManager:   0% 724/cnss_diag: 0% user + 0%
I art     : Wrote stack traces to '/data/anr/traces.txt'
I art     : Wrote stack traces to '/data/anr/traces.txt'
E pttSocketApp:          aniAsfIpc.c:971 Error(105):Recv failed, ipcPtr [0xad631000], sd 4
E pttSocketApp:       pttSocketApp.c:759 IPC Recv failed

看到该日志的ANR,是从Android应用里爆出来的,但怎么看都不像是这个应用的问题

  1. 查看ANR信息:
  • 发现有cpu相关信息,CPU usage,说明是当时cpu负载太高,应用分的时间片太少,执行不及时造成的假象ANR
  • 再看kswapd0: 0% user + 83% kernel:swap分区有一个作用,在内存不足的时候,硬盘上一部分当做虚拟内存来使用,而kswapd0进程,一般是内存不足了,使用swap分区与内存换页操作交换数据,十分耗CPU,就导致了CPU的高负荷

知道了CPU为什么高负荷,是因为内存不足,但是没有其他信息了啊,还得继续分析

  1. 从ANR日志时间点往前看
ActivityManager: Low on memory:
ActivityManager:   ntv   N   763042: networkxin (pid 3958) native
ActivityManager:   ntv   N    61606: businessxin (pid 3802) native
ActivityManager:   ntv   N    42253: mm-qcamera-daemon (    3,724K memtrack) (pid 833) native
ActivityManager:   ntv   N    29819: surfaceflinger (   17,808K memtrack) (pid 546) native
ActivityManager:   ntv   N    10412: media.codec (      504K memtrack) (pid 817) native
ActivityManager:   ntv   N     7871: app_process (pid 27764) native
ActivityManager:   ntv   N     7434: netmgrd (pid 908) native
ActivityManager:   ntv   N     7269: audioserver (pid 808) native
ActivityManager:   ntv   N     6645: rild (pid 822) native
ActivityManager:   ntv   N     6068: <pre-initialized> (pid 27772) native
ActivityManager:   ntv   N     5793: logd (pid 404) native
ActivityManager:   ntv   N     4913: business2 (pid 4023) native
ActivityManager:   ntv   N     4770: cnd (pid 720) native
ActivityManager:   ntv   N     4691: zygote (pid 807) native
ActivityManager:   ntv   N     4275: com.android.calendar (pid 27786) native
ActivityManager:   ntv   N     4131: mm-pp-dpps (pid 575) native
ActivityManager:   ntv   N     4095: zygote64 (pid 743) native
ActivityManager:   ntv   N     4026: qseeproxydaemon (pid 831) native
ActivityManager:   ntv   N     3758: streamaxfcgi (pid 4005) native
ActivityManager:   ntv   N     3387: xtwifi-client (pid 1534) native
ActivityManager:   ntv   N     3333: cameraserver (pid 811) native
ActivityManager:   ntv   N     2933: daemon (pid 1151) native
ActivityManager:   ntv   N     2735: media.extractor (pid 819) native
ActivityManager:   ntv   N     2525: vold (pid 416) native
ActivityManager:   ntv   N     2124: mediaserver (pid 820) native
ActivityManager:   ntv   N     2033: slim_daemon (pid 1535) native
ActivityManager:   ntv   N     2021: thermal-engine (pid 731) native
ActivityManager:   ntv   N     1998: audiod (pid 837) native
ActivityManager:   ntv   N     1569: dpmd (pid 846) native
ActivityManager:   ntv   N     1563: wpa_supplicant (pid 2470) native
ActivityManager:   ntv   N     1556: /init (pid 1) native
ActivityManager:   ntv   N     1554: imsdatadaemon (pid 878) native
ActivityManager:   ntv   N     1420: drmserver (pid 814) native
ActivityManager:   ntv   N     1392: netd (pid 821) native
ActivityManager:   ntv   N     1382: sdcard (pid 1943) native
ActivityManager:   ntv   N     1351: mediadrmserver (pid 818) native
ActivityManager:   ntv   N     1309: lowi-server (pid 1532) native
ActivityManager:   ntv   N     1222: mdtpd (pid 838) native
ActivityManager:   ntv   N     1150: ipacm (pid 871) native
ActivityManager:   ntv   N     1122: time_daemon (pid 1764) native
ActivityManager:   ntv   N     1084: cnss_diag (pid 724) native
ActivityManager:   ntv   N     1047: cnss-daemon (pid 26777) native
ActivityManager:   ntv   N     1038: qti (pid 894) native
ActivityManager:   ntv   N      978: wcnss_service (pid 733) native
ActivityManager:   ntv   N      958: adsprpcd (pid 738) native
ActivityManager:   ntv   N      956: commonfs (pid 4273) native
ActivityManager:   ntv   N      942: qcom-system-daemon (pid 717) native
ActivityManager:   ntv   N      941: nginx: worker process (pid 3973) native
ActivityManager:   ntv   N      939: sensors.qcom (pid 631) native
ActivityManager:   ntv   N      918: loc_launcher (pid 827) native
ActivityManager:   ntv   N      879: pm-service (pid 542) native
ActivityManager:   ntv   N      874: ATFWD-daemon (pid 829) native
ActivityManager:   ntv   N      866: keystore (pid 816) native
ActivityManager:   ntv   N      854: xtwifi-inet-agent (pid 1533) native
ActivityManager:   ntv   N      766: gatekeeperd (pid 839) native
ActivityManager:   ntv   N      758: perfd (pid 427) native
ActivityManager:   ntv   N      746: commonfs (pid 4041) native
ActivityManager:   ntv   N      738: installd (pid 815) native
ActivityManager:   ntv   N      736: lmkd (pid 544) native
ActivityManager:   ntv   N      726: qseecomd (pid 606) native
ActivityManager:   ntv   N      726: imsqmidaemon (pid 734) native
ActivityManager:   ntv   N      706: pm-proxy (pid 576) native
ActivityManager:   ntv   N      690: servicemanager (pid 545) native
ActivityManager:   ntv   N      679: esepmdaemon (pid 538) native
ActivityManager:   ntv   N      672: ueventd (pid 382) native
ActivityManager:   ntv   N      665: ptt_socket_app (pid 722) native
ActivityManager:   ntv   N      610: logcat (pid 570) native
ActivityManager:   ntv   N      610: ipacm-diag (pid 848) native
ActivityManager:   ntv   N      601: rmt_storage (pid 533) native
ActivityManager:   ntv   N      595: tftp_server (pid 534) native
ActivityManager:   ntv   N      592: dpmd (pid 825) native
ActivityManager:   ntv   N      585: logcat (pid 3742) native
ActivityManager:   ntv   N      567: msm_irqbalance (pid 898) native
ActivityManager:   ntv   N      513: sh (pid 428) native
ActivityManager:   ntv   N     7453: (Other native)
ActivityManager:   sys   P    56781: system (pid 1619) fixed
ActivityManager:   pers  P    31196: com.android.systemui (    3,068K memtrack) (pid 1889) fixed
ActivityManager:   pers  P    19700: com.android.phone (pid 2031) fixed
ActivityManager:   pers  P     9098: .dataservices (pid 2572) fixed
ActivityManager:   pers  P     7617: com.android.nfc (pid 2559) fixed
ActivityManager:   pers  P     6229: com.qualcomm.qti.services.secureui:sui_service (pid 2612) fixed
ActivityManager:   pers  P     6186: com.oma.drm.server (pid 2602) fixed
ActivityManager:   pers  P     5795: com.qualcomm.qti.loadcarrier (pid 2580) fixed
ActivityManager:            1199160: TOTAL (   25,104K memtrack)
ActivityManager:   MemInfo:   124,172K slab,    45,092K shmem,    20,048K vm alloc,    16,092K page tables    22,640K kernel stack
ActivityManager:                1,096K buffers,   115,140K cached,    45,388K mapped,     7,772K free
ActivityManager:   ZRAM:   320,124K RAM,   524,284K swap total,         0K swap free
ActivityManager:   Free RAM:    78,620K
ActivityManager:   Used RAM: 1,427,204K
ActivityManager:   Lost RAM:    29,608K
ActivityManager: Process com.xin.rx (pid 27772) has died

看到AMS有打印 Low on memory信息,看到了是哪个进程内存那么高了,接下来就要根据当时的场景分析,去找那个进程内存高的原因了

ps:一般不止一次报这个Low on memory,多往前搜几次,一般能找到哪个进程可能发生了内存泄漏,内存一直在增长

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345

推荐阅读更多精彩内容