当前位置:网站首页>一次内存泄漏的问题记录
一次内存泄漏的问题记录
2022-06-09 10:34:00 【lucky_tom】
项目场景:
同事在开发一款人脸识别app时,在多次执行,打开--》人脸识别--》关闭--》打开循环测试过程中会耗尽内存导致被kill。
问题描述:
测试同学通过用monkey脚本测试,遇到内存泄漏的问题。
日志片段1:可以看到系统一直在频繁gc,已经超过设定的512mb上限。
09-22 13:53:21.106 3052 3127 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:21.630 3052 5107 I zygote : Clamp target GC heap from 535MB to 512MB
09-22 13:53:21.630 3052 5107 I zygote : Alloc concurrent copying GC freed 38(1192B) AllocSpace objects, 0(0B) LOS objects, 0% free, 511MB/512MB, paused 167us total 1.441s
09-22 13:53:21.630 3052 3142 I zygote : WaitForGcToComplete blocked Alloc on HeapTrim for 2.013s
09-22 13:53:21.630 3052 5107 W zygote : Throwing OutOfMemoryError "Failed to allocate a 460812 byte allocation with 1192 free bytes and 1192B until OOM, max allowed footprint 536870912, growth limit 536870912"
09-22 13:53:21.630 3052 3142 I zygote : Starting a blocking GC Alloc
09-22 13:53:21.630 3052 3142 I zygote : Starting a blocking GC Alloc
09-22 13:53:21.630 3052 4168 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:21.630 3052 5107 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:21.630 3052 3806 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:21.630 3052 3127 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:21.630 3052 3061 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.103 3052 3142 I zygote : Clamp target GC heap from 535MB to 512MB
09-22 13:53:22.103 3052 3142 I zygote : Alloc concurrent copying GC freed 30(808B) AllocSpace objects, 0(0B) LOS objects, 0% free, 511MB/512MB, paused 145us total 472.588ms
09-22 13:53:22.103 3052 4168 I zygote : WaitForGcToComplete blocked Alloc on HeapTrim for 2.484s
09-22 13:53:22.103 3052 4168 I zygote : Starting a blocking GC Alloc
09-22 13:53:22.103 3052 4168 I zygote : Starting a blocking GC Alloc
09-22 13:53:22.103 3052 3142 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.103 3052 3127 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.103 3052 3061 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.103 3052 3806 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.103 3052 5107 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.557 3052 4168 I zygote : Clamp target GC heap from 535MB to 512MB
09-22 13:53:22.557 3052 4168 I zygote : Alloc concurrent copying GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 511MB/512MB, paused 131us total 453.636ms
09-22 13:53:22.557 3052 3127 I zygote : WaitForGcToComplete blocked Alloc on HeapTrim for 1.451s
09-22 13:53:22.557 3052 3127 I zygote : Starting a blocking GC Alloc
09-22 13:53:22.557 3052 3142 I zygote : WaitForGcToComplete blocked Alloc on HeapTrim for 454.347ms
09-22 13:53:22.558 3052 3142 I zygote : Starting a blocking GC Alloc
09-22 13:53:22.558 3052 3061 I zygote : WaitForGcToComplete blocked Alloc on HeapTrim for 2.941s
09-22 13:53:22.558 3052 3061 I zygote : Starting a blocking GC Alloc
09-22 13:53:22.558 3052 3061 I zygote : Starting a blocking GC Alloc
09-22 13:53:22.558 3052 3806 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.558 3052 5107 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.558 3052 4168 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.558 3052 3142 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:22.558 3052 3127 I zygote : Waiting for a blocking GC Alloc
09-22 13:53:23.032 3052 3061 I zygote : Clamp target GC heap from 535MB to 512MB
09-22 13:53:23.032 3052 3061 I zygote : Alloc concurrent copying GC freed 23(552B) AllocSpace objects, 0(0B) LOS objects, 0% free, 511MB/512MB, paused 120us total 474.598ms
09-22 13:53:23.033 3052 3806 I zygote : WaitForGcToComplete blocked Alloc on HeapTrim for 2.804s
09-22 13:53:23.033 3052 3806 I zygote : Starting a blocking GC Alloc
日志片段2:已经无法再分配新的内存空间
09-22 13:53:30.408 3052 3142 W System.err: java.lang.RuntimeException: Exception while computing database live data.
09-22 13:53:30.409 3052 3142 W System.err: at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
09-22 13:53:30.409 3052 3142 W System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
09-22 13:53:30.409 3052 3142 W System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
09-22 13:53:30.409 3052 3142 W System.err: at java.lang.Thread.run(Thread.java:764)
09-22 13:53:30.409 3052 3142 W System.err: Caused by: android.database.sqlite.SQLiteException: unknown error (code 0): Native could not create new byte[]
09-22 13:53:30.409 3052 3142 W System.err: at android.database.CursorWindow.nativeGetBlob(Native Method)
09-22 13:53:30.410 3052 3142 W System.err: at android.database.CursorWindow.getBlob(CursorWindow.java:403)
09-22 13:53:30.410 3052 3142 W System.err: at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45)
09-22 13:53:30.410 3052 3142 W System.err: at com.decard.facediscernoffline.module_base_offline.bean.db.FaceDataDao_Impl$10.call(FaceDataDao_Impl.java:1032)
09-22 13:53:30.410 3052 3142 W System.err: at com.decard.facediscernoffline.module_base_offline.bean.db.FaceDataDao_Impl$10.call(FaceDataDao_Impl.java:1016)
09-22 13:53:30.410 3052 3142 W System.err: at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:90)
09-22 13:53:30.410 3052 3142 W System.err: ... 3 more
原因分析:
1,查询此设备的DVM分配的heap上限
adb shell getprop | grep heap
[dalvik.vm.heapsize]: [512m]
2,重复复现步骤,抓取内存申请快照hprof文件
adb shell am dumpheap com.xxx.xxx /data/local/tmp/14_54.hprof
hprof-conv 14_54.hprof 14_54_R.hprof
hprof-conv在sdk的platform-tools目录中
3,使用Memory Analyzer Tool进行分析
打开14_54_R.hprof 文件后,

