How to release or clear the value of variable or object?
I have an application in which I have used some variables and I want to release or free the memory space used by them after using them or on application close. Is there any process of doing this?
I have used:
int x = Integer.parseInt(edt.getText().t开发者_StackOverflow社区oString()) * Integer.parseInt(edt1.getText().toString());
String z = String.valueOf(x);
edt - Stands For EditText which I have been used for entering the value.
After entering the value into the EditText, whenever I'll switch to my next activity page and then get back on my previous page the value of EditText is still available on it. But I want to clear the value of the variable.
How can I achieve this?
As you can see on the Activity lifecycle graph when a new activity is started the old one is merely paused and all values are retained.
It is not destroyed if there's no immediate need for memory for the rest of the system.
I assume that edt
is in the layout taken via findViewById
and not created by hand (not apparent from the code). So if you want to clear value of that EditText when switching back to old activity you need to do it by hand either in onPause
or onResume
.
On application close these will be released by Java VM. Keep in mind that your app might not be killed immediately after you leave last activity.
I had answered a nearly similar question here : Android proper clean up/disposing
Basically what you need to do is set objects to null so that the garbage collector can clear it up when it runs.
精彩评论