Android的线程分配 - 增长的堆? [英] Android Thread Allocation - growing heap?

查看:171
本文介绍了Android的线程分配 - 增长的堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好摆在那里,

我正在开发在我使用的需要重新启动活动的瞬间对API 7 Android应用程序。比方说我的活动是这样的:

 公共类AllocActivity扩展活动实现OnClickListener {    按钮,但;
    私人处理器手=新的处理程序();    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        的setContentView(R.layout.activity_alloc);        但是=(按钮)findViewById(R.id.button);
        but.setText(刷新);
        but.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(查看为arg0){
                意向意图= getIntent();
                startActivity(意向);
                完();
            }
        });
    }    @覆盖
    保护无效的onDestroy(){
        super.onDestroy();
        System.gc()的;
    }    / ******螺纹和可运行****** /    最终的Runnable fullAnim =新主题(新的Runnable(){
        @覆盖
        公共无效的run(){
            尝试{
                hand.post(anim1);
                视频下载(2000);
                hand.post(anim2);
                视频下载(1000);
                // 等等
            }赶上(InterruptedException的IE){ie.printStackTrace();}
        }
    });    最终的Runnable anim1 =新的Runnable(){
        @覆盖
        公共无效的run(){
            //非静态方法findViewById
            ImageView的天空=(ImageView的)findViewById(R.id.sky);
        }
    };
}

的问题是,似乎不释放fullAnim螺纹,使得堆被〜100K在每次重启生长GC - 直到它减慢和崩溃。声明fullAnim静态不解决这个问题 - 但是,当我使用非静态引用这并不工作,为我。

所以在这一点上,我很kindof失去了 - 我希望u能建议我下一步去哪里。是不是我可能是做错了还是有一个工具,我可以用它来管理线程删除并重新启动后,自由堆。

敬请认为

更新

感谢大家谁回答 - 帮助了很多。使用的TimerTask做最后的伎俩。我做了以下变化:

  / ******线程,可运行****** /最终的TimerTask fullAnim =新的TimerTask(){
    @覆盖
    公共无效的run(){
        尝试{
            hand.post(anim1);
            视频下载(2000);
            hand.post(anim2);
            视频下载(1000);
            // 等等
        }赶上(InterruptedException的IE){ie.printStackTrace();}
    }
};

作为活动超过6K禄久,这是没有面临更大影响的pretty体面的解决办法。奖励!

我不使用Timer来shedule任务 - 不知道它不好的做法,但
动画被称为是这样的:

 线程t =新主题(fullAnim);
t.start();


解决方案

由于最后一个变量具有较低的优先级GC。所以,你需要明确释放的onPause(该runneable对象)的方法,因为有不ensurence onDestory()将调用完成()调用后立即生效。

  @覆盖
    保护无效的onPause(){
        super.onPause();
        //删除计时器停止动画
       如果(T!= NULL){
        t.cancel();
       }        System.gc()的;
    }

更新

使用定时器来实现这一目标。

 布尔isFirstAnim = TRUE;
TIMER T =新的Timer();
        t.schedule(新的TimerTask(){            @覆盖
            公共无效的run(){
                          如果(isFirstAnim){
                              //每播放第一个动画
                          }其他{
                              //每播放秒的动画
                          }
            }
        },0,3000);

Hi everyone out there,

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:

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);
        }
    };
}

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.

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.

kindly regards

UPDATE

thanks to everyone who answered - helped alot. using TimerTask did the trick in the end. i did the following change:

/****** 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();}
    }
};

as the activity was more than 6k loc long this was a pretty decent solution without facing bigger impacts. KUDOS!

i dont use a Timer to shedule the task - dont know if its bad practice but the animation is called like this:

Thread t = new Thread(fullAnim);
t.start();

解决方案

Because final variable have low priority for GC. So you need to explicitly release the runneable objects in onPause() method because there is not ensurence onDestory() will call immediate after finish() call .

@Override
    protected void onPause(){
        super.onPause();
        //cancel timer to stop animations
       if(t!=null){
        t.cancel();
       }

        System.gc();
    }

UPDATE

use timer to achieve this

boolean isFirstAnim=true;
Timer t = new Timer();
        t.schedule(new TimerTask() {

            @Override
            public void run() {
                          if(isFirstAnim){
                              // play your first animation at every
                          }else{
                              // play your second animation at every
                          }                 
            }
        }, 0, 3000);

这篇关于Android的线程分配 - 增长的堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