Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.OutOfMemoryError: Failed to allocate

So I made an activity with a viewPager, and well, it crashes as soon as the viewPager.setAdapter works. Here's the crash log.

java.lang.OutOfMemoryError: Failed to allocate a 26488162 byte allocation with 16777216 free bytes and 24MB until OOM
                  at java.lang.StringFactory.newStringFromChars(Native Method)
                  at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:629)
                  at java.lang.StringBuffer.toString(StringBuffer.java:723)
                  at java.io.StringWriter.toString(StringWriter.java:100)
                  at android.util.Log.getStackTraceString(Log.java:345)
                  at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:61)
                  at com.android.internal.os.RuntimeInit.-wrap0(RuntimeInit.java)
                  at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:86)
                  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
                  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

Here's the activity's class

public class SetupActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);
        ViewPager setupPager = (ViewPager) findViewById(R.id.setupViewpager); 
        SetupPagerAdapter setupPagerAdapter = new SetupPagerAdapter(getSupportFragmentManager());    
        setupPager.setAdapter(setupPagerAdapter);
   }
}

Here's the SetupPagerAdapter

public class SetupPagerAdapter extends FragmentPagerAdapter {
    public SetupPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        if(position == 0){
            return new SetupFragmentOne().newInstance();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 1;
    }
}

and Here's SetupFragmentOne

public class SetupFragmentOne extends Fragment {
    public SetupFragmentOne newInstance() {
        SetupFragmentOne fragment = new SetupFragmentOne();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.setup_fragment_1, container);
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(),"coolvetica_rg.ttf");
        TextView tv = (TextView) view.findViewById(R.id.setupText1);
        tv.setTypeface(typeface);
        return view;
    }
}
like image 400
Ahmad Avatar asked Oct 25 '25 06:10

Ahmad


1 Answers

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/

like image 116
rafsanahmad007 Avatar answered Oct 26 '25 20:10

rafsanahmad007