Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7953247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:06:20+00:00 2026-06-04T03:06:20+00:00

I am trying to understand the concept of memory leaks. I tried this code

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:06:22+00:00Added an answer on June 4, 2026 at 3:06 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand this concept but I don't get it yet. I'm new
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
I am trying to understand the concept of texture mapping. I tried to map
I am trying to understand the concept of synchronizing on an object. Using this
Trying to use PureMVC framework and understand MVC concept. Don't understand this sentence: The
I'm really trying hard to understand this new concept after working so long with
I am trying to understand the concept of Beat Detektion and I found that
This is more of a generic question. But I am trying to understand a
The question might sound vague but I'm trying to understand the general concept of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.