-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Possible memory leak when uploading files using S3Client #4916
Comments
Hi @e200, Thanks for the in depth repro steps. I have gotten similar results by running your code. Thanks, |
This bug is making it impossible to use S3. Our services are being restarted every single time we upload files due to the containers running out of resources. |
@e200 you are absolutely right! We are in the same position. I think this should be fixed with highest priority, otherwise the current stable version of AWS SDK for Go is completely useless for accessing to S3. Is that possible? |
@awaken why the irony? I didn't mean to say that the AWS SDK is completely useless, only the current S3 client is causing issues. It may not affect others, but it is surely affecting us. |
@e200 there is a misunderstanding here. I had no intention to make any joke. I was serious. As I said, I think right now the sdk is useless for accessing to S3. For any other use case, I agree with you, it should be working. My intention was to ring a bell for the resolution of the issue. |
Hi @e200 , Running the same profiler for v2 of the SDK didnt bring up the same results. Until the team has bandwidth to prioritize investigating this, can you give v2 a try and see if this helps in the meantime? Thanks, |
I wasn't aware of the new version. Will try... Thank you for the feedback. |
+1 |
@e200 Did V2 work ? |
@bagavp I didn't try anymore. I solved the issue from our side by making O(1) operation instead. |
@e200 I tried the v2 version. there is a memory leak. Add defer, it works.Hope it helps you. body, err := os.ReadFile("test.txt")
if err != nil {
return nil, err
}
reader := bytes.NewReader(body)
defer reader.Reset([]byte("")) // <-- here
putObjectInput := &s3.PutObjectInput{
Body: reader,
Bucket: aws.String(bucket),
Key: aws.String(filename),
} |
The original analysis here is inaccurate. The pprof See the profile descriptions here: https://pkg.go.dev/runtime/pprof#Profile. The
As we can see above, the only thing we're holding onto is the most recent 2MiB buffer used as the upload input. Aside: the reason it's allocating ~1500MiB over the lifetime of your upload loop is because of how the underlying byte slice pool works. The pool grows and shrinks within the life of individual |
|
Describe the bug
This memory leak occurs when uploading content using the S3Client.
Expected Behavior
We expect the process of uploading content to not allocate so much memory.
Current Behavior
If you upload 300 file contents of 2mb to S3 (both local and in the actual AWS) it allocates an average memory of
1490.07MB
in total and the memory is never freed.The issue is within the
github.com/aws/aws-sdk-go/service/s3/s3manager.(*maxSlicePool).newSlice
as you can see in the following pprof output:The same problem doesn't happen if you use the same codebase but replace the client from yours to minio.
Reproduction Steps
Follow the instructions in the readme.md
https://github.com/e200/s3client_memory_leak
Possible Solution
Take a look at the pool. The issue happens for both synchronous and asynchronous executions. The memory is being allocated for each upload and not freed.
Additional Information/Context
We were having this issue in production and our pods were getting out of resources many times because of the amount of allocated resources by the client. We use the client to cache some compiled content and the problem only occurs when uploading, but on download everything seems normal. The allocated memory is never freed, if it is, it is taking to long to be available again.
The same code works as expected if replacing the
aws sdk s3 client
by the packageminio
without any other changes(simply by replacing the package and using its equivalent method PutObject)
.SDK version used
v1.44.302
Environment details (Version of Go (
go version
)? OS name and version, etc.)MacOS Ventura 13.2.1, go 1.20.6
The text was updated successfully, but these errors were encountered: