java – 内存异常分析hprof文件转储
发布时间:2020-12-15 00:48:26 所属栏目:Java 来源:网络整理
导读:这是与这个问题有关的 java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 我创建了有问题的转储文件..它提供以下信息 One instance of "byte[]" loaded by "system class loader" occupies 1,10,59,216(51.02
这是与这个问题有关的
java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 我创建了有问题的转储文件..它提供以下信息 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[] 那么现在可以做什么呢?我如何清除问题? 我的list_objects [context] -inbound文件 CLASS NAME SHALLOW HEAP RETAINED HEAP byte[11059200] @ 0xb4979590 | 1,216 | 1,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 请帮助我绝望我如何解决内存问题? 我的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(); } } 解决方法
这个想法是缩小您的图像,使其在较小的屏幕上看起来不错,而且您不必在内存中加载整个位图.
1)首先得到您将要显示的ImageView /屏幕的大小. 2)通过传递BitmapFactory.Options.inJustDecodeBounds来读取Bitmap的大小.这将为您提供Bitmap的大小,而不是加载整个位图. 3)获取采样大小.计算屏幕的高度和宽度与图像高度和宽度的比率.使用最小的一个,使最大的尺寸看起来不错. 4)最后使用下面的功能来获取不会吃掉你的记忆的下抽样图像. 2)(代码) 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)(代码) 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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读