Android的Memoryleak代码(Android Memoryleak in code)

2019-09-17 07:14发布

我想了解内存泄漏的概念。 我想这个代码,并试图几个方面我从相关的帖子发现,但我解决不了的问题。 需要认识上的帮助,其中内存泄漏在此代码发生。 我的应用程序只有2个活动

//第一项活动

package com.pace.mat;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MATDemoActivity extends Activity implements OnClickListener {

    private Dialog dialog1;
    private Button btnSubmit;
    private Context myClassContext;
    private ImageView RedImage,BlueImage,Yellow,Orange,Green;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myClassContext = this;

        RedImage = (ImageView) findViewById(R.id.Red);
        BlueImage = (ImageView) findViewById(R.id.Blue);
        Yellow = (ImageView) findViewById(R.id.Yellow);
        Orange = (ImageView) findViewById(R.id.Orange);
        Green = (ImageView) findViewById(R.id.Green);

       RedImage.setImageResource(R.drawable.red);
       BlueImage.setImageResource(R.drawable.blue);
       Yellow.setImageResource(R.drawable.yellow);
       Orange.setImageResource(R.drawable.orange);
       Green.setImageResource(R.drawable.green);

        btnSubmit = (Button)findViewById(R.id.btnSubmitAtFirst);
        btnSubmit.setOnClickListener(this);

    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(arg0 == (View)btnSubmit)
        {
            dialog1=new Dialog(myClassContext); 
            Window window = dialog1.getWindow(); 
            window.setBackgroundDrawableResource(android.R.color.transparent); 
            window.requestFeature(window.FEATURE_NO_TITLE);                     
            dialog1.setContentView(R.layout.progress_indicator); 
            dialog1.show();     

            // Doing a network intensive task

            if(dialog1 !=null)
            {
                dialog1 = null;
                myClassContext =  null;
                window = null;
            }

            Intent i = new Intent(MATDemoActivity.this,SecondActivity.class);
            startActivity(i);
        }
    }

    @Override
    public void onStop() {    
        super.onStop();  
        myClassContext =  null;
        dialog1 = null;
        RedImage = null;
        BlueImage = null;
        Yellow = null;
        Orange = null;

        Green=null;
        this.finish();   
    }       

    @Override
    public void onPause() {
        super.onPause();
         myClassContext =  null;
            dialog1 = null;
            RedImage = null;
            BlueImage = null;
            Yellow = null;

            Orange = null;
            Green=null;
         this.finish(); 
    }           

    @Override
    public void onDestroy() {
        super.onDestroy();
         myClassContext =  null;
            dialog1 = null;
            RedImage = null;
            BlueImage = null;

            Yellow = null;
            Orange = null;
            Green=null;
        this.finish();
    }

}

//第二项活动

package com.pace.mat;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class SecondActivity extends Activity implements OnClickListener {

    private Dialog dialog1;
    private Button btnSubmit;
    private Context myClassContext1;
    private ImageView RedImage,BlueImage,Yellow,Orange,Green;

     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);

        myClassContext1 = this;

        RedImage = (ImageView) findViewById(R.id.Red);
        BlueImage = (ImageView) findViewById(R.id.Blue);
        Yellow = (ImageView) findViewById(R.id.Yellow);
        Orange = (ImageView) findViewById(R.id.Orange);
        Green = (ImageView) findViewById(R.id.Green);

       RedImage.setImageResource(R.drawable.red);
       BlueImage.setImageResource(R.drawable.blue);
       Yellow.setImageResource(R.drawable.yellow);
       Orange.setImageResource(R.drawable.orange);
       Green.setImageResource(R.drawable.green);

        btnSubmit = (Button)findViewById(R.id.btnSubmitAtFirst);
        btnSubmit.setOnClickListener(this);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v == (View)btnSubmit)
        {
            dialog1=new Dialog(myClassContext1); 
            Window window = dialog1.getWindow(); 
            window.setBackgroundDrawableResource(android.R.color.transparent); 
            window.requestFeature(window.FEATURE_NO_TITLE);                     
            dialog1.setContentView(R.layout.progress_indicator); 
            dialog1.show();     

            // Uploading an Image to network 

            if(dialog1 !=null)
            {
                dialog1 = null;
                myClassContext1 =  null;
                window = null;
            }

            Intent i = new Intent(this,MATDemoActivity.class);
            startActivity(i);
        }
    }

    @Override
    public void onStop() {    
        super.onStop();  

        this.finish();   
    }       

    @Override
    public void onPause() {
        super.onPause();

         this.finish(); 
    }           

    @Override
    public void onDestroy() {
        super.onDestroy();
         myClassContext1 =  null;
            dialog1 = null;
            RedImage = null;
            BlueImage = null;

            Yellow = null;
            Orange = null;
            Green=null;
        this.finish();
    }

}