竟然有多个activity实例,基本断定是发生了上下文的强引用导致Activity无法被回收。
4,点击右键Paths to GC Roots-->exclude all phantom/weak/soft etc reference

发现引用的代码位置,进行排查,发现使用的都是activity的context。
解决方案:
重构代码,把需要引用上下文对象的类,都用ApplicationContext 来代替。
参考博客
边栏推荐
- Course design of network sniffer design based on C language
- 【Pyhton 实战】---- 批量【端午节】海报下载
- Clunky hero v0.96 Chinese version
- Thirty nine - SQL segment / group summary of data content
- 計網 | OSI模型中各層單比特
- [go] introduction to exp
- 福建土楼沟文化旅游开发有限公司6%股权转让,来自塔米狗分享
- Master tape editing tool Wavelab 11 Pro
- Leetcode 2048. 下一个更大的数值平衡数(有点意思,已解决)
- 精诚所至,金石为开
猜你喜欢

Course design of network sniffer design based on C language

More than observation | Alibaba cloud observable Technology Summit officially launched

Detailed steps for using Excel conditional format

字符串切割 group by

Tamidog knowledge - Interpretation of the new regulations on non-public agreement transfer of state-owned property rights in 2022!

Thirty one - the number of mongodb links in nodejs simple proxy pool (combined) exploded

MOFs, metal organic framework materials of folate ligands, are loaded with 5-fluorouracil, sidabelamine, taxol, doxorubicin, daunorubicin and other drugs

Amazing SQL query experience, group by slow query optimization!

三维数字沙盘展示具备哪些应用优势

每日一题-1232. 缀点成线
随机推荐
Quartz多个调度器+线程池模式分别调度任务
[email protected] -808 loaded 5-fluorouracil| [email protected]
4. 软件工程:航空行李托运费计算
QT char to realize dynamic waveform display
merge sort
微信小程序 登录失效后处理
每日一题-1200. 最小绝对差
Eight sorting methods (difficulty: heap sort merge sort quick sort)
多引擎数据库管理工具 DataGrip 2022.1.5中文版
6% equity transfer of Fujian tulougou Cultural Tourism Development Co., Ltd., shared by tamigou
数据资产管理:企业的数据资产怎么盘?
Dotnet core can also coordinate distributed transactions!
Thirty two - nodejs simple proxy pool (is it finished?) The SuperAgent uses a timeout that is not a timeout
DM platform management - NETCORE
Redis的使用
Use the five number generalization method to determine the outliers in the data set
SQL2008数据库
From "no one cares" to comprehensive competition, the first 10 years of domestic industrial control safety
太神奇的 SQL 查询经历,group by 慢查询优化!
环糊精金属有机骨架(β-CD-MOF)装载二巯丁二酸(β-CD-MOF/DMSA)β-环糊精金属有机骨架的载药机制