-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
database/sql: memory leaks in Stmt.removeClosedStmtLocked #66410
Comments
Thank you @apocelipes, for the discovery and for the investigation! Yes please, welcome to the Go project and we would greatly appreciate it if you could send a change through! However, I do encourage the much simpler and cheaper approach of inserting a nil at the end of the slice and adding a comment s.css[i] = s.css[len(c.ss)-1]
// Zero the last element before shrinking, for GC.
s.css[len(s.css)-1] = nil
s.css = s.css[:len(s.css)-1] |
Zero out elements before shrinking the slice to avoid memory leaks. Fixes golang#66410.
Change https://go.dev/cl/572956 mentions this issue: |
Zero out elements before shrinking the slice to avoid memory leaks. Fixes golang#66410
Zero out elements before shrinking the slice to avoid memory leaks. Fixes golang#66410.
Zero out elements before shrinking the slice to avoid memory leaks. Fixes golang#66410
Zero out elements before shrinking the slice to avoid memory leaks. Fixes golang#66410
Zero out elements before shrinking the slice to avoid memory leaks. Fixes golang#66410 Change-Id: I8f64c21455761f7f7c8b6fee0b6450b98f691d91 GitHub-Last-Rev: b15586e GitHub-Pull-Request: golang#66419 Reviewed-on: https://go-review.googlesource.com/c/go/+/572956 TryBot-Result: Gopher Robot <gobot@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
Go version
go devel go1.23-8f7df2256e
Output of
go env
in your module/workspace:What did you do?
I was reading through the source code of database/sql and I found some potential memory leaks.
What did you see happen?
In the method "Stmt.removeClosedStmtLocked", old elements are not cleared after slice shrinking:
The code is here.
Since "s.css" is a slice of ”connStmt“ and ”connStmt“ contains two pointer fields, if we don't set the element removed from "s.css" to a zero value, a potential memory leak will occur.
What did you expect to see?
We should zero out the obsolete elements or just use "slices.DeleteFunc" to avoid leaks and simplify the code.
Using "slices.DeleteFunc" will result in more data copies, but I don't think it will have much impact on "connStmt" (It's small).
If you think this need to be fixed, I'm happy to contribute a related pull request.
The text was updated successfully, but these errors were encountered: