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

Memory Leak in S3's download_fileobj and upload_fileobj Methods #4132

Closed
Gabibing opened this issue May 16, 2024 · 8 comments
Closed

Memory Leak in S3's download_fileobj and upload_fileobj Methods #4132

Gabibing opened this issue May 16, 2024 · 8 comments
Assignees
Labels
bug This issue is a confirmed bug. closed-for-staleness p2 This is a standard priority issue response-requested Waiting on additional information or feedback. s3

Comments

@Gabibing
Copy link

Gabibing commented May 16, 2024

Describe the bug

I have encountered a memory leak issue when using the S3 client's download_fileobj and upload_fileobj methods with BytesIO in torchserve environment.

Expected Behavior

I expected the memory usage to remain stable when using download_fileobj and upload_fileobj methods for downloading and uploading files to and from an S3 bucket.

Current Behavior

The memory usage increases constantly and is not released properly. Even after running gc.collect().

Reproduction Steps

Perhaps the issue occurs when continuously downloading/uploading different files.
I downloaded/uploaded different media files within torchserve(multi thread env).

import io
import boto3

def reproduce_memory_leak():
    s3_client = boto3.client('s3')
    bucket = 'your-bucket-name'
    s3key_src = '5-10MB files.wav' + str(i) 
    s3key_dst = '5-10MB files.wav' + str(i)

    for i in range(1000):
        with io.BytesIO() as buf:
            s3_client.download_fileobj(bucket, s3key_src, buf)

        with io.BytesIO(b'example bytes') as buf:
            s3_client.upload_fileobj(buf, bucket, s3key_dst)

if __name__ == "__main__":
    reproduce_memory_leak()

Possible Solution

The memory leak can be resolved by replacing the code with the following implementation:

# s3_client.download_fileobj(bucket, s3key_src, buf)
with io.BytesIO() as buf:
    response = s3_client.get_object(Bucket=bucket, Key=s3key_src)
    buf.write(response['Body'].read())

# s3_client.upload_fileobj(buf, bucket, s3key_dst)
with io.BytesIO(b'example bytes') as buf:
    s3_client.put_object(Bucket=bucket, Key=s3key_dst, Body=buf.getvalue())

Additional Information/Context

additional attchment: pytorch config.properties

default_workers_per_model=2
vmargs=-Xmx8g -XX:+UseContainerSupport -XX:+ExitOnOutOfMemoryError
install_py_dep_per_model=true

inference_address=http://0.0.0.0:8080
cors_allowed_origin='*'
cors_allowed_methods=GET, POST
cors_allowed_headers=X-Custom-Header

max_request_size=655350000
max_response_size=655350000
default_response_timeout=300

SDK version used

1.34.106

Environment details (OS name and version, etc.)

Ubuntu 20.04.6 LTS (Docker)

@Gabibing Gabibing added bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged. labels May 16, 2024
@Gabibing Gabibing changed the title Memory Leak in download_fileobj and upload_fileobj Methods Memory Leak in S3's download_fileobj and upload_fileobj Methods May 16, 2024
@Gabibing
Copy link
Author

I'm closing this issue as it is likely related to PyTorch rather than boto3.

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

@Edwardyan1112
Copy link

@Gabibing I encountered a similar issue. Using download_fileobj in the dataloader leads to memory leaks. Could you explain why you say this problem is related to PyTorch? Is it related to the versions of PyTorch and boto3?

@Gabibing Gabibing reopened this May 20, 2024
@Gabibing
Copy link
Author

@Edwardyan1112 I was mistaken. I think it's a memory issue with boto3. So I reopened this issue.

@Edwardyan1112
Copy link

@Edwardyan1112 I was mistaken. I think it's a memory issue with boto3. So I reopened this issue.

What specific problems did you encounter?

@Gabibing
Copy link
Author

@Edwardyan1112 When I use download_fileobj and upload_fileobj, MemoryUtilization.Percent indicators continue to increase. But it won't free memory.

@tim-finnigan tim-finnigan self-assigned this May 22, 2024
@tim-finnigan tim-finnigan added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label May 22, 2024
@tim-finnigan
Copy link
Contributor

Thanks for reaching out, can you you please share debug logs (with sensitive info redacted) by adding boto3.set_stream_logger('') to your script, as well as a memory profile report?

@tim-finnigan tim-finnigan added response-requested Waiting on additional information or feedback. s3 p2 This is a standard priority issue and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. needs-triage This issue or PR still needs to be triaged. labels May 22, 2024
Copy link

github-actions bot commented Jun 2, 2024

Greetings! It looks like this issue hasn’t been active in longer than five days. We encourage you to check if this is still an issue in the latest release. In the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or upvote with a reaction on the initial post to prevent automatic closure. If the issue is already closed, please feel free to open a new one.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Jun 2, 2024
@github-actions github-actions bot closed this as completed Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a confirmed bug. closed-for-staleness p2 This is a standard priority issue response-requested Waiting on additional information or feedback. s3
Projects
None yet
Development

No branches or pull requests

3 participants