// LOG CAT数据时,我谨从第一个到第二个活动

05-17 12:12:43.323: E/WindowManager(2264): Activity com.pace.mat.SecondActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f63b88 that was originally added here
05-17 12:12:43.323: E/WindowManager(2264): android.view.WindowLeaked: Activity com.pace.mat.SecondActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f63b88 that was originally added here
05-17 12:12:43.323: E/WindowManager(2264):  at android.view.ViewRoot.<init>(ViewRoot.java:247)
05-17 12:12:43.323: E/WindowManager(2264):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
05-17 12:12:43.323: E/WindowManager(2264):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
05-17 12:12:43.323: E/WindowManager(2264):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-17 12:12:43.323: E/WindowManager(2264):  at android.app.Dialog.show(Dialog.java:241)
05-17 12:12:43.323: E/WindowManager(2264):  at com.pace.mat.SecondActivity.onClick(SecondActivity.java:54)
05-17 12:12:43.323: E/WindowManager(2264):  at android.view.View.performClick(View.java:2408)
05-17 12:12:43.323: E/WindowManager(2264):  at android.view.View$PerformClick.run(View.java:8816)
05-17 12:12:43.323: E/WindowManager(2264):  at android.os.Handler.handleCallback(Handler.java:587)
05-17 12:12:43.323: E/WindowManager(2264):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 12:12:43.323: E/WindowManager(2264):  at android.os.Looper.loop(Looper.java:123)
05-17 12:12:43.323: E/WindowManager(2264):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-17 12:12:43.323: E/WindowManager(2264):  at java.lang.reflect.Method.invokeNative(Native Method)
05-17 12:12:43.323: E/WindowManager(2264):  at java.lang.reflect.Method.invoke(Method.java:521)
05-17 12:12:43.323: E/WindowManager(2264):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-17 12:12:43.323: E/WindowManager(2264):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-17 12:12:43.323: E/WindowManager(2264):  at dalvik.system.NativeStart.main(Native Method)

Answer 1:

你的代码似乎没有任何内存泄漏。 和大部分的时间内存泄漏是由于保持长期参照上下文 。

下面是罗曼盖伊一篇好文章,这将有助于您了解详细的内存泄漏。 检查出来 。

希望这将有助于。

编辑:根据你更新你的问题

看你的日志后,它似乎是有没有内存泄漏的窗口泄露例外。 关闭或解散你的对话框中,您完成您的活动之前。 这将解决您的问题。



Answer 2:

看起来像你从另一个调用一个活动重复,这就像你正在循环进入活动,并切换它们。

MATDemoActivity电话

Intent i = new Intent(MATDemoActivity.this,SecondActivity.class); 
            startActivity(i);

然后从SecondActivity您所呼叫

Intent i = new Intent(this,MATDemoActivity.class);   
        startActivity(i); 

还你重新创建的所有对象的每一项活动,而不是两个活动之间共享。

尝试解决这些问题,看看是否可行。



Answer 3:

从日志,我认为这个问题的原因是您展示一个对话框,但没有关闭它,那么你指定对话框为空,并开始另一项活动。

if(dialog1 !=null)
{
    dialog1 = null;
    myClassContext1 =  null;
    window = null;
}

因此,活动将泄漏的对话框(对话框窗口)。 无论如何,如果你不需要一个对话框显示,你应该关闭它。



文章来源: Android Memoryleak in code