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

Fix Consistent-Hash Memory Leak #151

Merged
merged 1 commit into from
Feb 7, 2024

Conversation

tempusfrangit
Copy link
Contributor

Based upon how bytes.Buffer works, it will continue to grow the underlying slice while reading data and add additional headroom to ensure that there is enough space, this means that when we allocate the bytes.Buffer to a small amount (minChunkSize()) the growth occurs regularly on any reads that push beyond the initial slice. Even at a minChunkSize of ~150M, when downloading SDXL we end up with a balloon of an additional ~10GiB allocated.

This has resulted in SigKill (OOMKILL) of pget in lower-ram systems. This does not address the overall memory allocation requirements of pget, which still generally encompasses the entire file size. An additional pass will need to be done to ensure the consumer can read and the buffer can be GC'd to minimize overall memory allocation.

This only surfaced in consistent-hash mode as buffer mode was properly setting the buffered reader to the chunkSize by calculating end - start +1. Consistent Hash only used m.minChunkSize() which is appropriate for the initial chunk download, but inappropriate for the subsequent larger chunks.

This particular memory leak would not affect files or file chunks that are smaller than the m.minChunkSize() value.

Closes: #150

Based upon how bytes.Buffer works, it will continue to grow the
underlying slice while reading data and add additional headroom to
ensure that there is enough space, this means that when we allocate the
bytes.Buffer to a small amount (minChunkSize()) the growth occurs
regularly on any reads that push beyond the initial slice. Even at a
minChunkSize of ~150M, when downloading SDXL we end up with a balloon of
an additional ~10GiB allocated.

This has resulted in SigKill (OOMKILL) of pget in lower-ram systems.
This does not address the overall memory allocation requirements of
pget, which still generally encompasses the entire file size. An
additional pass will need to be done to ensure the consumer can read and
the buffer can be GC'd to minimize overall memory allocation.

This only surfaced in consistent-hash mode as buffer mode was properly
setting the buffered reader to the chunkSize by calculating end - start
+1. Consistent Hash only used m.minChunkSize() which is appropriate for
the initial chunk download, but inappropriate for the subsequent larger
chunks.

This particular memory leak would not affect files or file chunks that
are smaller than the m.minChunkSize() value.

Closes: #150
@tempusfrangit tempusfrangit added the bug Something isn't working label Feb 7, 2024
@tempusfrangit tempusfrangit self-assigned this Feb 7, 2024
Copy link
Contributor

@philandstuff philandstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. Super minor quibble: this isn't a memory leak, it's just inefficient memory use. Either way it's worth merging 👍

@philandstuff philandstuff merged commit 12cf853 into main Feb 7, 2024
5 checks passed
@philandstuff philandstuff deleted the fix-memory-leak-consistent-hash branch February 7, 2024 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MEMORY LEAK: In consistent hash mode pget often uses very large amounts of memory
2 participants