Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Significant increase in memory consumption StringDecoder when splitting input by delimiters. #31848

Closed
chali opened this issue Dec 14, 2023 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: regression A bug that is also a regression
Milestone

Comments

@chali
Copy link

chali commented Dec 14, 2023

Affects: 6.1.1


Context

I have been moving from Spring 5 to Spring 6 and I noticed some of our tests were failing with OutOfMemoryError I have been able to tease out smaller code sample showing the problem.

val data = ClassPathResource("data.txt").inputStream.bufferedReader().readText()
val buffer: Publisher<DataBuffer> = Flux.just(
            DefaultDataBufferFactory.sharedInstance.wrap(data.toByteArray(
                Charset.forName("UTF-8"))))
StringDecoder.textPlainOnly().decode(buffer, ResolvableType.forClass(String::class.java), null, null).subscribe {
    println(it)
}

The bigger picture when this is happening in our case. We have a test for processing server sent events. Our test has canned events in a single file. Our tests were creating manually response from webclient with the whole file as response body. It seems like the individual event separation by delimiter into own DataBuffer is causing memory consumption. Previous version (5.3.31) of Spring we were using wasn't having this behavior

Reproducing example

  1. clone https://github.com/chali/string-decode-memory-consumption/tree/master
  2. run ./gradlew check

Memory flamegraphs

Spring 6.1.1 is showing following memory consuption

Screenshot 2023-12-14 at 1 19 04 PM

Spring 5.3.31

Screenshot 2023-12-14 at 1 18 48 PM
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 14, 2023
@sdeleuze
Copy link
Contributor

@poutsma After a quick discussion with @simonbasle, it could be related to the fact we were using slice (so kind of views) in 5.3 and split in 6.1 with may involved more allocation. Could you please give it a look to see if there is something to refine in term of efficiency for that use case?

@poutsma poutsma self-assigned this Dec 18, 2023
@poutsma poutsma added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 18, 2023
@poutsma poutsma added this to the 6.1.3 milestone Dec 18, 2023
@poutsma poutsma added the for: backport-to-6.0.x Marks an issue as a candidate for backport to 6.0.x label Dec 18, 2023
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-6.0.x Marks an issue as a candidate for backport to 6.0.x labels Dec 18, 2023
@poutsma
Copy link
Contributor

poutsma commented Dec 18, 2023

@poutsma After a quick discussion with @simonbasle, it could be related to the fact we were using slice (so kind of views) in 5.3 and split in 6.1 with may involved more allocation.

The result of a split operation is as just as much a view as the result of a slice is, split is just more restrictive regarding the scope of the view.

It turned out to be a bug in reducing the capacity of the remainder buffer in DefaultDataBuffer::split. Preparing a fix now.

poutsma added a commit that referenced this issue Dec 18, 2023
This commit ensures that the capacity of the remainder buffer after a
split operation is set directly on the field. Calling capacity(int)
caused a new buffer to be allocated.

See gh-31848
Closes gh-31859
@chali
Copy link
Author

chali commented Dec 18, 2023

Thank you for quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

4 participants