Out of memory on a byte allocation

后端 未结 3 402
忘掉有多难
忘掉有多难 2021-01-15 21:59

This is the error I am receiving on the Android log, exactly, this is:

08-06 12:16:28.763: E/dalvikvm-heap(27065): Out of memory on a 184-byte allocation.


        
相关标签:
3条回答
  • 2021-01-15 22:38

    Since you are using a listActivity check if you have implemented the optimizations shown here.

    I solved a similar issue with list view by implementing the optimization

    Here are some excerpts from the presentation about optimizing a listAdapter

    The Slow way

    public View getView(int position, View convertView, ViewGroup parent) { 
         View item = mInflater.inflate(R.layout.list_item_icon_text, null);
         ((TextView) item.findViewById(R.id.text)).setText(DATA[position]); 
         ((ImageView) item.findViewById(R.id.icon)).setImageBitmap( 
                 (position & 1) == 1 ? mIcon1 : mIcon2);
         return item; 
    }
    

    The Proper way

     public View getView(int position, View convertView, ViewGroup parent) { 
         if (convertView == null) { 
             convertView = mInflater.inflate(R.layout.item, parent, false); 
         } 
         ((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]); 
         ((ImageView) convertView.findViewById(R.id.icon)).setImageBitmap( 
                 (position & 1) == 1 ? mIcon1 : mIcon2); 
         return convertView; 
     }
    

    The Best Way

    static class ViewHolder { 
            TextView text; 
            ImageView icon; 
    }
    
     public View getView(int position, View convertView, ViewGroup parent) { 
             ViewHolder holder; 
    
             if (convertView == null) { 
                 convertView = mInflater.inflate(R.layout.list_item_icon_text, 
                         parent, false);
                 holder = new ViewHolder(); 
                 holder.text = (TextView) convertView.findViewById(R.id.text); 
                 holder.icon = (ImageView) convertView.findViewById(R.id.icon); 
    
                convertView.setTag(holder); 
            } else { 
                holder = (ViewHolder) convertView.getTag(); 
            } 
    
            holder.text.setText(DATA[position]); 
            holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 
    
            return convertView; 
        }
    
    0 讨论(0)
  • 2021-01-15 22:41

    In my case, the problem caused by big-size images. I removed some of them (temporarily for test) and the problem solved. Note that in my case, the problem just was in lowest API-level I was trying the app (API 16).

    Finally, optimized them for solving the problem permanently.

    0 讨论(0)
  • 2021-01-15 22:54

    Sometimes you can get OutOfMemory error, when you try to execute too long SQL statements (eg, you don't use the String[] argument for the variables in the query, but hard code them in the statement).

    Try editing your where statements to field=? format, and specify the variables in the designated query parameter.

    See this thread: Sqlite Out of Memory when preparing update statement

    If this isn't the problem in your case, than I can't think of anything else with this little information.

    0 讨论(0)
提交回复
热议问题