问题描述
我想了解内存泄漏的概念。我想这code和尝试一些方法我是从相关的职位找到,但我能不能解决问题。需要认识上的帮助,其中的内存泄漏在这个code发生。我的应用程序只有2个活动
//第一项活动
包com.pace.mat;进口android.app.Activity;
进口android.app.Dialog;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.Window;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.ImageView;公共类MATDemoActivity扩展活动实现OnClickListener { 私人对话dialog1;
私人按钮btnSubmit按钮;
私人语境myClassContext;
私人ImageView的RedImage,BlueImage,黄色,橙色,绿色; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
myClassContext =这一点; RedImage =(ImageView的)findViewById(R.id.Red);
BlueImage =(ImageView的)findViewById(R.id.Blue);
黄色=(ImageView的)findViewById(R.id.Yellow);
橙色=(ImageView的)findViewById(R.id.Orange);
绿色=(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按钮=(按钮)findViewById(R.id.btnSubmitAtFirst);
btnSubmit.setOnClickListener(本); } 公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
如果(arg0都==(查看)btnSubmit按钮)
{
dialog1 =新的对话框(myClassContext);
窗口窗口= dialog1.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
window.requestFeature(window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.progress_indicator);
dialog1.show(); //做一个网络密集型任务 如果(dialog1!= NULL)
{
dialog1 = NULL;
myClassContext = NULL;
窗口=无效;
} 意图I =新意图(MATDemoActivity.this,SecondActivity.class);
startActivity(ⅰ);
}
} @覆盖
公共无效的onStop(){
super.onStop();
myClassContext = NULL;
dialog1 = NULL;
RedImage = NULL;
BlueImage = NULL;
黄色= NULL;
橙色= NULL; 绿色= NULL;
this.finish();
} @覆盖
公共无效的onPause(){
super.onPause();
myClassContext = NULL;
dialog1 = NULL;
RedImage = NULL;
BlueImage = NULL;
黄色= NULL; 橙色= NULL;
绿色= NULL;
this.finish();
} @覆盖
公共无效的onDestroy(){
super.onDestroy();
myClassContext = NULL;
dialog1 = NULL;
RedImage = NULL;
BlueImage = NULL; 黄色= NULL;
橙色= NULL;
绿色= NULL;
this.finish();
}}
//次活动
包com.pace.mat;进口android.app.Activity;
进口android.app.Dialog;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.Window;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.ImageView;公共类SecondActivity扩展活动实现OnClickListener { 私人对话dialog1;
私人按钮btnSubmit按钮;
私人语境myClassContext1;
私人ImageView的RedImage,BlueImage,黄色,橙色,绿色; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.secondactivity); myClassContext1 =这一点; RedImage =(ImageView的)findViewById(R.id.Red);
BlueImage =(ImageView的)findViewById(R.id.Blue);
黄色=(ImageView的)findViewById(R.id.Yellow);
橙色=(ImageView的)findViewById(R.id.Orange);
绿色=(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按钮=(按钮)findViewById(R.id.btnSubmitAtFirst);
btnSubmit.setOnClickListener(本);
} 公共无效的onClick(视图v){
// TODO自动生成方法存根
如果(V ==(查看)btnSubmit按钮)
{
dialog1 =新的对话框(myClassContext1);
窗口窗口= dialog1.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
window.requestFeature(window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.progress_indicator);
dialog1.show(); //上传图像到网络 如果(dialog1!= NULL)
{
dialog1 = NULL;
myClassContext1 = NULL;
窗口=无效;
} 意图I =新意图(这一点,MATDemoActivity.class);
startActivity(ⅰ);
}
} @覆盖
公共无效的onStop(){
super.onStop(); this.finish();
} @覆盖
公共无效的onPause(){
super.onPause(); this.finish();
} @覆盖
公共无效的onDestroy(){
super.onDestroy();
myClassContext1 = NULL;
dialog1 = NULL;
RedImage = NULL;
BlueImage = NULL; 黄色= NULL;
橙色= NULL;
绿色= NULL;
this.finish();
}}
// LOG CAT数据时,我谨从第一个到第二个活动
12月5日至一十七日:12:43.323:E /窗口管理器(2264):活动com.pace.mat.SecondActivity渗漏窗口com.android.internal.policy.impl.PhoneWindow $ @ DecorView这44f63b88最初加入这里
12月5日至17日:12:43.323:E /窗口管理器(2264):android.view.WindowLeaked:活动com.pace.mat.SecondActivity渗漏窗口com.android.internal.policy.impl.PhoneWindow$DecorView@44f63b88这是原本在这里添加
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.view.ViewRoot<&初始化GT;(ViewRoot.java:247)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.view.Window $ LocalWindowManager.addView(Window.java:424)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.app.Dialog.show(Dialog.java:241)
12月5日至17日:12:43.323:E /窗口管理器(2264):在com.pace.mat.SecondActivity.onClick(SecondActivity.java:54)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.view.View.performClick(View.java:2408)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.view.View $ PerformClick.run(View.java:8816)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.os.Handler.handleCallback(Handler.java:587)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.os.Handler.dispatchMessage(Handler.java:92)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.os.Looper.loop(Looper.java:123)
12月5日至17日:12:43.323:E /窗口管理器(2264):在android.app.ActivityThread.main(ActivityThread.java:4627)
12月5日至17日:12:43.323:E /窗口管理器(2264):在java.lang.reflect.Method.invokeNative(本机方法)
12月5日至17日:12:43.323:E /窗口管理器(2264):在java.lang.reflect.Method.invoke(Method.java:521)
12月5日至17日:12:43.323:E /窗口管理器(2264):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
12月5日至17日:12:43.323:E /窗口管理器(2264):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12月5日至17日:12:43.323:E /窗口管理器(2264):在dalvik.system.NativeStart.main(本机方法)
您code似乎没有有任何内存泄漏。而大部分时间内存泄漏是由于为保持长寿命参照的。
下面是罗曼盖伊一篇好文章,这将有助于您了解详细的内存泄漏。
。
希望这会有所帮助。
编辑:按你更新你的问题
在看你的日志后,其似乎是有没有内存泄漏的窗口泄露例外。
关闭或解散你的对话框中你完成你的活动之前。这将解决您的问题。
I am trying to understand the concept of memory leaks. I tried this code and tried few ways i found from related posts but i could not solve the issue. Need help on understanding where memory leak happens in this code. My application has only 2 activities
//First Activity
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();
}
}
// Second Activity
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 DATA WHEN I MOVE FROM FIRST TO SECOND ACTIVITY
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)
your code does not seem to have any memory leak. and most of the times memory leaks are due to keeping a long-lived reference to a Context.
Here is a good article by Romain Guy which will help you to understand Memory leaks in detail.check it out.
Hope it will help.
EDIT: as per you updated your question
After looking at your log its seems that there is no memory leak its window leaked exception. close or dismiss your dialog before you finish your activity. this will resolve your problem.
这篇关于Android的Memoryleak在code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!