I've been trying to trace down what I initially thought were memory leaks, only to find that the memory does get freed eventually but very slowly by the emulator's GC.
Basically I've started the app, gone to the root node (Select mode), started the activity and hit the back key. I repeated this several times until the heap was nearly full. I know the activity's onDestroy() was called each time I exited.
These are the figures I collected
Before garbage collection happens:
native dalvik other total
size: 14256 6407 N/A 20663
allocated: 14083 5184 N/A 19267
free: 124 1223 N/A 1347
(Pss): 3993 8315 9819 22127
(shared dirty): 1968 4584 1448 8000
(priv dirty): 3792 4216 8176 16184
Objects
Views: 210 ViewRoots: 2
AppContexts: 13 Activities: 12
Assets: 2 AssetManagers: 2
Local Binders: 22 Proxy Binders: 25
Death Recipients: 2
OpenSSL Sockets: 0
After garbage collection
native dalvik other total
size: 14256 6407 N/A 20663
allocated: 6107 3894 N/A 10001
free: 1776 2513 N/A 4289
(Pss): 2513 8227 3499 14239
(shared dirty): 1968 4584 1448 8000
(priv dirty): 2312 4128 1856 8296
Objects
Views: 48 ViewRoots: 2
AppContexts: 4 Activities: 3
Assets: 2 AssetManagers: 2
Local Binders: 22 Proxy Binders: 16
Death Recipients: 2
OpenSSL Sockets: 0
Garbage collection timings ( I finshed switching activities at 16:13)
02-27 16:13:51.022: DEBUG/INF_TAG(13723): Select Mode onStop
02-27 16:14:07.863: DEBUG/dalvikvm(13723): GC freed 31080 objects / 1978968 bytes in 52ms
02-27 16:14:48.562: DEBUG/dalvikvm(13723): GC freed 45884 objects / 2084224 bytes in 251ms
02-27 16:15:30.802: DEBUG/dalvikvm(13723): GC free开发者_JAVA技巧d 46405 objects / 2157376 bytes in 265ms
02-27 16:16:12.863: DEBUG/dalvikvm(13723): GC freed 45091 objects / 2085880 bytes in 391ms
02-27 16:17:00.992: DEBUG/dalvikvm(13723): GC freed 45346 objects / 2072712 bytes in 251ms
02-27 16:17:44.542: DEBUG/dalvikvm(13723): GC freed 45177 objects / 2101152 bytes in 187ms
02-27 16:18:29.182: DEBUG/dalvikvm(13723): GC freed 45603 objects / 2106496 bytes in 242ms
02-27 16:19:19.823: DEBUG/dalvikvm(13723): GC freed 58797 objects / 2723784 bytes in 266ms
02-27 16:19:40.122: DEBUG/dalvikvm(13723): GC freed 22403 objects / 1058168 bytes in 216ms
02-27 16:20:06.223: DEBUG/dalvikvm(13723): GC freed 21685 objects / 993504 bytes in 211ms
02-27 16:20:29.173: DEBUG/dalvikvm(13723): GC freed 20948 objects / 980304 bytes in 223ms
02-27 16:20:52.272: DEBUG/dalvikvm(13723): GC freed 21149 objects / 968136 bytes in 207ms
02-27 16:21:16.332: DEBUG/dalvikvm(13723): GC freed 21349 objects / 972216 bytes in 207ms
02-27 16:21:40.233: DEBUG/dalvikvm(13723): GC freed 22008 objects / 984904 bytes in 110ms
02-27 16:22:03.313: DEBUG/dalvikvm(13723): GC freed 21115 objects / 948144 bytes in 215ms
02-27 16:22:27.362: DEBUG/dalvikvm(13723): GC freed 21646 objects / 970288 bytes in 222ms
02-27 16:22:50.463: DEBUG/dalvikvm(13723): GC freed 21834 objects / 981016 bytes in 218ms
02-27 16:23:15.133: DEBUG/dalvikvm(13723): GC freed 21821 objects / 965472 bytes in 211ms
02-27 16:23:40.662: DEBUG/dalvikvm(13723): GC freed 23011 objects / 990424 bytes in 122ms
02-27 16:24:47.162: DEBUG/dalvikvm(13723): GC freed 28716 objects / 874880 bytes in 124ms
02-27 16:25:55.603: DEBUG/dalvikvm(13723): GC freed 30100 objects / 943888 bytes in 135ms
I'm quite prepared to accept that I may be leaking some small objects, but I just can't see why it takes the GC so long to free up the heap and remove the activities from the stack. Is this typical behaviour on a real phone?
P.S. I tried calling System.gc() in the root node's onResume() but it makes no difference
The OS appears to be very conservative about running the GC, because whenever it runs it interferes with the user experience. Since phones (and emulators) have lots of memory, the GC tends to run infrequently and incrementally. It's only when memory starts to get low that the OS becomes more aggressive about reclaiming resources.
精彩评论