一. Android内存泄漏都有哪些情况?
从软件分层来讲有三种,java层、jni层和native层.
二. java层泄漏都有哪些情况
1. 往list、map、queue这种类型的变量中无限制的加item,算不上泄漏,但会造成内存溢出,数据量大建议存在磁盘.
2. 内部类持有外部类,造成内部类对象使用完成无法回收.
a. 单例持有activity context
b. system service使用activity context
3. 类似Bitmap这种需要大内存的对象谨慎使用,瞬时对象过多也会内存溢出.
三. jni层内存泄漏
1. 使用了GetStringUTFChars等却没有ReleaseStringUTFChars会导致native heap增长.
2.
四. native层泄漏
C、C++代码malloc或者new了内存,没有free或delete,导致内存泄漏.
native 内存泄漏
# adb shell dumpsys meminfo “packagename” -d
循环读取
# while : ;do adb shell dumpsys meminfo “packagename” -d | grep "Native Heap"; sleep 1; done;
0000095573:~ xiaxing$ adb shell dumpsys meminfo “packagename” -d
Applications Memory Usage (kB):
Uptime: 9151889 Realtime: 9151889
** MEMINFO in pid 10657 [packagename] **
Pss Private Private Swapped Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 35784 35636 0 0 37712 36237 1474
Dalvik Heap 34018 33784 0 0 33932 31281 2651
Dalvik Other 1061 1056 0 0
Stack 36 36 0 0
Ashmem 2 0 0 0
Other dev 7204 0 7204 0
.so mmap 8061 332 2656 0
.apk mmap 800 0 0 0
.ttf mmap 66 0 0 0
.dex mmap 8704 8592 80 0
.oat mmap 7725 0 1232 0
.art mmap 1951 1116 0 0
Other mmap 1507 8 368 0
GL mtrack 17896 17896 0 0
Unknown 2116 2112 0 0
TOTAL 126931 100568 11540 0 71644 67518 4125
App Summary
Pss(KB)
------
Java Heap: 34900
Native Heap: 35636
Code: 12892
Stack: 36
Graphics: 17896
Private Other: 10748
System: 14823
TOTAL: 126931 TOTAL SWAP (KB): 0
Objects
Views: 42 ViewRootImpl: 1
AppContexts: 3 Activities: 1
Assets: 6 AssetManagers: 2
Local Binders: 29 Proxy Binders: 21
Parcel memory: 8 Parcel count: 34
Death Recipients: 0 OpenSSL Sockets: 0
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 0
Asset Allocations
zip:/data/app/com.baidu.aip.aigate-1/base.apk:/assets/facedetect.binary: 58K
Applications Memory Usage (kB):
Uptime: 321262859 Realtime: 321262859
** MEMINFO in pid 19708 [com.baidu.aip.aigate] **
Pss Private Private Swapped Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 995838 995672 8 520 996484 912881 83602
Dalvik Heap 35015 34196 544 0 30982 29442 1540
Dalvik Other 669 652 8 0
Stack 36 36 0 0
Ashmem 2 0 0 0
Other dev 7204 0 7204 0
.so mmap 8043 320 2184 2016
.apk mmap 1374 0 60 0
.ttf mmap 267 0 232 0
.dex mmap 5912 4 2244 0
.oat mmap 11613 0 6080 4
.art mmap 3943 864 1752 0
Other mmap 1957 8 896 0
GL mtrack 17486 17486 0 0
Unknown 1888 1888 0 80
TOTAL 1091247 1051126 21212 2620 1027466 942323 85142
App Summary
Pss(KB)
------
Java Heap: 36812
Native Heap: 995672
Code: 11124
Stack: 36
Graphics: 17486
Private Other: 11208
System: 18909
TOTAL: 1091247 TOTAL SWAP (KB): 2620
Objects
Views: 65 ViewRootImpl: 2
AppContexts: 4 Activities: 2
Assets: 6 AssetManagers: 2
Local Binders: 14 Proxy Binders: 16
Parcel memory: 5 Parcel count: 22
Death Recipients: 0 OpenSSL Sockets: 0
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 0
Asset Allocations
zip:/data/app/com.baidu.aip.aigate-1/base.apk:/assets/facedetect.binary: 58K