I am using .cacheOnDisc() method for storing images in cache memory (Default method-Unlimited Extrenal Storage).should i enable cacheInMemory for my app?what r the effects will be if i dont use cacheInMemory option?
A memory cache caches the images in memory (RAM), i.e. it does not have to load and decode the image from internal storage because that is rather slow. You should IMO always use a memory cache.
The effect of not using memory cache can be - depending on implementation - that scrolling through lists of images is either stuttering or slower than necessary.
Disk (e.g. SD card) cache makes sense if the images are downloaded from the internets and you don't want to re-download them each time the app starts again. Local storage is much faster than the internets but still much slower than memory.
Related
In android many image loading libraries (like Picasso -- which uses 2% of the storage for disk cache, Glide) use disk cache in addition to in memory lru cache. I can understand why this might be useful for images downloaded from the network -- if the in memory cache is full, read it from disk rather than fetching them remotely -- thus avoiding network latency etc. However, if we are just reading local images on the android device itself -- do we gain anything by using a separate disk cache with the serialized bitmap data -- since the data will have to be read from the disk anyways ? Probably makes sense if your app needs a thumbnail and subsample the original image once and store it in cache ? Are there any studies showing perf gains. I have seen use of disk cache in googles samples and other bitmap cache libraries.
so here are some refs I found in the AOSP docs:
A memory cache is useful in speeding up access to recently viewed
bitmaps, however you cannot rely on images being available in this
cache. Components like GridView with larger datasets can easily fill
up a memory cache. Your application could be interrupted by another
task like a phone call, and while in the background it might be killed
and the memory cache destroyed. Once the user resumes, your
application has to process each image again.
A disk cache can be used in these cases to persist processed bitmaps
and help decrease loading times where images are no longer available
in a memory cache. Of course, fetching images from disk is slower than
loading from memory and should be done in a background thread, as disk
read times can be unpredictable.
I'm using vectordrawable for displaying images. The images are all resources which are bundled with the app (apk). My problem is, that the memory on every new activity massivly get higher until the app crashes with an OutOffMemoryException.
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:903)
at android.graphics.Bitmap.createBitmap(Bitmap.java:880)
at android.graphics.Bitmap.createBitmap(Bitmap.java:847)
I've looked into MAT for memory leaks, but did'nt find out any more than the bitmap errors.
What are the efficients way to display vectordrawables? Maybe the general architecture of my app isnt right with the activity lifecycle?
I didn't find any informations about the common memory ussage of other apps (facebook, aibnb, whatsapp,..). My usage is around 40-70MB.
There are a few things to be careful about when dealing with images:
Are you loading your images every time when you show them? This can be a potentially very memory consuming operation. It's better to load the images into memory once and then reuse them across the app.
Are you cleaning the memory occupied by the images you no longer use? If you are sure that an image is no longer needed, you should clean the allocated bitmap memory by calling bitmap.recycle(). This invalidates the bitmap and frees all the occupied memory, making it available for other operations.
Are the images too large? There is a limit for the maximum size of a single image. Trying to load a bigger image will also cause an OutOfMemoryError, even though memory can be available. In that case, you may want to optimise the image files that you are trying to load.
Go though your app and check for those potential problems one by one. Also, there are good profiling tools available for the Android platform. They can show you potential problems with the memory management, like excessive memory allocation, etc.
Good luck.
I have a gallery application which loads all the media (images, music and video) thumbnails.
I'm using Universal Image loader to load the images with following configs
DisplayImageOptions mOptions = new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565)
.showStubImage(R.drawable.media)
.showImageForEmptyUri(R.drawable.media)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.showImageOnFail(R.drawable.media).cacheInMemory().cacheOnDisc()
.build();
and
config = new ImageLoaderConfiguration.Builder(mContext).enableLogging()
.discCache(new UnlimitedDiscCache(cacheDir))
.threadPoolSize(10).build();
This app works flawlessly on a 2GB ram device. But what i have observed is than on lower ram devices, the application is really laggy. Could someone tell me if these configuration are fine? or do i need to alter something to gain better performance in low end devices?
I figured out the problem.
cacheInMemory() was the problem. This will have performance problems in low end devices. or 1GB ram devices.
Also i suggest you not to use unlimited cache. i.e UnlimitedDiscCache(cacheDir)).
Use limited cache since it will take up lot of memory. In my case it was almost 1.2GB on my phone.
Having altered these options. I'm having a good performance.
How is the size of cache and data calculated in the application properties?
I save all my data to the SD card, but Android shows me that I'm constantly using 4 KB, but on the SD card are about 50 KB. The same with the cache! I put a 3 MB picture in there. But my cache size is zero!
I think I'm using the correct directory: /mnt/sdcard/Android/data/my.package.name/files and /mnt/sdcard/Android/data/my.package.name/cache.
Has this changed in Android 4.0.x?
See this screenshot:
I believe the Cache size is the size of the Internal Cache directory that is returned by http://developer.android.com/reference/android/content/Context.html#getCacheDir()
I think you've used:
http://developer.android.com/reference/android/content/Context.html#getExternalCacheDir()
The application properties page only shows usage for the internal memory, not for the external memory (SD card).
I don't know exactly why they chose to do it like this, but one reason would be that the SD card is not managed to the same degree: while you can get Android to manage SD card data for your application, you can also bypass it and use a custom folder structure.
Additionally, external memory is usually far larger than the internal memory, and can be managed by the user, so SD card usage is probably not all that important anyway.
I'm developing a small social networking app that makes use of something like profile pictures. Images are stored on a server and I have scripts set up that will send the image for each user to the app, which then displays it in an image view for each user, and then saves the image to external storage. The way I have it implemented now is that anytime the app needs the image after it downloads it from the server, the app will get the image from external storage unless a user has uploaded a new image (I thought this would be faster than redownloading it from the server every time). However, it seems to be taking longer to get the file from external storage than it does to get it from my server (and the server is pretty slow, running on wifi from 3 floors away...budget constrains :) ).
My question is what is the fastest way to get these images if the user hasn't uploaded a new one. Should I just downlaod it from the server everytime (I'm assuming not) or is there a better place in the filesystem to store the images that makes for faster retrieval?
Loading images from the SD card should be very fast. Some strategies:
Do it only once - Load the images into memory asynchronously when your activity or application starts. You don't want to be hitting the SD card every time your view updates.
Make them small - If you're having performance problems displaying thumbnails in a list, try saving your thumbnails as smaller images using inSampleSize to put less pressure on the decoder.
Use internal memory - I think that internal memory is faster, but it tends to be in short supply. You could certainly store some number of thumbnails in your cache directory to help speed up step 1.
Responsiveness over performance - The golden rule is to remember that absolute performance does not always correlate with responsiveness. Even if the images take a long time to load, choosing cleverly when to load the images can have a great impact on the user's perception of speed.