Android: Failed to Allocate Memory

Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

OutOfMemoryError is the most common problem that occurs in android while especially dealing with bitmaps. This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.

As mentioned by Aleksey, you can add the below entities in your manifest file android:hardwareAccelerated="false" , android:largeHeap="true" it will work for some environments.

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

you should definitely read some of Androids Developer concept's, especially here:Displaying Bitmaps Efficiently

Read all 5 topics and rewrite your code again. If it still doesn't work we will be happy to see what you've done wrong with the tutorial material.

Here some of the possible answers for these type of errors in SOF

Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap

How to solve java.lang.OutOfMemoryError trouble in Android

Android : java.lang.OutOfMemoryError

java.lang.OutOfMemoryError

Solution for OutOfMemoryError: bitmap size exceeds VM budget

Edit: From the comments of @cjnash

For anyone that still had crashes after they added this line, try sticking your image into your res/drawable-xhdpi/ folder instead of your res/drawable/ and this should resolve this issue

Failed to allocate memory: 8

I figured it out. The problem was in the amount of ram I had specified for the virtual machine, and it was 1024MB, now I have 512MB and it is ok, now I need to find how to improve this amount of ram, 512 is not so much, and the machine is a little bit laggy.

java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM

Simple Solution which I found is,
Add hardwareAccelerated & largeHeap under application AndroidManifest.xml

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

java.lang.OutOfMemoryError: Failed to allocate

try this in your manifest file:

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

This error occurs when Java Virtual Machine (JVM) cannot allocate a object due to lack of memory space and also, the garbage collector cannot free some space.

try changing this line:

View view = inflater.inflate(R.layout.setup_fragment_1, container);

to

View view = inflater.inflate(R.layout.setup_fragment_1, container,false);

To resolve out of memory error...better approach :

If you are using lots of bitmaps or drawable images...you should put different resolution images in different folders...hdpi,xhdpi etc...
see this :https://developer.android.com/guide/practices/screens_support.html

Also unbind your drawables when activity destroyed..so that memory can be freed by Garbage collector

 private void unbindDrawables(View view)
{
if (view.getBackground() != null)
{
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup && !(view instanceof AdapterView))
{
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
{
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}

Also see this: https://androidactivity.wordpress.com/2011/09/24/solution-for-outofmemoryerror-bitmap-size-exceeds-vm-budget/

Android Studio Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate memory

The problem isn't these particular allocations. 2 images of that size is semi-reasonable (semi because 2 pictures that big won't both fit on the screen at once, so why not load one at a time as needed). The problem is that all of your allocations together are too much. Look for leaks or memory you hold on to that doesn't need to be held- especially bitmaps. I'd use a heap dump to see if there's way too many of some object.

Also, increasing RAM to 4GB won't work- the entire device doesn't have 4GB. These are phones, not computers (hell computers didn't have that 4 years ago).

Android: failed to allocate memory

Solution to running a faster emulator:

http://androiddevnotes.com/2011/03/08/1299521520000.html

Runs my game a lot faster, comparable to phone quality.

-- Update 08/24/12 --

I see that people still come here and comment on this answer, and I have done more research on this issue. I present to you the results of my findings.

NOTE: These tests were run on my computer, your computer may be different.

  1. Running an Android 2.2 (armeabi) emulator with Property 'Device ram size' = 2048 will
    fail, just as before
  2. Running an Android 2.2 (armeabi) emulator with Property 'Device ram size' = 2048 MB will pass, however if you actually check the ram on the device, its around 128 MB.
    • check 1: Settings > Applications > Running Services - add all the numbers on the bottom of the screen
    • check 2: run the following command in adb shell: cat /proc/meminfo/ and note the MemTotal field
  3. Running an Android 4.1 (armeabi-v7) emulator with Property 'Device ram size' = 2048 will work, however if you actually check the ram on the device its around 512 MB
    • check 1: Settings > Apps > Running tab - add all the numbers on the bottom of the screen
    • check 2: run the following command in adb shell: cat /proc/meminfo/ and note the MemTotal field
  4. Running an Android 4.1 (armeabi-v7) emulator with Property 'Device ram size' = 2048 MB will work as well, but with the same results as 3.

Conclusion

Adding 'MB' as noted in some of the comments below may allow your device to be launched, however it doesn't actually have 2 GB of RAM. Even with a 4.1 Emulator, the RAM Size is at 512 MB.

Recommendation

Please use an actual device for testing.

Android Emulator: Failed to allocate memory: 8 even with 8MB RAM

I've had problems like this. The only way to fix it was to set it to either 256, 512 or 1024. I have no idea why it won't work with any random value.



Related Topics



Leave a reply



Submit