在我的应用开始之前,我观察到Android的奇怪行为。似乎在我的应用开始做某事之前就分配了大量内存。 GC告诉我,只有90%的堆是免费的。此时应用程序即使在" onCreate"之前启动我的第一个活动被称为JVM只剩下4%的可用内存。这里发生了什么,以及如何防止这种情况发生,因为只剩下4%的64MB堆,我的应用程序无法运行。
03-09 21:00:16.517 1684-2249/? D/k: Processing package: de.ponysoft.backhome
03-09 21:00:16.549 1684-1684/? D/ChimeraCfgMgr: Loading module com.google.android.gms.vision from APK com.google.android.gms
03-09 21:00:16.587 1524-1524/? E/NetworkScheduler.SchedulerReceiver: Invalid parameter app
03-09 21:00:16.587 1524-1524/? E/NetworkScheduler.SchedulerReceiver: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
03-09 21:00:16.684 1684-2252/? I/PeopleContactsSync: triggerPendingContactsCleanup: no accounts
03-09 21:00:16.699 1684-2249/? D/GassUtils: Found app info for package de.ponysoft.backhome:3. Hash: 8514543eb70ddc4389765ad8baac5ba8cab5be6e67d756a46e474b3d85051122
03-09 21:00:16.700 1684-2249/? D/k: Found info for package de.ponysoft.backhome in db.
03-09 21:00:17.375 346-366/? W/VoiceInteractionManagerService: no available voice recognition services found for user 0
03-09 21:00:17.850 346-377/? I/art: Explicit concurrent mark sweep GC freed 12467(807KB) AllocSpace objects, 5(81KB) LOS objects, 26% free, 11MB/15MB, paused 13.482ms total 1.768s
03-09 21:00:17.925 1684-1795/? I/Icing: Indexing 6F97C46050CD983C7FFCEB53F6194D5AB9C51AB5 from com.google.android.gms
03-09 21:00:17.972 2224-2224/? D/AndroidRuntime: Shutting down VM
03-09 21:00:18.184 1684-1795/? I/Icing: Indexing done 6F97C46050CD983C7FFCEB53F6194D5AB9C51AB5
03-09 21:00:19.720 2260-2260/? D/AndroidRuntime: CheckJNI is ON
03-09 21:00:20.299 2260-2260/? E/memtrack: Couldn't load memtrack module (No such file or directory)
03-09 21:00:20.302 2260-2260/? E/android.os.Debug: failed to load memtrack module: -2
03-09 21:00:20.553 2260-2260/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
03-09 21:00:20.608 346-1270/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=de.ponysoft.backhome/.InitActivity} from uid 0 on display 0
03-09 21:00:20.780 2260-2260/? D/AndroidRuntime: Shutting down VM
03-09 21:00:20.968 2270-2270/? I/art: Not late-enabling -Xcheck:jni (already on)
03-09 21:00:21.005 346-930/? I/ActivityManager: Start proc de.ponysoft.backhome for activity de.ponysoft.backhome/.InitActivity: pid=2270 uid=10057 gids={50057, 9997, 3003, 1028, 1015} abi=armeabi-v7a
03-09 21:00:21.200 69-69/? I/art: Explicit concurrent mark sweep GC freed 706(30KB) AllocSpace objects, 0(0B) LOS objects, 90% free, 110KB/1134KB, paused 21.161ms total 146.487ms
03-09 21:00:21.261 346-367/? I/ActivityManager: Killing 1127:com.android.music/u0a36 (adj 15): empty for 1801s
03-09 21:00:21.336 346-411/? W/libprocessgroup: failed to open /acct/uid_10036/pid_1127/cgroup.procs: No such file or directory
03-09 21:00:21.388 69-69/? I/art: Explicit concurrent mark sweep GC freed 7(240B) AllocSpace objects, 0(0B) LOS objects, 90% free, 110KB/1134KB, paused 1.021ms total 179.966ms
03-09 21:00:21.491 69-69/? I/art: Explicit concurrent mark sweep GC freed 5(160B) AllocSpace objects, 0(0B) LOS objects, 90% free, 110KB/1134KB, paused 872us total 96.457ms
03-09 21:00:22.586 2270-2286/? I/GMPM: App measurement is starting up
03-09 21:00:22.662 2270-2270/? D/InitActivity: onCreate
03-09 21:00:22.817 2270-2270/? D/InitActivity: onStart
此高内存消耗还会导致我的应用程序因OOM异常而崩溃
03-09 21:00:31.543 2270-2287/? I/art: Clamp target GC heap from 65MB to 64MB
03-09 21:00:31.543 2270-2287/? I/art: Alloc partial concurrent mark sweep GC freed 28(1312B) AllocSpace objects, 0(0B) LOS objects, 4% free, 61MB/64MB, paused 1.088ms total 25.705ms
03-09 21:00:31.613 2270-2287/? I/art: Clamp target GC heap from 65MB to 64MB
03-09 21:00:31.613 2270-2287/? I/art: Alloc concurrent mark sweep GC freed 8(12KB) AllocSpace objects, 0(0B) LOS objects, 4% free, 61MB/64MB, paused 2.256ms total 67.540ms
03-09 21:00:31.613 2270-2287/? I/art: Forcing collection of SoftReferences for 8MB allocation
03-09 21:00:31.676 2270-2287/? I/art: Clamp target GC heap from 65MB to 64MB
03-09 21:00:31.676 2270-2287/? I/art: Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 4% free, 61MB/64MB, paused 1.077ms total 61.122ms
03-09 21:00:31.677 2270-2287/? E/art: Throwing OutOfMemoryError "Failed to allocate a 9060076 byte allocation with 2786284 free bytes and 2MB until OOM"
03-09 21:00:31.678 2270-2287/? D/skia: --- allocation failed for scaled bitmap
03-09 21:00:31.696 2270-2287/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: de.ponysoft.backhome, PID: 2270
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 9060076 byte allocation with 2786284 free bytes and 2MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:467)
at de.ponysoft.backhome.async.AsyncLoadImages.decodeSampledBitmapFromResource(AsyncLoadImages.java:186)
at de.ponysoft.backhome.async.AsyncLoadImages.doInBackground(AsyncLoadImages.java:91)
at de.ponysoft.backhome.async.AsyncLoadImages.doInBackground(AsyncLoadImages.java:27)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
03-09 21:00:31.702 346-1507/? W/ActivityManager: Force finishing activity de.ponysoft.backhome/.InitActivity
03-09 21:00:31.769 55-55/? E/EGL_emulation: tid 55: eglCreateSyncKHR(1237): error 0x3004 (EGL_BAD_ATTRIBUTE)
我在使用API 21的模拟器中运行了这个。但是我也在三星Galaxy S2这样的旧手机上观察到了这种行为。我的应用程序的发布版本总共为3.7MB。所有资产均为全高清。在较新的手机上,只有旧款手机没有内存问题。