9

I'm using a viewpager to load around 50 webviews... All the webviews are loaded into assests and each weview has an HTML page that is accessing around 70 images each... As i swipe, my app is crashing after around 30 pages, might be cos of the webviews still keep their reference to the images in the assests folder... Is there any way to release the webviews that the Viewpager is not using at that particular time?

awesomePager.setAdapter(new AwesomePagerAdapter(this, webviewdata));

Details:

Android WebView Memory Leak when loading html file from Assets  
Failed adding to JNI local ref table (has 512 entries)
"Thread-375" prio=5 tid=15 RUNNABLE

while dynamically loading webview on viewpager

Logcat: enter image description here

3 Answers 3

1

Try to scale down bitmap.Most of the time Bitmap is a main reason we get Memory issue. Also learn about how to recycle the bitmaps. Following snippet will help you.

BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile( filename, options );
        options.inJustDecodeBounds = false;
        options.inSampleSize = 2; 

        bitmap = BitmapFactory.decodeFile( filename, options );
        if ( bitmap != null && exact ) {
            bitmap = Bitmap.createScaledBitmap( bitmap, width, height, false );
        }

Also make sure you did override following method.

@Override
public void destroyItem(View collection, int position, Object view) {
    ((ViewPager) collection).removeView((TextView) view);
}

Or you can create a function to Scale Down Bitmap

private byte[] resizeImage( byte[] input ) {

    if ( input == null ) {
        return null;
    }

    Bitmap bitmapOrg = BitmapFactory.decodeByteArray(input, 0, input.length);

    if ( bitmapOrg == null ) {
        return null;
    }

    int height = bitmapOrg.getHeight();
    int width = bitmapOrg.getWidth();
    int newHeight = 250;

    float scaleHeight = ((float) newHeight) / height;

    // creates matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleHeight, scaleHeight);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
            width, height, matrix, true);

    bitmapOrg.recycle();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    resizedBitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);            

    resizedBitmap.recycle();

    return bos.toByteArray();            
}       
1
  • 1
    @Vipul Yes i have overridden destroyItem, still no luck.
    – Kiran
    Commented Jun 14, 2012 at 12:43
0

The WebView works with the JNI and this can only hold 512 local references, try loading your content directly from web and the problem shouldn't occur.

I get this problem when I override shouldInterceptRequest(WebView view, String url) in webviewclient and hand local references from my own caching mechanism.

This might be a bug in the webview itself. At least it isn't how it should behave if you ask me.

0

Setting layer type of webview to software works.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

But now you lose hardware acceleration and your webview may slow down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.