How to Go to main Activity - android

I have wrote a code for a button to let me move from an activity in my application to my main activity. But an error occurred during run :
07-21 09:28:13.864: E/dalvikvm-heap(371): 25165824-byte external allocation too large for this process.
07-21 09:28:13.864: E/GraphicsJNI(371): VM won't let us allocate 25165824 bytes
I have used an image and stored them in my database but without any recycle is this the problem?
Here is my code in bitmap :
try {
Log.d("to get image", "ok");
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageInfo.get(position)).getContent());
imageView.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
My app suppose to retrieve default images from youtube for any key search word and store them in my data base.

Android apps have a limited heap size (minimum on old devices is as little as 16MB). This varies per device, so be careful during QA. Since a bitmap can take as much as 4 bytes per pixel, large bitmaps can easily fill up your heap (1000 x 1000 pixels = 3.8MB). Your bitmap seems to take 25MB which is huge!
What this means is that you have to be very careful and use several tactics to avoid wasting memory:
When you don't need the bitmap anymore, release it and call bitmap.recycle() to make sure the memory is freed immediately. If you need the bitmap again in the future, you can load it again from disk/resources.
If the bitmap is larger than your screen size, you can scale it down to save memory. The technique here will let you load it initially reduced by 2/3/4.. If you need more fine-tuning, you can load it full size and then rescale using Bitmap.createScaledBitmap. Just don't forget to recycle() the original large bitmap immediately.
Please note that with bitmap scaling, I did not recommend to just resize your source images. Different Android devices have different resolutions. So I would keep my source images big enough to look perfect on the highest resolution devices available. Low resolution devices usually have less heap memory, so the runtime scaling takes care of those.

This error means that You have a too large image inside. The problem here is, that You get a memory leak. If You use bitmaps, the best way is too use small bitmaps that fits on multiple devices. If You use bitmaps, also call recycle() if this bitmap is not used anymore. I recommend to read through this Guide to use Bitmaps:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Also, there is an alternative if recycling is not possible to You. You can scale Your bitmap to Your needs like perfectly explained in another thread here in stackoverflow:
Strange out of memory issue while loading an image to a Bitmap object

Related

What is difference between MemoryCacheSize and BitmapPoolSize in Glide

I'm curious about MemorySizeCalculator of Glide. I can get default memory cache size from getMemoryCacheSize() and bitmap pool size from getBitmapPoolSize().
MemorySizeCalculator:
/**
* Returns the recommended memory cache size for the device it is run on in bytes.
*/
public int getMemoryCacheSize() {
return memoryCacheSize;
}
/**
* Returns the recommended bitmap pool size for the device it is run on in bytes.
*/
public int getBitmapPoolSize() {
return bitmapPoolSize;
}
I understood memory cache concept as well but not sure bitmap pool size, so I found some information from this link : https://medium.com/#ali.muzaffar/performance-improvement-and-bitmap-pooling-in-android-f97b380cf965
Bitmap pooling is a simple technique (though fairly complex to
implement), that aims to reuse bitmaps instead of creating new ones
every time. To put it simply, when you need a bitmap, you check a
bitmap stack to see if there are any bitmaps available. If there are
not bitmaps available you create a new bitmap otherwise you pop a
bitmap from the stack and reuse it. Then when you are done with the
bitmap, you can put it on a stack.
I think that the bitmap which is pushed to a stack is also cached on memory. Is there anyone who can help me understand this concept?
bitmap pool cached dirty bitmap, that used to save time to create
bitmap. because we can't promise all of bitmap cached in
memoryCache.
memoryCache cached bitmap which you have used before.
Image heavy applications have to decode many images, so there will be continuous allocation and deallocation of memory in application. This results in frequent calling of the Garbage Collector (GC). And if you call the GC too many times, your application UI freezes. Glide use the Bitmap Pool Concept to load images efficiently. By using the Bitmap pool to avoid continuous allocation and deallocation of memory in your application, you reduce GC overhead, which results in a smooth-running application.
The basic principe is simple. When processing bitmap you have to verify if bitmap 2 is the same as bitmap 1. For this, you have to compare them. If it is the case you can reuse bitmap 1 as an inbitmap to reuse the same memory space to process gitmap 2 then skiping GC.
Memory cache is a memory space allocated to each application. One application cannot access the cache memory of other application.. It's size is limited.

