-
Notifications
You must be signed in to change notification settings - Fork 18k
strings: strings.Builder case memory leak #34463
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
Comments
Please show us more of the program. The memory profile you show is exactly what I would expect if your program never releases the strings built with That said, while there may be a bug here somewhere, it does not make sense to put |
Thanks for help. Yes, you are right, when I read the detail code of We are using The main target of the function is to build a sql like The demo of our function is here: https://play.golang.org/p/o0cEeZ7o7xl The function is called every 3min by a time.Ticker. The memory increase really slow, the memory monitor of previous 7 days is blow: This case me to read heap to looking for what is leaking. Thanks again~ |
Closing as this looks to be resolved and working as intended. |
I've tried some solution and I think I found the leaking code, but I don't know why(I think it's a bug). First solution, I remove the usage of // Key code of first solution
sb := new(strings.Builder)
// full code writing ref: https://play.golang.org/p/o0cEeZ7o7xl
// sb.WriteString(...)
// sb.WriteRune(...)
_, err := ormer.Raw(strings.TrimSuffix(sb.String(), ","), sqlParamList...).Exec() The second solution worked and the memory is not leaking. // Key code of second solution
sb := new(strings.Builder)
// full code writing ref: https://play.golang.org/p/o0cEeZ7o7xl
// sb.WriteString(...)
// sb.WriteRune(...)
sql := sb.String()
sql = strings.TrimSuffix(sql, ",")
sb.Reset()
sb = nil
_, err := ormer.Raw(sql, sqlParamList...).Exec() Here is the memory usage curve: I don't really understand why the first solution will case the Here is the heap of first solution: Here is the heap of second solution: |
How are you measuring memory use? |
The curve is provide by monitor-agent of The ECS only run the About our usage: We are running a same app-server for different customers on different Firstly, I found the memory keep high-load on a Cause of our The smaller server has the some leaking too, but less users makes it much more slowly, the heap shows that the leaking is on Thanks for help, let me know If any other information I can provide,. |
Hi, @martisch @ianlancetaylor, here is the additional information: I tried another code in another server:
Just a little different of the Here is the heap: And here is the heap for |
Really sorry that I made a mistake that set |
Just reset |
What version of Go are you using (
go version
)?Tried both 1.13.0 and 1.12.9.
Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (
go env
)?In docker image
alpine:3.8
, build cmd isGOOS=linux GOARCH=amd64 go build
on macOS10.14.6
go env
OutputWhat did you do?
Using a
sync.Pool
to reuse strings.Builder.https://play.golang.org/p/TkWbxGu4PRJ
I use strings.Builder to build sql, and exec the
sql-str
every times, but I found that the memory will increase slowly, then I calledpprof/heap
and found that thestrings.(*Builder).WriteString
inuse a lot of memory and looks never released.What did you expect to see?
The memory not increase unexpectedly
What did you see instead?
The memory not increase unexpectedly
Really thanks for help!
The text was updated successfully, but these errors were encountered: