简体   繁体   中英

Out of memory issue..App starting with 10mb memory

I have built a app for galaxy tab using fragments to display around 55 pages.Most of them are bitmaps and rest are dynamic pages(using drawables). When i start the app it consumes around 10mb and then it keep on increasing like 2 mb per page. When it reaches to around page number 40 it crashes with Out of memory error(Around 55MB).

Here is the code of my main activity and a fragment.

package com.example.hscroll.demo;

import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;

import com.example.hscroll.library.imagezoom.ImageViewTouch;

public class MainAct extends FragmentActivity{

private PagerAdapter mPagerAdapter;
private CustomViewPager viewPager;

//public static int PAGE_NUMBER = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);     

    super.setContentView(R.layout.viewpager_layout);
    this.initialisePaging();        
}

private void initialisePaging()
{
    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
    fragments.add(Fragment.instantiate(this, FragmentSignupForm.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment3.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment4.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment5.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment6.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment7.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment8.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment9.class.getName()));
    fragments.add(Fragment.instantiate(this, MainFragmentActivity.class.getName()));
    fragments.add(Fragment.instantiate(this, FragmentQuestionaire.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment10.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment11.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment12.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment13.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment14.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment15.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment16.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment17.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment18.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment19.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment20.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment21.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment22.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment23.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment24.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment25.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment26.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment27.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment28.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment29.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment30.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment31.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment32.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment33.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment34.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment35.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment36.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment37.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment38.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment39.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment40.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment41.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment42.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment43.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment44.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment45.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment46.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment47.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment48.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment49.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment50.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment51.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment52.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment53.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment53.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment54.class.getName()));


    this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(),fragments);

    viewPager = (CustomViewPager)findViewById(R.id.viewpager);
    viewPager.setAdapter(this.mPagerAdapter);   
    ImageViewTouch.customViewPager = viewPager;
}
}

Fragment - -

package com.example.hscroll.demo;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.example.hscroll.library.imagezoom.ImageViewTouch;

public class Fragment2 extends Fragment{

ImageViewTouch imgview ;
LayoutInflater inflater;
FileInputStream in;
BufferedInputStream buf;
Bitmap bitmap;

private final String  PATH = "/mnt/sdcard/Ideal Solar/Layout_1.png";

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
    this.inflater = inflater;
    if(container == null)return null;
    container = (LinearLayout)inflater.inflate(R.layout.fragment0_layout, container, false);

    imgview= (ImageViewTouch)container.findViewById(R.id.imageView1);
    bitmap = SelectImageFunctions.selectImage(inflater.getContext(), PATH);
    if(bitmap!=null)
        imgview.setImageBitmapReset( bitmap, true );

    return container;
}   

 @Override
    public void onResume() {

        if(bitmap == null)
        {
            bitmap = SelectImageFunctions.selectImage(inflater.getContext(), PATH);
        }
        super.onResume();
    }

 @Override
    public void onDestroyView() {
         bitmap.recycle();
            bitmap = null;
         super.onDestroyView();
    }    
}

I am using this library to apply zoom,scroll etc on images/pages.

Thanks

in manifest file add android:largeHeap="true" entity. thanks to that application can allocate more size from heap.

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:largeHeap="true" >

Unfortunately, there is a hard limit on how much memory Java code can use in Android. Actually there are two hard limits: general Java object heap usage, and bitmap usage. Each seems to be around 20MB (possibly more on newer devices with more RAM). There is no option for adjusting these limits available to the developer or user (at least on an unrooted device).

Interestingly, these limits do not apply to native code. That is free to consume as much RAM as it likes, until the system starts killing off processes, and gets round to the one chewing all the RAM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM