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.
Related
I have been trying to fix a memory leak in my app for a long time, and I keep running into dead ends. The app is heavily image-centric, so I use the Picasso library to handle caching and memory use. I passed the high ram use (up to 100-170mb for my app) to bad memory management on Picasso's part, but I did some testing and disabled memory caching for Picasso, which loads all my images, and there was not a very noticable drop in RAM usage. It was still using 90 to upwards of 170 mb of RAM. I did a full heap dump into a hprof file and opened it with MemoryAnalyzer to see this:
http://i.gyazo.com/6b8d884852fa7cae546fc4cad1fc44c9.png.
If I go to Path to GC Roots, it shows no roots and no parents. There is no link to any of these over 50,000,000 bytes in these 25 massive byte arrays, and I really don't know where to start looking for the cause of it.
Do you have any suggestions on what the bug could be and any possible fixes?
Thank you very much for helping me out!
Try to use LeakCanary to find the memory leak.
Also, you can try Fresco for image loading & displaying, it stores images in the native memory region, so you won't use too much memory from the managed region, GC won't take too much time and you could avoid OutOfMemoryErrors.
One major issue with my application is crashing, which happens a lot due to the huge amount of content my app contains (it is a content-sharing site client). I get many memory errors, and I can be using up to 170-180 MB of ram, which is ridiculous.
http://i.gyazo.com/6cd53e6cf6f0a9bfdd6a24b323a70b09.gif http://gyazo.com/b64d50f76b2ef608954a6d6cdd5d52d0
Those screenshots are just from loading 25 submissions and scrolling through them.
My current setup is like so: LruCache with size of
(Runtime.getRuntime().maxMemory() / 1024) / 8
which handles all submission images. When I load a submission photo or thumbnail, it goes into that cache. Albums are handled by a simple ArrayAdapter and WeakHashMap store for bitmaps, because it is rarely called (maybe 1 out of every 25-30 posts contains an album). Gifs are streamed through GfyCat to a VideoView, no real crashes occur on gifs or albums. The real errors occur when I am scrolling, which is strange because I load the images into the LruCache all at once to save mobile radio time (battery improvements).
The issue seems to be that android is trying to possibly put more into the LruCache than it can, because I get errors like this
java.lang.OutOfMemoryError: Failed to allocate a 3169972 byte allocation with 1400991 free bytes and 1368KB until OOM
even though my LruCache size is 24576kb.
Am I handling memory correctly? What steps can I take to improve stability but keep the app speedy?
Thanks!
You can also even enhance the picasso further by use this configuration
Picasso.with(this)
.load(YOUR_URL)
.config(Bitmap.Config.RGB_565).fit()
.into((ImageView) findViewById(
R.id.frame_main_main_layout));
it will decrease allocating the memory and make the performance better
I ended up switching from the Ion image loading library to Picasso, and have saved 33% of ram usage with automatic caching, so I got rid of the LruCache and all my stores, now it's working better than ever!
This is one of the most asked question by beginners but still I couldn't get any help unfortunately. In an activity I have a viewflipper to which I assign imageviews with images programatically ( around 100 images added to viewflipper using for loop ). Might be due to image size or due to the large no of images I get OutOfMemory Error for bitmaps. My Questions are:
If this is because my drawables take memory space, is there any way by which I can release the space occupied by them?
Can garbage collection be the reason for this? If so what's the solution?
Since am using a viewflipper, is there any way by which I can only load my current, previous and next view of viewflipper? Will this help occupy less memory?
Please let me know if my question is confusing or is not understandable. This is an important topic to me as I am facing this problem in almost all my apps.
to avoid,aquire large heap using this in manifest:
android:largeHeap="true"
and almost all problem solutions are present on these links related to memory out of error,hope that one of them may solve your problem:
meomry-out-of-error
Memory Leak and Out of memory Error using List,LinkedList and HashMap
Memory Leak and Out of memory Error using LRU Cache
Memory Leak and Out of memory Error using Disk LRU Cache
Bitmaps are handled by native memory. This means that when the GC collects the bitmap's reference, the bitmap's memory is not immediately removed. To clear the bitmap's memory immediately, you can call bitmap.recycle().
As for the ViewFlipper, you going to want to find a way to manually unload and reload images as they are displayed to the user. Remember to use recycle(), and don't try to display a bitmap that already was recycled.
Hope this helps :)
Now what happens over here is every app in android is alotted a specific amount of heap size that is as little as 20mb
Bitmaps take up a lot of memory, especially for rich images like photographs
so whenever you load a heavy image or array of heavy images it exceeds the heap size and android automatically shuts down the app because it was using more than the allotted heap size
Read Documentaion
So whenever you are working with heavy images
Load a scaled down version
Asynchronously process the image
and then final display it in an imageview
I'm writing an image gallery app and I keep running into out of memory errors. I cache all my images but the problem occurs when I try switching between images really fast. I'm assuming the app is allocating memory faster than the GC has time to free them up (because the crash doesn't happen when I switch images slowly).
After banging my head against this problem for days, I finally decided to give largeHeap setting in the manifest file a try. After this setting, my app no longer crashes no matter how fast I switch between images.
Now, I want to know if there is any convention or general guideline to using largeHeap setting because it probably wouldn't make much sense if, say, a note taking app used largeHeap. Generally speaking, what apps are a good candidate for largeHeap setting?
Thanks
Generally speaking, what apps are a good candidate for largeHeap setting?
Ones where you can justify to the user why you're forcing all their other apps out of memory, to give you an outsized amount of heap space.
Personally, I would not consider "an image gallery app" to qualify. AutoCAD, video editors, and the like would qualify.
With respect to your memory management issues, make sure that you are using inBitmap on BitmapOptions when running on API Level 11+, so you recycle existing buffers rather than go through garbage collection. Particularly for an image gallery, where you probably have a lot of fairly consistent thumbnail sizes, recycling existing buffers will be a huge benefit. This can help both overall memory consumption (i.e., you are truly out of memory) and memory fragmentation (i.e., you get an OutOfMemoryError with plenty of heap space, but no single block big enough for your allocation, due to Android's frakkin' non-compacting garbage collector).
You might also consider looking at existing image cache implementations, such as the one that Picasso has, to see if there are some tips you could learn (or possibly just reuse).
First, make sure you aren't loading larger bitmaps than necessary:
Load a Scaled Down Version into Memory.
Then, before trying largeHeap, try to free the memory quickly yourself:
If you call bitmap.recycle(); as soon as you are SURE you will not use a bitmap again, then the bulk of that bitmap's memory will be immediately freed. (When the GC gets around to it, all that remains is a tiny object.)
On newer Android versions, there are alternatives (instead of recycle) that may be more effective:
Managing Bitmap Memory
Personally, I still use recycle often, especially if I might be loading a different size image, so can't reuse the existing one. Also, I find it easier to code "unloading" of old media separately from "loading" of new media, when changing to a different fragment or activity:
As leave the old fragment, all old bitmaps I recycle (then, if reachable from a static field, set to null).
The rule of thumb for whether to use largeHeap, is to consider it after you've tried alternative ways to reduce memory usage.
Code your app so you could turn it off again, and still run.
For example, monitor your memory usage, and load "scaled down" bitmaps if memory is tight. Will the user really notice if a given image is not at their device's "retina" resolution?
Or if it is an older, slower, device, will largeHeap make your app feel unresponsive / jerky? If so, can you drop resolution even further, or show fewer bitmaps at one time?
Get your app to work in all circumstances, without largeHeap [by techniques mentioned above]. NOTE: you can "force-test" running on tight memory, by allocating some "dummy" bitmaps, and hold references to them in global fields, so they don't get freed.
NOW you are able to evaluate the trade-off, as it affects YOUR app:
When you do turn largeHeap on, use your app heavily - are there places where it is now "more sluggish", or animations "stutter" or otherwise seem less smooth? BE SURE TO TEST ON AT LEAST ONE OLDER DEVICE, AND ON ONE HIGH_RESOLUTION DEVICE.
You might be seeing long GC times, due to the larger heap.
OR you might conclude that largeHeap is working well for you, and now you can confidently say that it is the best choice in your circumstance.
I would like ask you simple question.
I fight with java.lang.outofmemory error. I
t is caused probably by pictures but also I have in my project quite alot unused imports and unused variables at this moment (application with 7 activities and every activity aprox 40 variables).
Take unused imports and unused declared variables memory ?
Do you think, can be java.lang.outofmemory error caused of many variables and imports ?
From Android:
Bitmaps take up a lot of memory, especially for rich images like
photographs. For example, the camera on the Galaxy Nexus takes photos
up to 2592x1936 pixels (5 megapixels). If the bitmap configuration
used is ARGB_8888 (the default from the Android 2.3 onward) then
loading this image into memory takes about 19MB of memory (2592*1936*4
bytes), immediately exhausting the per-app limit on some devices.
Basically, Images are a killer if not used properly.
See this android tutorial on Loading Large Bitmaps Efficiently
Specifically, the code examples to loading in scaled bitmaps from files/resources, at the required resolution.
Imports have no effect on the memory, at run time. The only thing they might do is slow down the build time. Nothing detrimental.
No, variables not occupy more memory in Heap of application. This may be due to bitmap in your application. If you are getting any error, make sure you have released images background like
imageview.setBackgroundDrawable(null);
relativeLayout.setBackgroundDrawable(null);
or
imageview.setBitmapImage(null);
This will remove drawable images used in layouts.