2

CASE 1:

public class NewEntryActivity 
{

  public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.xxxx);


    //my_button_1 
        my_button_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final Dialog dialog = new Dialog(NewEntryActivity.this);

              dialog.setContentView(R.layout.abcabc);

              dialog.show(); 

              // On click of OK button in this dialog it will DISMISS the dialog. ( NOT remove )

            }
            }

    //my_button_2
        my_button_2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            final Dialog dialog = new Dialog(NewEntryActivity.this);

              dialog.setContentView(R.layout.xyzxyz);

              dialog.show(); 

              // On click of OK button in this dialog it will DISMISS the dialog. ( NOT remove )

            }


            }

    }
}

CASE 2:

public class NewEntryActivity 
{

  public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.xxxx);


        final Dialog dialog1 = new Dialog(NewEntryActivity.this);
        final Dialog dialog2 = new Dialog(NewEntryActivity.this);

        dialog1.setContentView(R.layout.abcabc);
        dialog2.setContentView(R.layout.xyzxyz);

        //my_button_1 
        my_button_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

              dialog1.show(); 

            }
            }

    }


}

In CASE 2, I have the dialogs already with content views set in it. Just showing/dismissing on click of buttons.

So My question is , Will the first case free some memory, OR will it have the same effect?

jpjacobs
  • 9,359
  • 36
  • 45
  • 3
    Whenever you create object at that memory occupy . case 1 is good !! – Nirav Ranpara Oct 25 '12 at 10:33
  • @NiravRanpara OK I got it. But my_button_1.setOnClickListener is not a method , and it is an event inside another method. So will that make difference? –  Oct 26 '12 at 06:34
  • 1
    Here my situation is, I have an app and I have around 100 Dialogs to show. Also my screen holds lot of other data at the same time. So my concern is to make sure about the memory usage –  Oct 26 '12 at 06:35

2 Answers2

1

Its merely depends on the your app consumes how much amount of memory particularly in this activity but the case 1 seems good.

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
1

Android’s process and memory management is a little unusual. Like Java and .NET, Android uses its own run time and virtual machine to manage application memory. Unlike either of these frameworks, the Android run time also manages the process lifetimes. Android ensures application responsiveness by stopping and killing processes as necessary to free resources for higher-priority applications.

Each Android application runs in a separate process within its own Dalvik instance, relinquishing all responsibility for memory and process management to the Android run time, which stops and kills processes as necessary to manage resources.

Dalvik and the Android run time sit on top of a Linux kernel that handles low-level hardware interaction including drivers and memory management, while a set of APIs provides access to all of the under- lying services, features, and hardware.

Dalvik Virtual Machine Dalvik is a register-based virtual machine that’s been optimized to ensure that a device can run multiple instances efficiently. It relies on the Linux kernel for threading and low-level memory management.


This thread will definitely help you clear your concepts about android memory management.
Here is another link to know more about android memory allocation.
Good Luck!

Community
  • 1
  • 1
Pratik
  • 831
  • 2
  • 8
  • 13