“new String()”导致GC_CONCURRENT语句?

时间:2012-04-06 20:09:33

标签: android

线

strPuffer = new String(buffer, 0, bytes, Charset.forName("UTF-8"));

会导致很多GC_CONCURRENT ......

while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    strPuffer = new String(buffer, 0, bytes, Charset.forName("UTF-8"));
                    curMsg.append(strPuffer);
                    int endIdx = curMsg.indexOf(end); 
                    if (endIdx != -1) { 
                        fullMessage = curMsg.substring(0, endIdx + end.length()-1); 
                        curMsg.delete(0, endIdx + end.length()); 
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, fullMessage)
                            .sendToTarget();}
                } catch (IOException e) {
                    //
                    connectionLost();
                    break;
                }

            }

我该怎样防止这种情况?

1 个答案:

答案 0 :(得分:1)

你在循环中创建了很多对象。看到这条消息只是意味着垃圾收集器通过保持内存释放来完成它的工作。 GC清除堆中未引用的对象。