Java 内存不足异常+;分析hprof文件转储

Java 内存不足异常+;分析hprof文件转储,java,android,out-of-memory,heap-memory,Java,Android,Out Of Memory,Heap Memory,这与这个问题有关 我创建了有问题的转储文件。。它提供了以下信息 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[]

这与这个问题有关

我创建了有问题的转储文件。。它提供了以下信息

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[]
请帮助我,我绝望了。如何解决内存问题

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();
    }
}
请尝试以下代码:

Resources res = getContext().getResources();
int id = R.drawable.image; 
Bitmap b = BitmapFactory.decodeResource(res, id);    
_img .setimagebitmap(b);

通常,当我在低Ram设备上遇到这种异常时,这是因为对于这种分辨率,我的某种图像非常昂贵(很多kb)。 我遇到了一个奇怪的问题,因为我只使用了xhdpi和hdpi drawables目录,在mdpi甚至ldpi设备上,hdpi转换到正确的分辨率会引发此异常,因为无法调整图像大小

这在任何设备上都会发生?你能不能检查一下,你有没有这种低端设备的特定图形

通过HPOF文件的任何方式,您都可以看到哪种对象或至少哪种活动/视图存在问题。 你需要分析所有这些数据(没有一个总体计划来找到它正在崩溃你的应用程序的资源),看看哪个有问题


希望这对您有所帮助,如果有任何问题,请随时提问。

我们的想法是对您的图像进行向下采样,以便它在较小的屏幕上看起来很好,并且您不必在内存中加载整个位图

1) 首先获取要显示的ImageView/屏幕的大小

2) 通过传入位图工厂.Options.inJustDecodeBounds读取位图的大小。这将为您提供位图的大小,而不是加载整个位图

3) 得到一个样本大小。计算屏幕的高度和宽度与图像高度和宽度的比率。使用最小的尺寸,使最大尺寸看起来很好

4) 最后使用下面的函数获得不会消耗内存的下采样图像

2) (代码)

4) (代码)

在清单中添加largeHeap=“true”可能会有所帮助。这将允许应用程序使用更多内存


请参阅:

粘贴您的代码,以便我们进行分析。@DevCarlsberg我已经发布了我的主文件。。请查查..老兄过去会帮你的.这一切我都做了。。我还是会犯同样的错误。。我的文件大小都是30-40kb,是的,我已经将heap设置为large@devcarlsbergok,让我给你解码位图的代码。它将解决你的问题issue@DevCalsberg此代码不起作用eclipse不接受它。。Resources res=getContext().getResources();home.setimagebitmap(b);这两种代码都会产生错误。。这段代码在逻辑上与我使用的代码不一样吗???这是一条最好的绷带,从链接中可以看出:“大多数应用程序不应该需要它,而应该专注于减少它们的总体内存使用。”
Resources res = getContext().getResources();
int id = R.drawable.image; 
Bitmap b = BitmapFactory.decodeResource(res, id);    
_img .setimagebitmap(b);
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, bitmapOptions);
int imageWidth = bitmapOptions.outWidth;
int imageHeight = bitmapOptions.outHeight;
inputStream.close();
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;
  }