11

I have a project that I am planning, and one bit of information I'll want to have is how much memory a reference takes up in Python. AFAIK, a reference in python is the same as a pointer, and I am guessing that it would be the same size as a 32bit or 64bit pointer (but I could be wrong).

Could anyone clear this up for me, so that I don't have to go on guesswork?

asked May 19, 2015 at 15:01
8
  • 3
    This will heavily depend on the particular implementation of python you want to use. Commented May 19, 2015 at 15:04
  • I don't know how to target references specifically, but sys.sizeof() may help you, as it returns the size of an object in bytes. Commented May 19, 2015 at 15:06
  • @dhke I plan to use CPython, but may also use Cython or Stackless. Commented May 19, 2015 at 15:11
  • 1
    @SuperBiasedMan Good point, thanks. Looks like it's 8 bytes, which is the size you'd expect of a pointer in a 64bit program. Commented May 19, 2015 at 15:21
  • 8
    sys.getsizeof() returns the size of the referenced object, not the reference itself. Yes, Python just uses pointers for references. Commented May 19, 2015 at 15:33

1 Answer 1

9

This was answered in the comments through a discussion between me and SuperBiasedMan. The size of a reference in Python is the same as the word size for the CPU. So, it's 4 bytes on a 32bit build of python, and 8 bytes on 64bit python.

answered Dec 16, 2015 at 17:00
Sign up to request clarification or add additional context in comments.

1 Comment

Note that there may be other costs associated with that reference besides the size of the pointer itself. For example, global variables are stored in dicts, and adding another global variable might do nothing to the size of the dict, or it might require a resize and nearly double the size of the dict.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.