Android memory allocation - android

I am getting a "bitmap size exceeds VM budget" error. I have read that there is a 16MB memory limit. In this thread Romain Guy says that "you can only allocate 16 MB
of memory for your entire application".
However, my app must be running out of memory long before it reaches that limit. So my question is: how do I allocate memory to my application ... how can I get the allocation to my app increased (within the 16MB maximum)?

As with any Java VM, the heap memory will automatically grow to the max size. But, bitmaps are allocated outside the VM, so you don't "see" them easily in the stats. The best thing you can do is make sure you don't use large bitmaps, or scale them down using
http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
From Eclipse you can generate a heap dump when you are on Android 1.6 or up and you can analyze the dump with Eclipse MAT.
Generally you can't control the max heap size on a real device, unless you are working with custom hardware or firmware.
There should be an article at developer.android.com on dumping the heap on 1.6, but I'm unable to find it. :(
Edit
Also, I have to mention that you can request more memory for applications by using
android:largeHeap="true"
in the manifest. But this is highly ill-adviced as most applications do not need this.

Note that the heap limit is device-dependent. On a Droid or Nexus One, that limit is 24 MB (to accommodate the larger graphic resources.)

If you're using threads, then the debugger might be the source of the problem. If you run the app under the debugger, then any threads created will still be retained by the debugger, even when they're finished running. This leads to memory errors that won't occur when the app is running without the debugger.
http://code.google.com/p/android/issues/detail?id=7979
https://android.googlesource.com/platform/dalvik/+/master/docs/debugger.html

I found the answer for your question recently.
Go to Android SDK Directory and
run sdk manager (Tools->android run this file)
In SDK manager go to Tools-->Manage AVDs
Now Android Virtual Device Manager Dialogue box is open..
in that window select your AVD and click Edit
Now in SD card section select SIZE and set 1024 MiB
in Hardware section click New and select property " Maximum VM application heap Size"
Now set 100 (based on your app requirement)
Finally click "Edit AVD"
After edit click refresh button on sdk Manager
Now run your app in AVD your problem is solved.

Related

Android Allocated Memory: DDMS vs Running App

Why does DDMS and Android's Running App (Settings->Apps->Running) show another allocated memory value?
DDMS shows that my application allocated memory size is equal ~40MB, on the other hand the Running App shows 80MB.
Which one value is correct? If DDMS value is correct how can I enforce the Running App to show correct value.
There is similar question, but answers are not enough for me.
I answer on my own question.
DDMS shows memory utilized only by Dalvik. The Running App show all memory which is utilized by application it is the sum of:
Native Heap
Dalvik Heap
Dalvik Other
Stack
Other dev
Graphics
and much more, in order to get detailed information about utilized memory please read Viewing Overall Memory Allocations.

strange behavior of android application

I'm developing a large project with android technology
sometimes I have some exceptions like (Out Of Memory Error) on small devices
what I do when an exception occurred is to re-open the application
what happen that it leaves the previous process open and open a new process with new instance of application
I have tried to make
finish()
kill process
System.exit
to close the application and re-open it again but nothing works for me
any help please ???
First Thing you have to take care of, is Heap size.. You can track How much memory is getting consumed when your are running application. Under DDMS perspective there is a heap tab which will give you info about how much memory is consumed when you are running your application.
on what version of the android you are testing ? Android Os will allocate some memory for each application to run.In android 2.2 16mb will be allocated for one application. Version 2.3 allocates 24Mb for an application ,Android 3.0 allocates 42Mb. try testing on different versions of android, you will get to know..
Out of memory happens when RAM in a device is utilized fully, the system will throw this error and kill/restart the process. You need to find the appropriate place where you use the most of memory and handle the memory properly. Out of Memory generally happens because of poor bitmap/image handling. Check your code where you handle Bitmaps/ImageViews/PNG's etc.
Android default heap size is almost good for every heavy load jobs. Trace your code and find memory consuming parts of your code and try to optimize them. To do so, use HEAP tab in DDMS view in Eclipse.
yes that is the problem .i have faced similar problem recently then i resized my images then my problem is solved.Try to find out a way how to reduce the size of the images which you are loading from sever side back-end.

How to increase android virtual device max heap size?

I would like to increase the android virtual device map heap size with Eclipse. I tried to set the Max VM application heap size to 128 in the Eclipse AVD Manager, but it does not work, the line Runtime.getRuntime().maxMemory()/(1024*1024); always returns 48, no matter the set heap size. Device ram size is set to 512. Selected target is Android 4.1.2 (API Level 16). Moreover, I have set android:largeHeap="true" in the manifest file.
Is there a limit to max heap size (is 128MiB to much ?), or is there another file to edit or parameter to set ?
add large heap in Manifest file
<application
android:icon="#drawable/example"
android:label="#string/app_name"
android:largeHeap="true">
......
......
</application>
To do that, in Eclipse, go to "Debug Configurations". You can find that in the drop-down under the "debug" icon. Select "target", and select a preferred emulator target to launch. Then under "additional emulator command line options," add this:
-partition-size 128
Then CLOSE the emulator (and remove any devices), and click the debug icon, which will launch the preferred emulator you selected. This is important: Eclipse needs to launch the debugger, not AVD.
Note that the size (128) is in megabytes.
Take a look also here: http://viralpatel.net/blogs/jvm-java-increase-heap-size-setting-heap-size-jvm-heap/
Or: increase the AVD RAM and the max VM application heap size in VM options:
Go to Window-->AVD Manager-->Virtual Devices-->Edit.
Use the following code. I think it would be help you:
VMRuntime.getRuntime().setMinimumHeapSize(4 * 1024 * 1024);
you can't increase the heap size dynamically.
you can request to use more by using android:largeHeap="true" in the manifest, but you might not get any more heap size than normal, since it's only a request.
also, you can use native memory, so you actually bypass the heap size limitation.
here are some posts i've made about it:
How to cache bitmaps into native memory
JNI bitmap operations , for helping to avoid OOM when using large images
and here's a library i've made for it:
https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations

DDMS and os show different memory info about my app

I'm working with the ddms to find memory leaks and other bugs.
When tracking the memory I see a heap size of 30MB.
But when I go to device and check the memory in the application manager I see 70 MB.
Is it normal?
What's the reason for this ?
Device : SamsunGalaxy GT9300
Yes its normal, DDMS shows only running app memory. You can also try another tool like
memory analyser tool(MAT). You see best result comparison to DDMS.
Here is the link.

how can i see how much memory my app is using out of its VM budget?

I've looked in DDMS but didn't see anything. Thanks.
You should have clicked "Update Heap" button in DDMS in Devices view.
You see only about 3Mb (which is great btw) because the VM allocates just as much as it needs. If you start loading images in your app you'll see it grow. When it reaches 16 (for 320x480 devices) or 24 (for larger devices) Mb, you'll get an OutOfMemory error
sergiu posted some code here: How do I detect the heap size usage of an android application

Categories

Resources