执行AsyncTask时发出内存错误

时间:2014-02-14 18:12:02

标签: android performance android-asynctask

我正在尝试运行android.its的现有项目,给出错误跟踪

02-14 22:49:14.812: E/AndroidRuntime(665): FATAL EXCEPTION: AsyncTask #3
02-14 22:49:14.812: E/AndroidRuntime(665): java.lang.RuntimeException: An error occured while executing doInBackground()
02-14 22:49:14.812: E/AndroidRuntime(665):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.lang.Thread.run(Thread.java:856)
02-14 22:49:14.812: E/AndroidRuntime(665): Caused by: java.lang.OutOfMemoryError
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.io.BufferedWriter.<init>(BufferedWriter.java:71)
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.io.BufferedWriter.<init>(BufferedWriter.java:54)
02-14 22:49:14.812: E/AndroidRuntime(665):  at com.jakewharton.disklrucache.DiskLruCache.open(DiskLruCache.java:226)
02-14 22:49:14.812: E/AndroidRuntime(665):  at com.hercules.quantum.QuantumActivity$InitThumbnailDiskCacheTask.doInBackground(QuantumActivity.java:5575)
02-14 22:49:14.812: E/AndroidRuntime(665):  at com.hercules.quantum.QuantumActivity$InitThumbnailDiskCacheTask.doInBackground(QuantumActivity.java:1)
02-14 22:49:14.812: E/AndroidRuntime(665):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-14 22:49:14.812: E/AndroidRuntime(665):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-14 22:49:14.812: E/AndroidRuntime(665):  ... 4 more

与其显示此错误的源代码

class InitThumbnailDiskCacheTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            synchronized (thumbnailLruCacheLock) {
                // Check if media is mounted or storage is built-in, if so, try and use external cache dir
                // otherwise use internal cache dir
                final String cachePath =
                        Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                                !Environment.isExternalStorageRemovable() ? getExternalCacheDir().getPath() :
                                        getCacheDir().getPath();
                                Log.i("Cache Path","Cache Path"+cachePath);

                try {
                    if (!isCancelled()) {
                        thumbnailLruCache = DiskLruCache.open(
                                new File(cachePath + File.separator + "thumbnails"),
                                APP_VERSION,
                                VALUE_COUNT,
                                DISK_CACHE_SIZE);
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Failed to initialize thumbnail cahce", e);
                }
                thumbnailLruCacheStarting = false; // Finished initialization
                thumbnailLruCacheLock.notifyAll(); // Wake any waiting threads
            }
            return null;
        }
    }

任何人都可以指导如何解决它。

1 个答案:

答案 0 :(得分:0)

你分配的内存过多。在Android中,流程的内存数量有限(请参阅this question),您的流程内存总数不能超过该数量,否则您将获得OutOfMemoryError。考虑按几个步骤拆分加载,以便GC可以释放未使用的内存。