Android应用程序因内存不足而崩溃

时间:2017-10-05 09:32:34

标签: android

我正在开发一个包含4个标签的应用程序。

  1. 标签1:具有20个组的可展开listview
  2. 标签2:具有30个组的可展开listview
  3. 标签3:Gridview包含22个项目(每行3列) [滚动] 4.Tab 4:网格视图有24个项目(每行3列) [滚动]
  4. 我的Gridview适配器实现如下:(同时用于标签3和4)

    public class LogoImageAdapter extends BaseAdapter{
        private Context context;
        private List<Integer> logoIds;
        public LogoImageAdapter(Context context, List<Integer> logoIds) {
            this.context = context;
            this.logoIds = logoIds;
        }
        @Override
        public int getCount() {
            return logoIds.size();
        }
    
        @Override
        public Object getItem(int position) {
            return logoIds.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return logoIds.get(position);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View gridView;
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                gridView = inflater.inflate(R.layout.grid_item, null);
            } else {
                gridView = convertView;
            }
            /*TextView title = (TextView) gridView.findViewById(R.id.siteNameTV);
                title.setText("Title_" + position);*/
            ImageView logoImage = (ImageView) gridView.findViewById(R.id.logoImageView);
            logoImage.setImageResource(logoIds.get(position));
            return gridView;
        }
    }
    

    应用程序消耗了几乎100 MB的内存并经常崩溃。 我认为应用程序崩溃是由于我的gridview实现。我在哪里弄错了?

    enter image description here

    编辑: logcat的:

    10-05 15:06:55.458 32557-32557/pmp.com.instapukkei W/FragmentManager: moveToState: Fragment state for Classified{3fc90a #0 id=0x7f0c006d android:switcher:2131492973:1} not updated inline; expected state 3 found 2
    10-05 15:06:57.907 32557-32557/pmp.com.instapukkei W/FragmentManager: moveToState: Fragment state for Tourism{33f7512 #2 id=0x7f0c006d android:switcher:2131492973:2} not updated inline; expected state 3 found 2
    10-05 15:06:59.856 32557-32557/pmp.com.instapukkei W/FragmentManager: moveToState: Fragment state for Shopping{6a52599 #3 id=0x7f0c006d android:switcher:2131492973:3} not updated inline; expected state 3 found 2
    10-05 15:07:02.252 32557-32557/pmp.com.instapukkei W/FragmentManager: moveToState: Fragment state for Travel{26da709 #4 id=0x7f0c006d android:switcher:2131492973:4} not updated inline; expected state 3 found 2
    10-05 15:07:02.387 32557-32568/pmp.com.instapukkei I/art: Clamp target GC heap from 102MB to 96MB
    10-05 15:07:07.410 32557-32557/pmp.com.instapukkei W/FragmentManager: moveToState: Fragment state for Tourism{33f7512 #2 id=0x7f0c006d android:switcher:2131492973:2} not updated inline; expected state 3 found 2
    10-05 15:07:08.494 32557-32557/pmp.com.instapukkei I/art: Waiting for a blocking GC Alloc
    10-05 15:07:08.501 32557-32568/pmp.com.instapukkei I/art: Clamp target GC heap from 103MB to 96MB
    10-05 15:07:08.502 32557-32557/pmp.com.instapukkei I/art: WaitForGcToComplete blocked for 8.292ms for cause Alloc
    10-05 15:07:08.502 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:11.500 32557-32557/pmp.com.instapukkei W/FragmentManager: moveToState: Fragment state for Tourism{33f7512 #2 id=0x7f0c006d android:switcher:2131492973:2} not updated inline; expected state 3 found 2
    10-05 15:07:12.775 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:12.775 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:12.802 32557-32557/pmp.com.instapukkei I/art: Alloc sticky concurrent mark sweep GC freed 481(18KB) AllocSpace objects, 3(3MB) LOS objects, 4% free, 91MB/96MB, paused 465us total 26.676ms
    10-05 15:07:12.906 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:12.906 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:12.917 32557-32557/pmp.com.instapukkei I/art: Clamp target GC heap from 103MB to 96MB
    10-05 15:07:12.917 32557-32557/pmp.com.instapukkei I/art: Alloc partial concurrent mark sweep GC freed 840(64KB) AllocSpace objects, 5(7MB) LOS objects, 8% free, 87MB/96MB, paused 599us total 10.801ms
    10-05 15:07:14.432 32557-32564/pmp.com.instapukkei I/art: Debugger is no longer active
    10-05 15:07:15.214 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.214 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.225 32557-32557/pmp.com.instapukkei I/art: Alloc sticky concurrent mark sweep GC freed 250(8KB) AllocSpace objects, 0(0B) LOS objects, 3% free, 92MB/96MB, paused 803us total 10.646ms
    10-05 15:07:15.226 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.237 32557-32557/pmp.com.instapukkei I/art: Clamp target GC heap from 102MB to 96MB
    10-05 15:07:15.238 32557-32557/pmp.com.instapukkei I/art: Alloc partial concurrent mark sweep GC freed 727(59KB) AllocSpace objects, 4(6MB) LOS objects, 10% free, 86MB/96MB, paused 467us total 11.147ms
    10-05 15:07:15.318 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.318 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.323 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.331 32557-32557/pmp.com.instapukkei I/art: Clamp target GC heap from 109MB to 96MB
    10-05 15:07:15.331 32557-32557/pmp.com.instapukkei I/art: Alloc partial concurrent mark sweep GC freed 592(58KB) AllocSpace objects, 0(0B) LOS objects, 2% free, 93MB/96MB, paused 441us total 7.585ms
    10-05 15:07:15.331 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.346 32557-32557/pmp.com.instapukkei I/art: Clamp target GC heap from 109MB to 96MB
    10-05 15:07:15.347 32557-32557/pmp.com.instapukkei I/art: Alloc concurrent mark sweep GC freed 10(12KB) AllocSpace objects, 0(0B) LOS objects, 2% free, 93MB/96MB, paused 408us total 14.839ms
    10-05 15:07:15.347 32557-32557/pmp.com.instapukkei I/art: Forcing collection of SoftReferences for 3MB allocation
    10-05 15:07:15.347 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.366 32557-32557/pmp.com.instapukkei I/art: Clamp target GC heap from 109MB to 96MB
    10-05 15:07:15.366 32557-32557/pmp.com.instapukkei I/art: Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 2% free, 93MB/96MB, paused 459us total 18.639ms
    10-05 15:07:15.366 32557-32557/pmp.com.instapukkei W/art: Throwing OutOfMemoryError "Failed to allocate a 3686412 byte allocation with 2923072 free bytes and 2MB until OOM"
    10-05 15:07:15.376 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.376 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.381 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.390 32557-32557/pmp.com.instapukkei I/art: Clamp target GC heap from 109MB to 96MB
    10-05 15:07:15.390 32557-32557/pmp.com.instapukkei I/art: Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 2% free, 93MB/96MB, paused 430us total 8.601ms
    10-05 15:07:15.390 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.405 32557-32557/pmp.com.instapukkei I/art: Clamp target GC heap from 109MB to 96MB
    10-05 15:07:15.406 32557-32557/pmp.com.instapukkei I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 2% free, 93MB/96MB, paused 435us total 14.874ms
    10-05 15:07:15.406 32557-32557/pmp.com.instapukkei I/art: Forcing collection of SoftReferences for 3MB allocation
    10-05 15:07:15.406 32557-32557/pmp.com.instapukkei I/art: Starting a blocking GC Alloc
    10-05 15:07:15.422 32557-32557/pmp.com.instapukkei I/art: Clamp target GC heap from 109MB to 96MB
    10-05 15:07:15.422 32557-32557/pmp.com.instapukkei I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 2% free, 93MB/96MB, paused 420us total 15.551ms
    10-05 15:07:15.422 32557-32557/pmp.com.instapukkei W/art: Throwing OutOfMemoryError "Failed to allocate a 3686412 byte allocation with 2916528 free bytes and 2MB until OOM"
    10-05 15:07:15.423 32557-32557/pmp.com.instapukkei D/skia: --- allocation failed for scaled bitmap
    10-05 15:07:15.423 32557-32557/pmp.com.instapukkei D/AndroidRuntime: Shutting down VM
    10-05 15:07:15.453 32557-32557/pmp.com.instapukkei E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: pmp.com.instapukkei, PID: 32557
                                                                         java.lang.OutOfMemoryError: Failed to allocate a 3686412 byte allocation with 2916528 free bytes and 2MB until OOM
                                                                             at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                             at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                             at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                                                                             at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                                                                             at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                                                                             at android.content.res.Resources.loadDrawableForCookie(Resources.java:2638)
                                                                             at android.content.res.Resources.loadDrawable(Resources.java:2543)
                                                                             at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                                                                             at android.graphics.drawable.AnimationDrawable.inflateChildElements(AnimationDrawable.java:330)
                                                                             at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:300)
                                                                             at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1215)
                                                                             at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1124)
                                                                             at android.content.res.Resources.loadDrawableForCookie(Resources.java:2633)
                                                                             at android.content.res.Resources.loadDrawable(Resources.java:2543)
                                                                             at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                                                                             at android.view.View.<init>(View.java:3957)
                                                                             at android.widget.ImageView.<init>(ImageView.java:155)
                                                                             at android.widget.ImageView.<init>(ImageView.java:150)
                                                                             at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                                             at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                                             at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1031)
                                                                             at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                             at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                             at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                             at pmp.com.instapukkei.tabs.Tourism.onCreateView(Tourism.java:31)
                                                                             at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                             at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1430)
                                                                             at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:728)
                                                                             at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                             at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                             at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                             at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                             at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
                                                                             at android.support.v4.view.ViewPager$3.run(ViewPager.java:254)
                                                                             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                             at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                             at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                             at android.os.Handler.handleCallback(Handler.java:746)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                            at com.andr
    

3 个答案:

答案 0 :(得分:2)

以WEbp格式转换图像

Click Here for WEbp Format

添加到您的Menifest这是临时解决方案

<application
   ........................
   android:largeHeap="true"> 

答案 1 :(得分:1)

使用可用解决方案之一优化您的图片(在class RegistrationContainer extends Component { render() { const MainContent = this.props.mainContent; return ( <Row> <Col offset="lg-3" lg={6}> // Render the passed children {this.props.children} </Col> </Row> ); } } 列表中),例如

This或者只是以正确的方式从您的PhotoShop(或您使用的任何内容)中重新导出。

另一种正确的方法是为不同的dpi生成不同的图标(see tools

  

我强烈建议您使用Glide或Picasso在列表中加载图片,因为它们具有内置缓存,这对于回收的视图很有用

UPD:使用Glide加载资源的示例

logoIds

在适配器中使用ViewHolder pattern,以避免在回收时重新绑定视图(Glide.with(convertView.getContext()) .load(R.id.resource_id) .into(imageView);

答案 2 :(得分:1)

将其添加到您的AndroidManifest.xml文件中。内部应用程序标记。

<application
    ....
       android:largeHeap="true"> 

检查this details