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
tarfile's cache balloons in memory when streaming a big tarfile #102120
Comments
PurityLake
added a commit
to PurityLake/cpython
that referenced
this issue
Feb 21, 2023
PurityLake
added a commit
to PurityLake/cpython
that referenced
this issue
Feb 24, 2023
PurityLake
added a commit
to PurityLake/cpython
that referenced
this issue
Feb 24, 2023
PurityLake
added a commit
to PurityLake/cpython
that referenced
this issue
Mar 8, 2023
PurityLake
added a commit
to PurityLake/cpython
that referenced
this issue
Mar 8, 2023
PurityLake
added a commit
to PurityLake/cpython
that referenced
this issue
Mar 8, 2023
PurityLake
added a commit
to PurityLake/cpython
that referenced
this issue
Mar 8, 2023
PurityLake
added a commit
to PurityLake/cpython
that referenced
this issue
Mar 8, 2023
PurityLake
added a commit
to PurityLake/cpython
that referenced
this issue
May 23, 2023
ethanfurman
pushed a commit
that referenced
this issue
May 23, 2023
|
With #102128 merged, is there anything more to do? Can we close this issue? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Bug report
I've got a bunch of tar files containing millions of small files. Never mind how we got here - I need to process those tars, handling each of the files inside. Furthermore, I need to process a lot of these tars, and I'd like to do it relatively quickly on cheapish hardware, so I'm at least a little sensitive to memory consumption.
The natural thing to do is to iterate through the tarfile, extracting each file one at a time, and carefully closing them when done:
This looks like it should handle each small file and discard it when done, so memory should stay pretty tame. I was very surprised to discover that this actually uses gigabytes of memory. That's fixed if you do this:
That works because tarinfo.TarFile has a cache,
self.members. That cache is appended to inTarFile.next(), which in turn is used inTarFile.__iter__.That cache is storing
TarInfos, which are headers describing each file. They're not very large, but with lots and lots of files, those headers can add up.The net result is that it's not possible to stream a tarfile's contents without memory growing linearly with the number of files in the tarfile. This has been partially addressed in the past (see #46334, from way back in 2008), but never fully resolved. It shows up in StackOverflow and probably elsewhere, with a clumsy recommended solution of resetting
tar.memberseach time, but there ought to be a better way.Your environment
CPython 3.10, mostly; I don't think OS or architecture etc are relevant.
Linked PRs
The text was updated successfully, but these errors were encountered: