洞察力赞赏 解决方案在streamToBytes()中,首先会根据缓存文件的长度来新建字节,是不是你的缓存文件比应用程序大最大堆大小?private static byte[] streamToBytes(InputStream in, int length) 抛出 IOException {字节[]字节=新字节[长度];...}公共同步条目 get(String key) {CacheHeader entry = mEntries.get(key);文件文件 = getFileForKey(key);byte[] data = streamToBytes(..., file.length());}如果你想清除缓存,你可以保留 DiskBasedCache 引用,在清除时间到来后,使用 ClearCacheRequest 并将该缓存实例传入:File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);DiskBasedCache 缓存 = 新的 DiskBasedCache(cacheDir);RequestQueue queue = new RequestQueue(cache, network);队列开始();//清除所有截击缓存.queue.add(new ClearCacheRequest(cache, null));这种方式会清空所有缓存,建议大家谨慎使用.当然,你可以做条件检查,只需要迭代cacheDir文件,估计过大然后删除.for (File cacheFile : cacheDir.listFiles()) {如果 (cacheFile.isFile() && cacheFile.length() > 10000000) cacheFile.delete();}Volley 不是作为大数据缓存解决方案设计的,它是普通的请求缓存,不要随时存储大数据.------------- 2014-07-17 更新 -------------其实,清除所有缓存是最终的方式,也不是明智的方式,我们应该在确定会的时候抑制这些大的请求使用缓存,如果不确定呢?我们仍然可以确定响应数据的大小是否大,然后调用setShouldCache(false)禁用它.public class TheRequest extends Request {@覆盖受保护的响应解析网络响应(网络响应响应){//如果响应数据太大,禁用缓存仍然是时间.如果 (response.data.length > 10000) setShouldCache(false);...}}Sometimes randomly Volley crashes my app upon startup, it crashes in the application class and a user would not be able to open the app again until they go into settings and clear app datajava.lang.OutOfMemoryErrorat com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:316)at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:526)at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:549)at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:392)at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:155)at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84)The "diskbasedbache" tries to allocate over 1 gigabyte of memory, for no obvious reasonhow would I make this not happen? It seems to be an issue with Volley, or maybe an issue with a custom disk based cache but I don't immediately see (from the stack trace) how to 'clear' this cache or do a conditional check or handle this exceptionInsight appreciated 解决方案 In the streamToBytes(), first it will new bytes by the cache file length, does your cache file was too large than application maximum heap size ?private static byte[] streamToBytes(InputStream in, int length) throws IOException { byte[] bytes = new byte[length]; ...}public synchronized Entry get(String key) { CacheHeader entry = mEntries.get(key); File file = getFileForKey(key); byte[] data = streamToBytes(..., file.length());}If you want to clear the cache, you could keep the DiskBasedCache reference, after clear time's came, use ClearCacheRequest and pass that cache instance in :File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);DiskBasedCache cache = new DiskBasedCache(cacheDir);RequestQueue queue = new RequestQueue(cache, network);queue.start();// clear all volley caches.queue.add(new ClearCacheRequest(cache, null));this way will clear all caches, so I suggest you use it carefully. of course, you can doing conditional check, just iterating the cacheDir files, estimate which was too large then remove it. for (File cacheFile : cacheDir.listFiles()) { if (cacheFile.isFile() && cacheFile.length() > 10000000) cacheFile.delete();}Volley wasn't design as a big data cache solution, it's common request cache, don't storing large data anytime.------------- Update at 2014-07-17 -------------In fact, clear all caches is final way, also isn't wise way, we should suppressing these large request use cache when we sure it would be, and if not sure? we still can determine the response data size whether large or not, then call setShouldCache(false) to disable it.public class TheRequest extends Request { @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { // if response data was too large, disable caching is still time. if (response.data.length > 10000) setShouldCache(false); ... }} 这篇关于排球内存不足错误,奇怪的分配尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 05:01