-
Notifications
You must be signed in to change notification settings - Fork 41.9k
WritableJson allocates too much memory #49428
Description
Enhancement request
Hi, we are using LogstashStructuredLogFormatter in our projects and it has affected the performance because the garbage collector is running a lot. After analyzing the allocated memory we have detected that the WritableJson.toByteArray method is the one to blame.
As far as I can see, you are using 2 buffers in memory for the same thing:
default byte[] toByteArray(Charset charset) {
Assert.notNull(charset, "'charset' must not be null");
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
toWriter(new OutputStreamWriter(out, charset));
return out.toByteArray();
}
catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}The 2 buffers are:
ByteArrayOutputStreambuffer.OutputStreamWriter.se.bb(aByteBufferinside theStreamEncoderinside theOutputStreamWriter)
Do you think that you could use "toJsonString().getBytes()"? Or maybe use some kind of org.springframework.core.io.buffer.DataBuffer implementation?
Also, when you compare WritableJson with Jackson ObjectMapper, you see that the ObjectMapper includes a "BufferRecycler" to reduce the number of times that the garbage collection has to run.
Do you think that you could use something similar?
Thanks!