org.springframework.core.io.buffer. DataBufferLimitException: Exceeded limit on max bytes to buffer – How To Fix?

How To Fix “org.springframework.core.io.buffer. DataBufferLimitException: Exceeded limit on max bytes to buffer”

The “org.springframework.core.io.buffer. DataBufferLimitException: Exceeded limit on max bytes to buffer” error may occur when you are developing a Spring WebFlux application. Let’s find out how you can fix this problem.

The “org.springframework.core.io.buffer. DataBufferLimitException: Exceeded limit on max bytes to buffer” Error

Reproduce The Error

After writing your Spring application with the WebFlux module, you can test it with the curl command:

$ curl https://localhost:8080/LearnShareIT

Another way is to open your browser and enter the above URL. However, if you build a memory-heavy application, you may see this message instead:

org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 272562
  at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:99)

Cause

WebFlux is an alternative to the MVC module in the Spring framework. It was only added to Spring in version 5.0, allowing developers to use the reactive programming paradigm in Java or Kotlin.

Applications written with WebFlux typically make use of the org.springframework.http.codec module to handle input and output streams between the client and server. These reactive streams provide the ability to process asynchronous streams with non-blocking back pressure.

It is important to note that there is a limit on how big of an input stream you can buffer when aggregating it. This limit can occur when your application splits the input stream. For example, when you split a text object into multiple smaller strings separated by a delimiter, the limit applies to those substrings.

By default, Spring doesn’t set this limit. It defaults to the particular codec you are using. In most cases, this value is 256K.

The DataBufferLimitException is raised when the cumulative amount of memory consumed by a stream exceeds that pre-configured limit. It happens a lot when you aggregate or cache data buffers. Another common circumstance is when you aggregate the parsed representation of a data buffer when it has been released.

The obvious solution for this error is to raise the memory limit on your data buffer. There are two ways to do this: the maxInMemorySize() method and the max-in-memory-size application property.

maxInMemorySize()

This is a method of the CodecConfigurer.DefaultCodecs interface (which is used when you want to customize the way HTTP messages are read and rendered on the server side).

Its syntax is as follows:

void maxInMemorySize(int byteCount)

You can change the byteCount argument to the number of bytes to buffer input streams. When it is set to -1, there is no limit.

This method needs to be put in the WebFluxConfigurer class, where you can change the settings of our WebFlux applications:

@Configuration
@EnableWebFlux
class MyConfig implements WebFluxConfigurer {
    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        configurer.defaultCodecs().maxInMemorySize(1000 * 1024);
    }
}

The above example sets the limit to 1000K. Change it to the value you desire.

max-in-memory-size

Another way to adjust this limitation is the Spring YAML configuration file. It should contain this line to raise the limit:

spring:
    codec:
        max-in-memory-size: 1000KB

Summary

The “org.springframework.core.io.buffer. DataBufferLimitException: Exceeded limit on max bytes to buffer” error happens when input streams in your WebFlux project have hit their buffer limit. Increase this value, and you will avoid that problem.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *