alex cz alex cz - 1 month ago 20
Android Question

Reduce memory setting image to imageview

I am showing a series of questions to the user. In each question I set drawables to 6 imageviews. The only problem is the ram usage goes up about 60MB ever time I go to the next screen.

I dont understand why this is happening because I am just replacing the picture with a different one? After about 5 screens the app crashing because of failed memory allocation. How would I release this memory of the screen before it so It doesnt build up.

Thanks in advance.

public class Test extends ActionBarActivity {

ImageButton a1;
ImageButton a2;
ImageButton a3;
ImageButton a4;
Intent result;
String correctOrWrong = "";
int question;
int amountOfC = 0;
int numCorrect;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

getSupportActionBar().hide();

ImageView qMain = (ImageView) findViewById(R.id.qMain);
a1 = (ImageButton) findViewById(R.id.a1);
a2 = (ImageButton) findViewById(R.id.a2);
a3 = (ImageButton) findViewById(R.id.a3);
a4 = (ImageButton) findViewById(R.id.a4);
ImageView qTitle = (ImageView) findViewById(R.id.qTitle);


a1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextQ(1);
}
});

a2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextQ(2);
}
});

a3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextQ(3);
}
});

a4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextQ(4);
}
});

Intent qGet = getIntent();
question = qGet.getIntExtra("QNUMBER", 1);
numCorrect = qGet.getIntExtra("NUMCORRECT", 1);

Log.d("lel", String.valueOf(question));

if (question == 1) {
qMain.setBackgroundResource(R.drawable.q1);
a1.setBackgroundResource(R.drawable.a13);
a2.setBackgroundResource(R.drawable.a12);
a3.setBackgroundResource(R.drawable.a11);
a4.setBackgroundResource(R.drawable.ca1);
qTitle.setBackgroundResource(R.drawable.q1t);

} else if (question == 2) {
qMain.setBackgroundResource(R.drawable.q2);
a1.setBackgroundResource(R.drawable.q23);
a2.setBackgroundResource(R.drawable.q2a);
a3.setBackgroundResource(R.drawable.q22);
a4.setBackgroundResource(R.drawable.q21);
qTitle.setBackgroundResource(R.drawable.q2t);
} else if (question == 3) {
qMain.setBackgroundResource(R.drawable.q3);
a1.setBackgroundResource(R.drawable.q31);
a2.setBackgroundResource(R.drawable.q32);
a3.setBackgroundResource(R.drawable.q33);
a4.setBackgroundResource(R.drawable.q3a);
qTitle.setBackgroundResource(R.drawable.q3t);
} else if (question == 4) {
qMain.setBackgroundResource(R.drawable.q4);
a1.setBackgroundResource(R.drawable.q4a);
a2.setBackgroundResource(R.drawable.q43);
a3.setBackgroundResource(R.drawable.q42);
a4.setBackgroundResource(R.drawable.q41);
qTitle.setBackgroundResource(R.drawable.q4t);
} else if (question == 5) {
qMain.setBackgroundResource(R.drawable.q5);
a1.setBackgroundResource(R.drawable.q53);
a2.setBackgroundResource(R.drawable.q5a);
a3.setBackgroundResource(R.drawable.q52);
a4.setBackgroundResource(R.drawable.q51);
qTitle.setBackgroundResource(R.drawable.q5t);

Answer

don't handle image memory/caching yourself, instead use a library such as glide - https://github.com/bumptech/glide

or

try clearing the imageview onDestroy() of the acitvity

a1.setImageDrawable(null);
a2.setImageDrawable(null);
a3.setImageDrawable(null);
a4.setImageDrawable(null);