内存不足问题..App以10mb内存开头

时间:2012-01-28 06:36:33

标签: android android-3.0-honeycomb android-fragments out-of-memory android-image

我已经使用片段为galaxy tab构建了一个应用程序,显示大约55页。其中大部分是位图,其余是动态页面(使用drawables)。当我启动应用程序时,它消耗大约10mb,然后它继续增加,如每页2 mb。当它到达第40页左右时,它会因内存不足错误(大约55MB)而崩溃。

这是我的主要活动和片段的代码。

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;
}
}

片段 - -

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();
    }    
}

我正在使用this库在图像/页面上应用缩放,滚动等。

由于

2 个答案:

答案 0 :(得分:3)

清单文件中的

添加android:largeHeap =“true”实体。 感谢该应用程序可以从堆中分配更多大小。

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

答案 1 :(得分:0)

不幸的是,Java代码可以在Android中使用多少内存存在硬性限制。实际上有两个硬限制:一般Java对象堆使用和位图使用。每个似乎大约20MB(可能更多在具有更多RAM的新设备上)。有 no 选项可用于调整开发人员或用户可用的这些限制(至少在无根设备上)。

有趣的是,这些限制不适用于本机代码。这可以自由地消耗尽可能多的RAM,直到系统开始杀死进程,然后转向咀嚼所有RAM的那个。