Search code examples
javaandroidandroid-6.0-marshmallow

Android java.lang.OutOfMemoryError on android 6.0


So i have this application on play store, which helps people clean their phone and i have gotten few errors messages that when people click "clean" button the app crashes and because of outofmemoryerror. Here is the error message:

java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:309)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 147468 byte allocation with 24512 free bytes and 23KB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:4215)
    at android.content.res.Resources.loadDrawable(Resources.java:4089)
    at android.content.res.Resources.getDrawable(Resources.java:2005)
    at android.content.res.Resources.getDrawable(Resources.java:1987)
    at android.app.ApplicationPackageManager.getDrawable(ApplicationPackageManager.java:977)
    at android.app.ApplicationPackageManager.loadUnbadgedItemIcon(ApplicationPackageManager.java:2842)
    at android.app.ApplicationPackageManager.loadItemIcon(ApplicationPackageManager.java:2774)
    at android.content.pm.PackageItemInfo.loadIcon(PackageItemInfo.java:220)
    at android.content.pm.PackageItemInfo.loadIcon(PackageItemInfo.java:206)
    at panda.cleaner.cleaner_batterysaver.fragment.SoftwareManageFragment$1.doInBackground(SoftwareManageFragment.java:139)
    at panda.cleaner.cleaner_batterysaver.fragment.SoftwareManageFragment$1.doInBackground(SoftwareManageFragment.java:127)
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

Here is the class where the error is coming from:

task = new AsyncTask<Void, Integer, List<AppInfo>>() {
            private int mAppCount = 0;//(SoftwareManageFragment.java:127)

@Override
            protected List<AppInfo> doInBackground(Void... params) {
                PackageManager pm = mContext.getPackageManager();
                List<PackageInfo> packInfos = pm.getInstalledPackages(0);
                publishProgress(0, packInfos.size());
                List<AppInfo> appinfos = new ArrayList<AppInfo>();
                for (PackageInfo packInfo : packInfos) {
                    publishProgress(++mAppCount, packInfos.size());
                    final AppInfo appInfo = new AppInfo();
                    Drawable appIcon = packInfo.applicationInfo.loadIcon(pm);//(SoftwareManageFragment.java:139)
                    appInfo.setAppIcon(appIcon);

                    int flags = packInfo.applicationInfo.flags;

                    int uid = packInfo.applicationInfo.uid;

                    appInfo.setUid(uid);

                    if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                        appInfo.setUserApp(false);//系统应用
                    } else {
                        appInfo.setUserApp(true);//用户应用
                    }
                    if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
                        appInfo.setInRom(false);
                    } else {
                        appInfo.setInRom(true);
                    }
                    String appName = packInfo.applicationInfo.loadLabel(pm).toString();
                    appInfo.setAppName(appName);
                    String packname = packInfo.packageName;
                    appInfo.setPackname(packname);
                    String version = packInfo.versionName;
                    appInfo.setVersion(version);
                    try {
                        mGetPackageSizeInfoMethod.invoke(mContext.getPackageManager(), new Object[]{
                                packname,
                                new IPackageStatsObserver.Stub() {
                                    @Override
                                    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
                                        synchronized (appInfo) {
                                            appInfo.setPkgSize(pStats.cacheSize + pStats.codeSize + pStats.dataSize);

                                        }
                                    }
                                }
                        });
                    } catch (Exception e) {
                    }

                    appinfos.add(appInfo);
                }
                return appinfos;
            }

Since i am just a junior developer, i am not sure what the problem might be. Particular phone that caught this error was using Android 6.0.Help will be much appriciated. Thanks alot in advance.


Solution

  • This is not the main problem i suppose. You should checkout the memory monitor for out of memory exception. First open the activity which occurs the error then press back button and check if there is any leaking.
    There are few things you can do about this exception.
    In android manifest, you should append largeheap = true under application tag.
    You can load bitmap or drawable files with configuration and make their space lower