问题
In my Android app I need to download metadata for some files from a XML API. I am using RetroFit to handle the requests that I've implemented as asynchronous ones, in the Callback I parse the XML and save in a file the data I need.
When I need to get info for many files, I get OutOfMemoryError and analyzing the heap with MAT, I discovered that XML body of previous responses is still there. Is there any way to force the app to flush the already used XML?
回答1:
I think I found a solution:
By default, the OkHttpClient uses a memory cache implementation for HttpResponseCache, switching to a disk implementation I was able to run 10000 requests without getting any OutOfMemory error.
I have looked at this example and edited the following lines to use Android internal storage:
File cacheDir = new File(System.getProperty("java.io.tmpdir"), "okhttp-cache");
cache = new HttpResponseCache(cacheDir, 10L * 1024 * 1024);
okHttpClient = new OkHttpClient();
okHttpClient.setResponseCache(cache);
来源:https://stackoverflow.com/questions/24930700/flush-previous-retrofit-responses