问题
I have worked on the application which I present in the RAM after I am out of my application. Also the size in RAM is increases every time when I play the application. I don't know why it is happening. Please suggest me something.
Also I am using timer in my application for 15 sec and after completing the time I show I Dialog that Your time is over. But some times that dialog appear 3-4 times on one time finish of time. This is very embarrassing for my application. Is this thing is due to the app is in background or anything wrong with my timer. Code of timer is below. Globally declare the
MyCount counter = new MyCount(time, 1000);
and the timer code is:
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
//call when time is finish
public void onFinish() {
// TODO Auto-generated method stub
AlertDialog.Builder alertbox = new AlertDialog.Builder(game.this);
alertbox.setMessage("Oops, your time is over");
alertbox.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
// Click listener on the neutral button of alert box
public void onClick(DialogInterface arg0, int arg1) {
quesCount++;
for(int i=0;i<4;i++){
btn[i].setVisibility(View.VISIBLE);
}
rndom_no=randomno();
showDataOverControls(rndom_no);
counter.start();
}
});
alertbox.show();
}
@Override
public void onTick(long millisUntilFinished) {
time1 = (TextView) findViewById
(R.id.time);
time1.setText(""+millisUntilFinished / 1000);
}
}
The for loop inside the application change the visibility of the buttons, rndom_no=randomno(); this function generate the random numbers, showDataOverControls(rndom_no); it fetch the data from the xml file by xml parsing. Please suggest if I am doing something wrong.
回答1:
Your app stays in ram as long as android wants it.. You don't have control to it.
Try reusing the activities by configuring the launchMode
property to stop it from increasing.
Also, use some flag to check the dialog box display and don't display if it's already displayed
回答2:
You app stays in the phone ram to allow the user to return to the app without much loading time. The Android memory system works in a way that as long as memory is not needed the last used apps stay in the RAM. Don't worry about that. If another app needs this memory and your app has no foreground components like activities your whole app will closed down step by step until the phone has enough memory again. The only way to block memory would it be to create a very badly designed service. And even services are shut down if foreground services need the memory.
来源:https://stackoverflow.com/questions/10585310/the-application-still-in-ram-after-using-it-and-take-more-2-space-each-time-i-pl