This is the code i am using to test the memory allocation
import pycurl
import io
url = \"http://www.stackoverflow.com\"
buf = io.BytesIO()
print(len(bu
io.BytesIO() returns a standard file object which has function tell(). It reports the current descriptor position and does not copy the whole buffer out to compute total size as len(bio.getvalue()) of bio.getbuffer().nbytes. It is a very fast and simple method to get the exact size of used memory in the buffer object.
I posted an example code and a more detailed answer here