Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Thread Allocation - growing heap?
    text
    copied!<p>Hi everyone out there,</p> <p>i am developing an android application against API 7 at the moment in which i use an activity which need to be restarted. Lets say my activity looks like this:</p> <pre><code>public class AllocActivity extends Activity implements OnClickListener{ Button but; private Handler hand = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_alloc); but = (Button) findViewById(R.id.button); but.setText("RELOAD"); but.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0){ Intent intent = getIntent(); startActivity(intent); finish(); } }); } @Override protected void onDestroy(){ super.onDestroy(); System.gc(); } /****** THREADS AND RUNNABLES ******/ final Runnable fullAnim = new Thread(new Runnable(){ @Override public void run(){ try{ hand.post(anim1); Thread.sleep(2000); hand.post(anim2); Thread.sleep(1000); // and so on }catch(InterruptedException ie){ie.printStackTrace();} } }); final Runnable anim1 = new Runnable() { @Override public void run(){ // non-static method findViewById ImageView sky = (ImageView)findViewById(R.id.sky); } }; } </code></pre> <p>The problem is that the gc doesnt seem to free the fullAnim thread so that the heap is growing by ~100K at every restart - till it slows down and crashes. Declaring fullAnim as static does solve this problem - but as i use non static references this doesnt work out for me. </p> <p>So at this point i am kindof lost - and i hope u can advice me where to go next. Is there something i might be doing wrong or is there a tool i can use to manage threads to drop and free heap after restart.</p> <p>kindly regards</p> <p><strong>UPDATE</strong> </p> <p>thanks to everyone who answered - helped alot. using TimerTask did the trick in the end. i did the following change:</p> <pre><code>/****** THREADS AND RUNNABLES ******/ final TimerTask fullAnim = new TimerTask(){ @Override public void run(){ try{ hand.post(anim1); Thread.sleep(2000); hand.post(anim2); Thread.sleep(1000); // and so on }catch(InterruptedException ie){ie.printStackTrace();} } }; </code></pre> <p>as the activity was more than 6k loc long this was a pretty decent solution without facing bigger impacts. KUDOS!</p> <p>i dont use a Timer to shedule the task - dont know if its bad practice but the animation is called like this:</p> <pre><code>Thread t = new Thread(fullAnim); t.start(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload