Skip to content

Commit da8f60d

Browse files
author
qiuyucheng
committed
fix(Point): prevent ThreadLocal memory leak in StringBuilder (#1019)
Introduced a maximum capacity threshold (64KB) for the cached StringBuilder. If the capacity exceeds this threshold, the StringBuilder is discarded and replaced to prevent Old Gen exhaustion in long-lived threads.
1 parent 5cb61c7 commit da8f60d

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/main/java/org/influxdb/dto/Point.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class Point {
5050
});
5151

5252
private static final int DEFAULT_STRING_BUILDER_SIZE = 1024;
53+
private static final int MAX_STRING_BUILDER_SIZE = 64 * 1024;
5354
private static final ThreadLocal<StringBuilder> CACHED_STRINGBUILDERS =
5455
ThreadLocal.withInitial(() -> new StringBuilder(DEFAULT_STRING_BUILDER_SIZE));
5556

@@ -554,7 +555,12 @@ public String lineProtocol(final TimeUnit precision) {
554555
// setLength(0) is used for reusing cached StringBuilder instance per thread
555556
// it reduces GC activity and performs better then new StringBuilder()
556557
StringBuilder sb = CACHED_STRINGBUILDERS.get();
557-
sb.setLength(0);
558+
if (sb.capacity() > MAX_STRING_BUILDER_SIZE) {
559+
sb = new StringBuilder(DEFAULT_STRING_BUILDER_SIZE);
560+
CACHED_STRINGBUILDERS.set(sb);
561+
} else {
562+
sb.setLength(0);
563+
}
558564

559565
escapeKey(sb, measurement);
560566
concatenatedTags(sb);

0 commit comments

Comments
 (0)