2012-04-06 58 views
0

線路「new String()」導致GC_CONCURRENT語句?

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

      } 

我如何阻止呢?

+0

爲什麼,你的問題是什麼? – 2012-04-06 20:16:04

+0

是不是一個好的跡象,你的垃圾收集器工作正常? :) – waqaslam 2012-04-06 20:20:44

+0

如果你對GC消息不利,你總是可以使用ByteBuffer和CharsetDecoder來做同樣的事情(在更多* loc *中)作爲'new String(..)'。 – Jens 2012-04-06 20:25:19

回答

1

您正在循環中創建很多對象。看到這個消息只是意味着垃圾收集器正在通過保持內存釋放來完成它的工作。 GC清除堆中未引用的對象。