Volley Out of Memory Error, Weird Allocation Attempt

Volley out of memory error, weird allocation attempt

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);
...
}
}

Volley Out Of Memory issue V1.1.0

I found the solution for this seems like I am recreating Request queue in my HTTP module per every request. To avoid that I used following singleton class in my module

`public class RequestQueSingleton {
private static RequestQueSingleton sSoleInstance;
private static RequestQueue reQuestQue;

private RequestQueSingleton(){}  //private constructor.

public static RequestQueSingleton getInstance(Context context){
if (sSoleInstance == null){ //if there is no instance available... create new one
sSoleInstance = new RequestQueSingleton();
reQuestQue = Volley.newRequestQueue(context);
}

return sSoleInstance;
}

public synchronized RequestQueue getInstance() {
Log.d("Request Que Obj",reQuestQue.hashCode()+"");
return reQuestQue;
}}`

Why there is OutOfMemory exception while calling Volley.RequestQuery.add()?

I would suspect your main issue is the memory exception, and as a side effect you get the exception regarding the ProgressDialog while trying to Remove the view after you've already got the OutOfMemoryError exception (this is the "has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41dbc578 V.E..... R......D 0,0-684,192} that was originally added here" part).

As the main Exception suggests your cache is probably larger than the Heap and your responses from the server are quite large and you should focus on resovling this issue.

I think this question might help you resolve this issue.

OutofMemory error on background service using Volley

Your service create new request queue each time when make http request best approach is to use singleton object of request queue and reuse it for each request.

here is code example
//Create new Java file App.java

public class App extends Application {
public static final String TAG = App.class.getSimpleName();
private static App mInstance;

private RequestQueue mRequestQueue;

public static synchronized App getInstance() {
return mInstance;
}

@Override
public void onCreate() {

super.onCreate();

mRequestQueue = Volley.newRequestQueue(getApplicationContext());

mInstance = this;

}

public RequestQueue getRequestQueue() {
return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> request, String tag) {
request.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(request);
}

public <T> void addToRequestQueue(Request<T> request) {
request.setTag(TAG);
getRequestQueue().add(request);
}

public void cancelPendingRequest(Object tag) {
getRequestQueue().cancelAll(tag);
}

}

// in android manifest reference name of that App class

 <application
android:name=".App"
....

//now when ever you make request do that

App.getInstance().addToRequestQueue(stringRequest);

this will create only one instance of request queue and use it for every request

Android Volley give me an outOfMemory exception

i did fix this to provide proper cache to the BitmapLruCache insted of LruCache

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;

return cacheSize;
}

public BitmapLruCache() {
this(getDefaultLruCacheSize());
}

public BitmapLruCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}

}

here is the link:
Android Volley ImageLoader - BitmapLruCache parameter?

java.lang.outofmemoryerror exception?

-If the problem is due to Bitmaps then use

Lazy Imageloader for Bitmaps in listviews

-Make sure you dnt have too much work in its background task.

-And if possible please post your code from where you are getting this issue so we can also learn from it . THanks! :)



Related Topics



Leave a reply



Submit