The surprising way to save memory with BytesIO
If you need a file-like object that stores bytes in memory in Python, chances are you you’re using Pytho’s built-in io.BytesIO()
.
And since you’re already using an in-memory object, if your data is big enough you probably should try to save memory when reading that data back out.
After all, it’s better not to have two copies of all the data in memory when only one will suffice.
In this article we’ll cover:
- A quick intro to
BytesIO
. - The memory usage impacts of
BytesIO.read()
. - The two alternatives for accessing
BytesIO
data efficiently, and the tradeoffs between them.
So what’s a BytesIO
?
Python’s io.BytesIO
allows you to create a file-like object that stores bytes in