io.BytesIO

F

Fabian von Romberg

Hi,

is there any way to get the allocated memory size from a io.BytesIO object?

Thanks and regards,
Fabian
 
S

Steven D'Aprano

Hi,

is there any way to get the allocated memory size from a io.BytesIO
object?

The same as for any object:

py> import io, sys
py> obj = io.BytesIO()
py> sys.getsizeof(obj)
48


Is this what you are after, the size of the object itself, including any
overhead?
 
F

Fabian von Romberg

Hi Steven,


actually why I need is to know how much memory has been allocated for buffering.

getsizeof gets the size of the object structure.

For example, if I do io.BytesIO.truncate(10000), it will resize the buffer to 10000 bytes. So my question is how to get those 10000 back from an attribute or method?

Regards,
Fabian
 
S

Steven D'Aprano

Hi Steven,


actually why I need is to know how much memory has been allocated for
buffering.

getsizeof gets the size of the object structure.

I can see at least four ways to get the current size of the BytesIO
buffer:

py> obj = io.BytesIO(b"x"*12468)
py> obj.getbuffer().nbytes
12468
py> len(obj.getvalue())
12468
py> len(obj.getbuffer())
12468
py> obj.seek(0, 2)
12468


As far as I can tell, BytesIO objects do not have a fixed size buffer.
They will increase in size as needed, just like real file objects on a
disk.
 
F

Fabian von Romberg

Hi Steve,

thanks for your reply.

Actually I played around with these methods. Whe you truncate(12468) for example, I thought the object would allocate 12468 bytes and I wanted to get that back. The methods your mention, works only for what ha been written (obj.write()).

I think I should use io.BufferedWriter instead.

Just one question, what has better performance: BufferedWriter or BytesIO?

Thanks and regards,
Fabian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top