使用 onDestroy() 优化 android 应用程序中的内存
Optimize memory in android app whit onDestroy()
我想优化我的 android 应用程序中的内存。我将生命周期方法实现为 onCreate() ,在其中创建对象,在 onStart() 中设置侦听器并调用方法,在 onDestroy() 中将变量和对象设置为 null 并手动调用垃圾收集器 System.gb(),这是优化我的 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();
}
在Java中,如果需要内存,垃圾收集器会自动运行。
调用 System.gc()
会立即启动垃圾处理。它检查未引用的对象并销毁它们。
除非对象是用 C++ 本机实现的,例如 Bitmap
对象,否则您无需担心垃圾回收。对于用 C++ 实现的对象,你需要调用相应的析构函数,例如 Bitmap
class.
中的 recycle()
方法
您不需要取消 onDestroy()
中的字段。 System.gc()
也是多余的。
Java 会为您处理所有事情。
因为 Activity 将被销毁,所以没有人会引用这些字段,并且 Activity 内的所有对象都将在您的情况下自动被垃圾收集。
您唯一需要做的就是在 onDestroy()
中调用 pDialog.dismiss()
,因为此对话框将在销毁后引用 Activity,从而导致内存泄漏。
我想优化我的 android 应用程序中的内存。我将生命周期方法实现为 onCreate() ,在其中创建对象,在 onStart() 中设置侦听器并调用方法,在 onDestroy() 中将变量和对象设置为 null 并手动调用垃圾收集器 System.gb(),这是优化我的 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();
}
在Java中,如果需要内存,垃圾收集器会自动运行。
调用 System.gc()
会立即启动垃圾处理。它检查未引用的对象并销毁它们。
除非对象是用 C++ 本机实现的,例如 Bitmap
对象,否则您无需担心垃圾回收。对于用 C++ 实现的对象,你需要调用相应的析构函数,例如 Bitmap
class.
recycle()
方法
您不需要取消 onDestroy()
中的字段。 System.gc()
也是多余的。
Java 会为您处理所有事情。
因为 Activity 将被销毁,所以没有人会引用这些字段,并且 Activity 内的所有对象都将在您的情况下自动被垃圾收集。
您唯一需要做的就是在 onDestroy()
中调用 pDialog.dismiss()
,因为此对话框将在销毁后引用 Activity,从而导致内存泄漏。