Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

out of memory exception + analyzing hprof file dump

this is in connection with this question

java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

i created the dump file in question.. and it gives following information

One instance of "byte[]" loaded by "<system class loader>" occupies 1,10,59,216
(51.02%) bytes. The memory is accumulated in one instance of "byte[]" 
 loaded by "<system class loader>".

Keywords byte[]

so now what can be done?? how do i clear the problem??

my list_objects[context]-inbound file

CLASS NAME                                                                 SHALLOW HEAP   RETAINED HEAP  
byte[11059200] @ 0xb4979590                                               |  1,10,59,216 |   1,10,59,216
mBuffer android.graphics.Bitmap @ 0xb3dc68d8                              |48            | 48
mBitmap android.graphics.drawable.BitmapDrawable @ 0xb3dbba60             | 72           | 144
mBackground android.widget.RelativeLayout @ 0xb3db3fc0                    |512           | 10,144
mBitmap android.graphics.drawable.BitmapDrawable$BitmapState @ 0xb3dc0068 |40          | 40
mBitmapState android.graphics.drawable.BitmapDrawable @ 0xb3dbba60        |72          |  144
referent java.lang.ref.WeakReference @ 0xb3dc2d68                         |24          |  24

pls help i am desperate. how can i solve the memory problem??

my home_screen.java

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_page);
    main();
 private void main() {
    // TODO Auto-generated method stub


    final Button home;
    final Button aboutus;
    final Button contacts;
    final Button clients;
    final Button services;

    try
    {

    home = (Button)findViewById(R.id.btnHome);
    aboutus = (Button)findViewById(R.id.btnAboutus);
    clients = (Button)findViewById(R.id.btnClients);
    contacts = (Button)findViewById(R.id.btnContacts);
    services = (Button)findViewById(R.id.btnServices);

    home.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.home1);
            Drawable d = new BitmapDrawable(getResources(),b);              
            home.setBackgroundDrawable(d);
            System.gc();
            Intent myIntent = new Intent(Home_Screen.this, Button_Anime.class);
            startActivity(myIntent);
        }
    });
    aboutus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.about1 );
            Drawable d = new BitmapDrawable(getResources(),b); 
            aboutus.setBackgroundDrawable(d);
            Intent myIntent = new Intent(Home_Screen.this, AboutUs.class);
            startActivity(myIntent);
        }
    });
    clients.setOnClickListener(new  OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.clients1 );
            Drawable d = new BitmapDrawable(getResources(),b); 
            clients.setBackgroundDrawable(d);
            Intent myIntent = new Intent(Home_Screen.this, Clients.class);
            startActivity(myIntent);
        }
    });
    contacts.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.contact1);
            Drawable d = new BitmapDrawable(getResources(),b);
            contacts.setBackgroundDrawable(d);
            Intent myIntent = new Intent(Home_Screen.this, Contacts.class);
            startActivity(myIntent);
        }
    });
    services.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.services1 );
            Drawable d = new BitmapDrawable(getResources(),b);
            services.setBackgroundDrawable(d);
            Intent myIntent = new Intent(Home_Screen.this, Services.class);
            startActivity(myIntent);
        }
    });

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
like image 543
user3214173 Avatar asked Jun 11 '14 06:06

user3214173


People also ask

How do I get rid of heap dump on memory error?

The parameter -XX:+HeapDumpOnOutOfMemoryError can be set to enable heap dump when the JVM runs out of memory. You have set this parameter in the BW_JAVA_OPTS environment variable. By default the heapdump snapshot gets stored in the root location with the name java_pid1.

Is it OK to delete Hprof files?

A hprof is a heap dump, which happens if the JVM has an OOM issue. You can delete it if you want, but it's probably worth digging into.

How do I analyze a .hprof file?

In order to analyze the hprof files, there is a tool called Memory Analyzer(MAT) provided by SAP and IBM jointly.

How do you analyze out of memory error?

The most preliminary thing to understand when trying to assess and understand an out of memory error the memory growth characteristics. Make your conclusions about the following possibilities: Spikes in usage: This type of OOM could be drastic based on the type of load.


2 Answers

The idea is to down sample your image so that it looks good on the smaller screen and that you dont have to load the entire bitmap in memory.

1) First get the size of your ImageView/ screen that you will be displaying on.

2) Read the size of you Bitmap by passing in BitmapFactory.Options.inJustDecodeBounds. This will give you the size of the Bitmap rather than loading the entire bitmap.

3) Get a insample size. Calculate the ratio of height and width of the screen to the image height and width. Use the smallest one so that the biggest dimension looks good.

4) Final use the function below to get the down sampled image that wont eat up your memory.

2)(code)

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, bitmapOptions);
int imageWidth = bitmapOptions.outWidth;
int imageHeight = bitmapOptions.outHeight;
inputStream.close();

4)(code)

private Bitmap downscaleBitmapUsingDensities(final int sampleSize,final int imageResId)
  {
  final Options bitmapOptions=new Options();
  bitmapOptions.inDensity=sampleSize;
  bitmapOptions.inTargetDensity=1;
  final Bitmap scaledBitmap=BitmapFactory.decodeResource(getResources(),imageResId,bitmapOptions);
  scaledBitmap.setDensity(Bitmap.DENSITY_NONE);
  return scaledBitmap;
  }
like image 129
toidiu Avatar answered Sep 28 '22 05:09

toidiu


Adding largeHeap="true" in your manifest may help. This will allow your application to use more memory.

See: http://developer.android.com/guide/topics/manifest/application-element.html

like image 28
clemp6r Avatar answered Sep 28 '22 05:09

clemp6r