Bitmap Size Exceed VM budget In android

In my android app i have set the image on ImageView capture by camera. its working fine. but when i capture image and back to the screen with setting that image on imageview and if i do this 3 to 4 time i got This ERROR BITMAP SIZE EXCEEDS VM BUDGETS ...................please need any solution for it.
this is my code below :
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.inTempStorage = new byte[16 * 1024];
image_from_camera.setImageBitmap(BitmapFactory.decodeFile(
"/sdcard/"+chore_string+".jpg", options));
i have done using bitmap.recycle(); or System.gc();
but this is not working . i need to have free the memory from bitmap when again camera button click to take second picture in app. PLEASE NEED ANY HELP........
THANKS. ...
increasing the value of the inSampleSize will decrease the memory overhead required to load the image. Change it to 8,12 or 16... and the image should eventually load but it might not look very good.
However, it seems like you might be using a good bit of memory prior to the load.
Use System.getRuntime() to get the runtime and then see what your max memory, total memory and free memory are to better understand how much space you have.
It's possible you are leaking memory prior to this allocation.
http://developer.android.com/reference/java/lang/Runtime.html
also (annoyingly) if you are using a device that can't run android 3.0 not all of the memory used will show up in the runtime calls. consider using a 3.0 device to more fully understand the memory usage.

android how to load/save original size Bitmap with no OutOfMemory

I read many discussions about the inSampleSize OutOfMemory dilemma.
Cannot get a good solution so i ask a question about it.
Im currently loading a bitmap with inSampleSize=4.
That will give me a Bitmap with the size 648x388.
Original On disk size is 2592x1592.
Im writing text on 648x388 bitmap and saving it back to disk.
Im writing on the 648x388 because the 2592x1592 give me OutOfMemory .
The way it works is that there can be 1-10 648x388 Bitmaps to be saved in a while loop.
I want to change this loop to save 1-10 2592x1592 Bitmaps.
How can i securely load the 2592x1592?
I don care about the resolution going down 60% or more.
As long as the Bitmap has the same size 2592x1592.
Is there a way to maybe keep the size but make Bitmap thinner,
removing color without making quality bad.
My first thought was going something like this to get the biggest bitmap i could get:
I have not tested this but get a feeling it's a bad way
boolean work = true;
int insample = 2;
BitmapFactory.Options options = new BitmapFactory.Options();
while(work){
try{
options.inSampleSize = insample;
bitmap = BitmapFactory.decodeFile(filePath,options);
work = false;
}catch(Exception e){
insample++;
}
}
any help would be grate.
Image processing requires a lot of memory. you cant use the whole bitmap and just modify it on your phone. use a web service for that. upload, process, download. sorry there is no other way a decoded bitmap just takes a lot of memory.
And by the way you cant catch an outOFMemory Exception. the app just crashes.
There's a hard limit on process size in Android and a 4 mega-pixel image at four bytes a pixel will hit it all by itself (on many devices), without any room for your program.
I think you are going to need to do one of two things: Create a web service to do the image processing on a server/in the cloud; or learn to do your image processing "on-the-fly" by manipulating the data directly instead of using a bitmap.

Why so much memory?

