Skip to content

WritableJson allocates too much memory #49428

@System25

Description

@System25

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:

  1. ByteArrayOutputStream buffer.
  2. OutputStreamWriter.se.bb (a ByteBuffer inside the StreamEncoder inside the OutputStreamWriter)

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions