English 中文(简体)
4. 发现记忆泄露
原标题:Android trouble finding memory leak

我对我的申请感到困惑不解。 我相当肯定地把记忆泄露到某个地方,我认为我已经把它缩小到某种活动上,并且认为这种泄漏与AdMob有关。

说明 我所看到的是,如果我提出自己的建议,第一项活动涉及大约3Mb(显示形象)。 第二项活动装满后,第一只草原被销毁,总肥皂使用量增至7.8Mb。 一旦AdMob ad在第二次活动中装满,肥皂的大小就达到12.5兆瓦。

如果我回头来从事第一项活动,那么,第2项活动叫)。 然而,我的申请中使用的记忆量根本不会减少。 即便是我明确呼吁制度。 我可能错了,但应该把肥皂的数量回3Mb?

令我感到困惑的是,如果我活着,再次开始第二次活动,那么我回头和第四时,肥皂的面积只能增加500千卡。 如同在第二次活动中,有些事情正在活下来并重新使用,尽管这些活动已经销毁。

我在这里简化了我的法典,看看一个人是否能够告诉我我我我什么是错的。 我也利用MAAT对垃圾堆放档案进行了研究,但我对我看什么和没有发现多少用途感到不满意。

<>My first (default) activity

public class FirstActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        setContentView(R.layout.main);
     }


     public void startSelectionPage(View v){
         Intent intent = new Intent(FirstActivity.this, ImageSelectActivity.class);
         startActivity(intent);
     }
}

<>strong>main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/startpage" >

        <ImageButton
          android:id="@+id/pb"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/playbutton"
          android:onClick="startSelectionPage"          
     />
</RelativeLayout>

<>亮度>

public class ImageSelectActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

         setContentView(R.layout.selectionpage);    

         ((Gallery) findViewById(R.id.gallery))
         .setAdapter(new ImageAdapter(this.getApplicationContext(), 150));     
    }

     @Override
     public void onDestroy() {
        AdView ad = (AdView) findViewById(R.id.adView);
        ad.destroy();
        super.onDestroy();
     }
 }

 class ImageAdapter extends BaseAdapter {
    private Context myContext;
    private int imageBackground;
    private int galleryHeight;

    private int[] myImageIds = {
                R.drawable.canyonthumb,
                R.drawable.yosemitethumb,
                R.drawable.flowerthumb,
                R.drawable.squirrelthumb

    };

    public ImageAdapter(Context c, int galleryHeight) { 
        this.myContext = c;
        TypedArray ta = c.obtainStyledAttributes(R.styleable.GalleryTheme);
        imageBackground = ta.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 1);
        ta.recycle();
        this.galleryHeight = galleryHeight;
    }

    public int getCount() { return this.myImageIds.length; }
    public Object getItem(int position) { return position; }
    public long getItemId(int position) { return position; }

    public View getView(int position, View convertView, ViewGroup parent) {     
        ImageView imageView = new ImageView(myContext);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new Gallery.LayoutParams(galleryHeight,galleryHeight));
        imageView.setBackgroundResource(imageBackground);
        imageView.setImageResource(this.myImageIds[position]);
        return imageView;
    }
}

selectionpage.xml

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/chooseanimage"/>

        <RelativeLayout
            android:id="@+id/rlayout1"
              android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >

                    <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="60dp"
                         ads:adUnitId="----"
                         ads:adSize="IAB_BANNER"
                         ads:testDevices="TEST_EMULATOR,---"
                         ads:loadAdOnCreate="true"
                         />

                    <Gallery
                        android:id="@+id/gallery"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_below="@id/imageView1"
                        android:layout_above="@+id/adView"
                        />

        </RelativeLayout>   
        </LinearLayout>        
最佳回答

我认为,我从未有过记忆泄露。 这个问题与GC有关,后者似乎允许物体留在记忆中的时间比我预期的要长得多。 如果我尝试16Mb 支持者的守则,GC就会更积极地开展工作。

我并不期望的是AdMob的高记忆使用。 在我的活动中,加上“反照”,导致使用大约8元的肥皂。

问题回答

https://stackoverflow.com/questions/5001682/android-memory- management-in-active-liferix” 答案不是你所要回答的问题,而是我相信你正在寻求的信息。

因此,对记忆、垃圾收集器和活动周期Im之间的关系作长期解释,然后将你提一下上述问题。

我不敢肯定,但这里我想的是:

在<代码>ImageSelectActative中,你创建了一个新条目:。 它是一种匿名物体,因为你在<条码>上使用<>新

<代码>ImageAdapter的物体,只要你活着,就不会销毁。 您可能会把您的活动的改编者定在onDestroy(上。

一个非常好的博客员额是关于贵专题的: 避免记忆泄露

最后第三点:

Avoid non-static inner classes in an activity if you don t control their life cycle, use a static inner class and make a weak reference to the activity inside. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance

你们应该做些什么。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签