I am using android webview in my app for browsing. All the pages load fine. no issues. But as I keep browsing (opening links) the memory keeps increasing. For example: 3 pages navigated its 60 mb and after it reaches a point the app crashes.
Now, these are regular websites like cnn.com, gmail.com, gap.com etc
Any ideas or pointers?
Thanks!
You should use caching to avoid such memory leaks!
loaded images can be cached on disk and loaded incrementally.
Here is a generic caching library together with example usage >>
Related
I have been trying to enable caching in Android WebView but every config I saw on SO ends in failure.
Here is what I have:
I have a webpage which has an Iframe inside that loads 10-12 pages in a loop all the content in those pages have Cache-Control: max-age=86400 header.
If I load these pages normally in a browser they work and the pages are cached.
So here is the config that I have
getSettings().setJavaScriptEnabled(true);
getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
getSettings().setDomStorageEnabled(true);
getSettings().setMediaPlaybackRequiresUserGesture(false);
getSettings().setAppCacheEnabled(true);
getSettings().setAppCachePath(context.getCacheDir().getAbsolutePath());
getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
These settings work for media playback and enabling js
EDIT
I built a new app and tried loading google with the loadUrl method and reloaded that with a button while the net was off, it was able to reload the page without internet it means it cached that page.
So the issue is that webview is not caching pages which are being loaded by the page.
If anyone knows how we can cache the content which is loaded by webview page loads it would be a great help.
-Thanks
So after a series of detailed tests on our iframe and Android device, I came to realize that WebView does not cache files which are bigger than a particular size.
One of my pages which were being loaded by the iframe had a video of 30mb which was not getting cached and was resulting in huge data consumption. (as it was getting rendered every 2 mins)
I reduced it to 10mb and it still did not cache it finally I reduced it to 980kb and it started caching.
I am not sure about the caching limit per file for WebView but I came across an interesting bug here which states after Android 4.3 the total cache limit was hardcoded to 20mb which we can find in this file on line 98.
If anyone knows where these limits are documented do let the community know by replying here.
This code solve my problem, hope help you too:
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
EDIT: Also you don't needs to these lines:
getSettings().setAppCacheEnabled(true);
getSettings().setAppCachePath(context.getCacheDir().getAbsolutePath());
Remove them and check one more time. AppCache setting not related to type of cache that you want.
I have this aplication thats keeps building cache every time I open the app
In my app I load like 20 images from my website, but I don't save them to cache,
does the app automatically save this images to cache? If so, I don't think they load from cache, cuz I still have to have internet to load them
Admob in the other hand, if I don't have internet and I loaded the test ad before, it will load the testad.
currently I have the app to delete the cache everytime it starts, keeping cache low, but this doesn't seems smart.
My questions are:
Does admob uses cache of my app?
Knowing my app loads 20 images from my website, this images being
loaded affects my cache size even if I don't save them
programmatically to cache ?
is deleting cache expensive enough to have a dedicated thread to
do just that?
Does admob uses cache of my app?
Yes i think Admob caches its ads so they load faster. And Admob should be managing its own cache size. I don't think you need to worry about that.
Knowing my app loads 20 images from my website, this images being loaded affects my cache size even if I don't save them
programmatically to cache ?
No images or bitmaps will not be cached unless you do so, see this link for more info. I will recommend that you use the Glide or Picasso for image loading since they are memory efficient and abstract out most of the complexity.
Is deleting cache expensive enough to have a dedicated thread to do just that?
I don't think you should use dedicated thread. What you can do is override onTrimMemory and keep monitoring the memory level.When your memory level is critical or low you can delete the cache. Check this link for more info. In my opinion if you use Glide for loading your images it will already handle the memory very well and prevents OutOfMemoryError. Also, loads image much faster.
Dear Folks,
I have idea regarding the general Memory Structure and how the OS manages the memory by using different mechanism!
I know that android uses linux kernel and there would be memory management module which would control access the way the memory is allocated to a process.
I recently had my doubt arising when i ran through the Out of memory exception in android.
Consider I have 10 images to be displayed in an activity and when my application opens the said activity all the 10 images would be loaded in memory and will be in the primary memory until my app is pushed to the background. This is how I understood so far regarding the processes in any operation system.
But I also ran into the doubt once the images are loaded in the memory and dispatched to the GUI I mean set to the activity , will not those images be present in primary memory or they will be cleared out from the memory ?
Please help me in understanding this!
I already Google -ed it, but not able to find what I needed
Thanks in Advance
This is completely wrong. You should not load 10 full sized images at all. You usually load them way smaller, directly from the disk into Thumbnails that are not a few megabytes each. You should read this article: https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
I have a listview and each listview item when clicked opens a gallery (conating 30 + images ) to be downloaded (using the urls).I don't want to re - download the images on next visit and also want the images to be present when i click on list in offline mode.
I went through several links in SO and am a bit confused on which approach to follow:
1> Use bytearray and store the images in sqlite db.
2> use context -> getExternalCacheDir() to store it on the
external memory (this wont work on devices with no external
memory.)
3> Use SoftReferences as suggested in following link
http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html
Please help to guide me on the best approach to handle around 1000 + images which works in offline mode as well.
1000 images.....I sure hope they're thumbnails or you're going to end using a lot of storage space.
You should really only cache what you need and what images are used repeatedly. Both options 1 and 2 are dependant on the phone even having storage space, if there's no space then you won't be caching much to disk. 3 won't store images permanently, if the app is put in the background and gets killed and so will the cache.
It's hard to say what the correct decision here is without knowing the inner workings and requirements of the app.
Just going by what you want then I would say go with several solutions
If there's external storage write to that, if not use internal storage, if there's not enough space internally use the soft references.
I have always found that out of the box caching solutions never fit quite right with my apps and wind up writing my own cache. Sorry I can't give a better answer.
PS. If you're not careful with 3 you might download too much and run into an OutOfMemoryException.
PPS. 3 can also throw a RejectedExecutionException which is caused by too many async tasks being started at once (eg scrolling quickly through a listview that gets an image via asynctask)
I'm using Jake Wharton's DiskLruCache lib.
I'm curious about app performance, caching strategy, using caching both in a view and across a whole application. Most of the time, the image won't change.
Let's say for example I have a 320x320 photo on my server. I open the stream, save the image.
In my list views, I show bitmaps and in the detail, I show a larger image. Should I save a thumbnail bitmap too? Is that more efficient?
What is your experience with sharing the cache "object" across the entire app (let's say I have multiple views that might leverage the same data. What are the issues with this?
For the sake of performance and currency, what if the image changes on the server. What's the best strategy to know that it's changed? I don't have access to modified date. Only size and yet, I don't really want to query size every time either. Set a flag in the app on the server and then query the flag?
In a traditional application (if there is such a thing), what's the best practice for clearing the cache from time to time? (indent weirded out.)
(I was inspired to write this after seeing all of the performance improvements by Facebook in iOS. I don't have billions to do caching but I would like to at least be smart about it! LOL)
A lot of these answers depend on the type of app you're writing, how important image updates are (and how likely images will change, etc), and total images produced. Aside from disk caching, you should be using memory caching as well, especially in ListViews and other areas where the same images will be scrolled through repeatedly. Look at LruCache and read the Caching Bitmaps entry from Google.
320x320 is probably too big for a listview, you will likely want to create thumbnails (depending on device, and how you're implementing your listviews).
1) You should be using Disk caching fairly aggressively (how you define that is up to the app you're writing). Use the external storage directory, and if they have a few GB left over, it's not an issue if you take 100 mb for your app for instance. It can all be cleared away if it's ever needed anyway.
2) There shouldn't be an issue. Disk IO (even to a flash medium) should never be handled on the main thread. Use AsyncTasks to load the images. There can only be one main foreground activity at once anyway, and while an activity is sleeping, it shouldn't be trying to read from the disk anyway.
3) Again this depends on how you're implementing your app. You should be able to get additional information when retrieving the file (even Apache can tell your app the last modified date).
3.1) You can have a sqllite db that keeps track of how often certain images are used, and most recent read. If the most recent read is a few days old, let that image expire.
Edit: There's a library from Jake Wharton now called Picasso that I would recommend using, which handles the Network/local IO as well as memory and disk caching. Check it ou here: http://square.github.io/picasso/ . A lot of what you will need to do can be accomplished with one line: Picasso.with(this).load(imageFileURL).into(imageView);