I have a 1000x1500 pixel bitmap of which I want to make a mutable copy in Android.
When I run the following code...
// int width = original.getWidth(); // 1000px
// int height = original.getHeight(); // 1500px
final Bitmap result = original.copy(original.getConfig(), true);
original.recycle();
...I get an OutOfMemoryError on the copy line:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
ERROR/GraphicsJNI(419): VM won't let us allocate 6000000 bytes
Why does the copy instruction need 6MB (!) for a 1000x1500 pixel bitmap?
How can I create a mutable bitmap from a non-mutable one in more memory-efficient way?
Edit
BitmapFactory returns inmutable bitmaps. Apparently, the only way of creating a mutable bitmap from an immutable one is to copy it into a new mutable bitmap. In the case of a 1000x1500 bitmap, this apparently requires 12MB (1000x1500x4x2), which causes an OutOfMemoryError in most Android devices.
Is this problem unsolvable in Android then?
To answer your first question:
1000*1500*32/8=6000000
(32 bits/pixel for color information)
To answer your second question: you need to reduce the size of the image, either by processing it in chunks, or be reducing the resolution or color depth.
cdonner put me in the right direction.
Turns out that the original bitmap was using ARGB_8888, which requires 32 bits per pixel and is more than what was needed for this particular app.
Changing the bitmap config to RGB_565, which requires 16 bits per pixel, reduced memory consumption by half.
There is a tricky workaround which I used to avoid OutOfMemoryError.
I registered a receiver so that it ran on different process:
<receiver android:name=".ImageTransformReceiver"
android:exported="true" android:process=":imageTransformProcess"/>
Inside a receiver I do expensive memory operations (loading of two large images and merging them into one). I write the result image into file and send broadcast back to main process referring to the result image file path in Intent.
This is just a hack that allows you to use more OS memory inside one application. Hope this helps.
As of API level, 11 BitmapFactory.Options has a boolean 'inMutable' which can be set to produce mutable Bitmaps.
While this doesn't change the memory use for the individual bitmap, it will save you from having to store two copies in memory.

Android: How does Bitmap recycle() work?

Let's say I have loaded an image in a bitmap object like
Bitmap myBitmap = BitmapFactory.decodeFile(myFile);
Now, what will happen if I load another bitmap like
myBitmap = BitmapFactory.decodeFile(myFile2);
What happens to the first myBitmap? Does it get Garbage Collected or do I have to manually garbage collect it before loading another bitmap, eg. myBitmap.recycle()?
Also, is there a better way to load large images and display them one after another while recycling on the way?
The first bitmap is not garbage collected when you decode the second one. Garbage Collector will do it later whenever it decides. If you want to free memory ASAP you should call recycle() just before decoding the second bitmap.
If you want to load really big image you should resample it. Here's an example: Strange out of memory issue while loading an image to a Bitmap object.
I think the problem is this: On pre-Honeycomb versions of Android, the actual raw bitmap data is not stored in VM memory but in native memory instead. This native memory is freed when the corresponding java Bitmap object is GC'd.
However, when you run out of native memory, the dalvik GC isn't triggered, so it is possible that your app uses very little of the java memory, so the dalvik GC is never invoked, yet it uses tons of native memory for bitmaps which eventually causes an OOM error.
At least that's my guess. Thankfully in Honeycomb and later, all bitmap data is stored in the VM so you shouldn't have to use recycle() at all. But for the millions of 2.3 users (fragmentation shakes fist), you should use recycle() wherever possible (a massive hassle). Or alternatively you may be able to invoke the GC instead.
You will need to call myBitmap.recycle() before loading the next image.
Depending on the source of your myFile (E.g. if it is something you have no control over the original size), when loading an image instead of just simply resampling some arbitrary number, you should scale the image to the display size.
if (myBitmap != null) {
myBitmap.recycle();
myBitmap = null;
}
Bitmap original = BitmapFactory.decodeFile(myFile);
myBitmap = Bitmap.createScaledBitmap(original, displayWidth, displayHeight, true);
if (original != myBitmap)
original.recycle();
original = null;
I cache the displayWidth & displayHeight in a static that I initialized at the start of my Activity.
Display display = getWindowManager().getDefaultDisplay();
displayWidth = display.getWidth();
displayHeight = display.getHeight();
Once bitmap had been loaded in memory , in fact it was made by two part data.
First part include some information about bitmap , another part include information about pixels of bitmap( it is maked up by byte array).
First part exisits in Java used memory, second part exisits in C++ used memory. It can use each other's memory directly.
Bitmap.recycle() is used to free the memory of C++.
If you only do that,the GC will collection the part of java and the memory of C is always used.
Timmmm was right.
according to :
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
In addition, prior to Android 3.0 (API Level 11), the backing data of a bitmap was stored in native memory which is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash.

Categories

Resources