encodeData 在低版本手機會出現OOM
KingsleyWu opened this issue · 2 comments
Device Version: android 7.0
Device Model:SM-G9250
Stacktrace:java.lang.OutOfMemoryError: Failed to allocate a 197416 byte allocation with 182464 free bytes and 178KB until OOM
at libcore.util.CharsetUtils.toUtf8Bytes(Native Method)at java.lang.String.getBytes(String.java:879)
at java.lang.String.getBytes(String.java:851)
at com.sensorsdata.analytics.android.sdk.AnalyticsMessages.encodeData()
at com.sensorsdata.analytics.android.sdk.AnalyticsMessages.sendData()
at com.sensorsdata.analytics.android.sdk.AnalyticsMessages.access$000()
at com.sensorsdata.analytics.android.sdk.AnalyticsMessages$Worker$AnalyticsMessageHandler.handleMessage()
at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
`
// AnalyticsMessages.java
private String encodeData(final String rawMessage) throws InvalidDataException {
GZIPOutputStream gos = null;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream(rawMessage.getBytes(CHARSET_UTF8).length);
gos = new GZIPOutputStream(os);
gos.write(rawMessage.getBytes(CHARSET_UTF8));
gos.close();
byte[] compressed = os.toByteArray();
os.close();
return new String(Base64Coder.encode(compressed));
} catch (IOException exception) {
// 格式错误,直接将数据删除
throw new InvalidDataException(exception);
} finally {
if (gos != null) {
try {
gos.close();
} catch (IOException e) {
// ignore
}
}
}
}
`
另外建議 rawMessage.getBytes(CHARSET_UTF8) 抽成一個變量,這個方法裡重複調用了兩次,本來不需要多申請內存的,現在申請了兩份。
@KingsleyWu 我们内部看一下,进行优化
@KingsleyWu v6.5.0 版本已优化