0

My library code will notify byte array to UI,which in turn queued.Another thread will dequeue the byte array and using an instance of handler bundle the byte array and send message to update UI.

code snippet which use handler to update UI

public void run(){

        while(running){
            try {
                byte[] msg=(byte[]) queue.getMsg();
                Message message=new Message();
                Bundle bundle=new Bundle();
                bundle.putByteArray("img",msg);
                message.obj=bundle;
                handler.sendMessage(message);
                message=null;


            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

but the thing is i am getting outofmemory exception after 5 to 10 minutes. Using Eclipse MAT heap dumps shows 90% of heap is occupied by the more instance of android.os.Message.

Sureshkumar Menon
  • 1,165
  • 7
  • 27
  • 49

1 Answers1

5

You're creating a new Message each time. This is probably what's leading to your issue. What you should be doing is getting a Message from the Handler's Message pool using:

Message message = handler.obtainMessage();

This will keep you from allocating all that new memory.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
CaseyB
  • 24,780
  • 14
  • 77
  • 112