2012年12月27日 星期四

Android - Memory leak note

1. Used the StringBuilder to avoid the memory leak:

Error :
for (int i=1; i<65535 i="i" p="p">{
    String aaa = aaa + "test sting"; // this will have memory leak
    String aaa = null;
}

Solution :

for (int i=1; i<65535 i="i" p="p">{

    StringBuilder sb;
    sb.append("test sting");
    String aaa = sb.ToString();
    sb.setLength(0);
}

2. Recycle the message

a.removeMessages(0);
Message msg = a.obtainMessage();
a.sendMessage(msg);

沒有留言: