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

Potential Memory Leak Due to Slice Expression #5327

Closed
1 task
Lslightly opened this issue Oct 8, 2024 · 3 comments
Closed
1 task

Potential Memory Leak Due to Slice Expression #5327

Lslightly opened this issue Oct 8, 2024 · 3 comments
Labels
bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made.

Comments

@Lslightly
Copy link

Describe the bug

Dear developers,

I used a self-written code linter to check the project and discovered a potential memory leak issue.

In the following code, before the slice expression iter.objects[1:], the elements of iter.objects[0] are not set to nil. This could potentially cause the objects pointed to by each element of iter.objects[0] to be continuously referenced by the underlying array pointed to by the iter.objects slice. As a result, these objects may not be garbage collected in a timely manner by the garbage collector.

iter.objects = iter.objects[1:]

// Next will use the S3API client to iterate through a list of objects.
func (iter *DeleteListIterator) Next() bool {
	if len(iter.objects) > 0 {
		iter.objects = iter.objects[1:]
	}

	if len(iter.objects) == 0 && iter.Paginator.Next() {
		iter.objects = iter.Paginator.Page().(*s3.ListObjectsOutput).Contents
	}

	return len(iter.objects) > 0
}

In a high-load production environment, if this memory leak persists, it could lead to increased memory consumption and eventually slow down the application or even cause out-of-memory errors.

A possible solution is setting the elements of iter.objects[0] to nil before the slice expression. This way, the objects that are no longer needed after the slice expression can be properly garbage collected.

However, I am not sure if this is indeed a problem. I would greatly appreciate it if you can kindly confirm whether setting nil may cause wrong behaviors of the program.

Thank you for your time and efforts in reading this issue and maintaining the project.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

Setting nil does not affect the behavior.

Current Behavior

No nil setting exist.

Reproduction Steps

The code is detected statically.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

none

Environment details (Version of Go (go version)? OS name and version, etc.)

none

@Lslightly Lslightly added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 8, 2024
@Madrigal
Copy link
Contributor

Madrigal commented Oct 9, 2024

Hi @Lslightly,

Thank you for taking the time to report this. We appreciate your diligence in identifying this possible memory leak concern.

First thing to note: This is the version 1 of the Go SDK, which is currently on maintenance mode (see pinned issue #5270). Because of this, we will unfortunately not be making any changes to this package unless they are critical or address a security issue.

Instead, we would kindly ask that you please try running your linter in the aws-sdk-go-v2 project, where active development is happening. We would be more than happy to investigate further any issues brought up and work towards a resolution.

Answering your question

However, I am not sure if this is indeed a problem. I would greatly appreciate it if you can kindly confirm whether setting nil may cause wrong behaviors of the program.

It does seem like a potential memory leak, specially under high pressure.

Thank you again for your contribution and for helping us improve the quality of our software.

@Madrigal Madrigal added closing-soon This issue will automatically close in 4 days unless further comments are made. and removed needs-triage This issue or PR still needs to be triaged. labels Oct 9, 2024
@Lslightly
Copy link
Author

Thanks for your patience and I will try aws-sdk-go-v2.

Copy link

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made.
Projects
None yet
Development

No branches or pull requests

2 participants