Search code examples
javaandroidmemory-managementactivity-lifecycle

Optimize memory in android app whit onDestroy()


I want to optimize the memory in my android application. I implement a lifecycle method as onCreate() where i create object, onStart() where i set the listener whit the call of method, and i onDestroy() when i set null the variable and object and call manually the garbage collector whit System.gb(), this is a good practice for optimizie the memory of my MainActivity?

public class MainActivity extends AppCompatActivity {
private static final String TAG = Registrazione.class.getSimpleName();
private ProgressDialog pDialog;
private Button btnAccedi;
private EditText email, password;
private TextView linkPasswordDimenticata, linkRegistrazione;
private SessionManager session;
private SQLiteHandler db;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MyApplication myApplication = new MyApplication();
    myApplication.attachBaseContext(getApplicationContext());

    setContentView(R.layout.activity_main);

    btnAccedi = (Button) findViewById(R.id.button_login);
    email = (EditText) findViewById(R.id.email_login);
    password = (EditText) findViewById(R.id.password_login);
    linkRegistrazione = (TextView) findViewById(R.id.textview_registrazione);
    linkPasswordDimenticata = (TextView) findViewById(R.id.textview_password);

    // Progress dialog
    pDialog = new ProgressDialog(this);
    pDialog.setCancelable(false);

    // SQLite database handler
    db = new SQLiteHandler(getApplicationContext());

    // Session manager
    session = new SessionManager(getApplicationContext());

}

@Override
protected void onStart() {
    super.onStart();

    if (session.isLoggedIn()) {
        // User is already logged in. Take him to main activity
        Intent intent = new Intent(MainActivity.this, Tabbed.class);
        startActivity(intent);
        this.finish();
    }

    btnAccedi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (UtilityNetwork.isOnline())
                login();
            else {
                Toast.makeText(getBaseContext(), "Connessione assente", Toast.LENGTH_LONG).show();
            }
        }
    });

    linkRegistrazione.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivityRegistrazione();
        }
    });

    linkPasswordDimenticata.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivityPasswordDimenticata();
        }
    });
}

private void startActivityRegistrazione() {
    Intent intent = new Intent(getApplicationContext(), Registrazione.class);
    startActivity(intent);
    this.finish();
}

@Override
protected void onDestroy() {
    super.onDestroy();

    btnAccedi = null;
    email = null;
    password = null;
    linkRegistrazione = null;
    linkPasswordDimenticata = null;
    pDialog = null;
    db = null;
    System.gc();
}

Solution

  • You don't need to nullify your fields in onDestroy(). System.gc() is also redundant. Java will handle all the things for you. Because Activity will be destroyed, no one will be referencing these fields and all objects inside of the Activity will be garbage-collected automatically in your case.

    The only thing you need to do is to call pDialog.dismiss() in onDestroy(), because this dialog will be referencing Activity after destroy causing a memory leak.