The short answer

You're getting the size of the class, not of an instance of the class. Call int to get the size of an instance:

>>> sys.getsizeof(int())
28

If that size still seems a little bit large, remember that a Python int is very different from an int in (for example) C. In Python, an int is a fully-fledged object. This means there's extra overhead.

Every Python object contains at least a refcount and a reference to the object's type in addition to other storage; on a 64-bit machine, just those two things alone take up 16 bytes! The int internals (as determined by the standard CPython implementation) have also changed over time, so that the amount of additional storage taken depends on your version.

int objects in CPython 3.11

Integer objects are internally PyLongObject C types representing blocks of memory. The code that defines this type is spread across multiple files. Here are the relevant parts:

typedef struct _longobject PyLongObject;

struct _longobject {
    PyObject_VAR_HEAD
    digit ob_digit[1];
};

#define PyObject_VAR_HEAD      PyVarObject ob_base;

typedef struct {
    PyObject ob_base;
    Py_ssize_t ob_size; /* Number of items in variable part */
} PyVarObject;

typedef struct _object PyObject;

struct _object {
    _PyObject_HEAD_EXTRA
    union {
       Py_ssize_t ob_refcnt;
#if SIZEOF_VOID_P > 4
       PY_UINT32_T ob_refcnt_split[2];
#endif
    };
    PyTypeObject *ob_type;
};

/* _PyObject_HEAD_EXTRA is nothing on non-debug builds */
#  define _PyObject_HEAD_EXTRA

typedef uint32_t digit;

If we expand all the macros and replace all the typedef statements, this is the struct we end up with:

struct PyLongObject {
    Py_ssize_t ob_refcnt;
    PyTypeObject *ob_type;
    Py_ssize_t ob_size; /* Number of items in variable part */
    uint32_t ob_digit[1];
};

uint32_t means "unsigned 32-bit integer" and uint32_t ob_digit[1]; means an array of 32-bit integers is used to hold the (absolute) value of the integer. The "1" in "ob_digit[1]" means the array should be initialized with space for 1 element.

So we have the following bytes to store an integer object in Python (on a 64-bit system):

  • 8 bytes (64 bits, Py_ssize_t, signed) for ob_refcnt - the reference count
  • 8 bytes (64 bits, PyTypeObject*) for ob_type - the pointer to the int class itself
  • 8 bytes (64 bits, Py_ssize_t, signed) for ob_size - which stores how many 32-bit integers are used to store the integer

and finally a variable-length array (with at least 1 element) of

  • 4 bytes (32 bits) to store each part of the integer

The comment that accompanies this definition summarizes Python 3.11's representation of integers. Zero is represented not by an object with size (ob_size) zero (the actual size is always at least 1 though). Negative numbers are represented by objects with a negative size attribute! This comment further explains that only 30 bits of each uint32_t are used for storing the value.

>>> sys.getsizeof(0)
28
>>> sys.getsizeof(1)
28
>>> sys.getsizeof(2 ** 30 - 1)
28
>>> sys.getsizeof(2 ** 30)
32
>>> sys.getsizeof(2 ** 60 - 1)
32
>>> sys.getsizeof(2 ** 60)
36

On CPython 3.10 and older, sys.getsizeof(0) incorrectly returns 24 instead of 28, this was a bug that was fixed. Python 2 had a second, separate type of integer which worked a bit differently, but generally similar.

You will get slightly different results on a 32-bit system.

Answer from senderle on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how much memory do int's take up in python?
r/learnpython on Reddit: How much memory do int's take up in python?
December 6, 2014 -

Going through the docs I didn't find a definite answer

If I do

x = 234234
sys.getsizeof(x)

It returns 28, so that's 224 bits ?

Apparently getsizeof() returns the size of an object in bytes, is this size consistent across all int's in python though?

I know that there are long ints, and that they are converted automatically to long ints at a certain value in python, but I'm just wondering about ints for now.

In Java the size that I got was 32 bit, so 4 bytes.

Is this correct, that :

python int = 224 bits

java int = 32 bits

And is this because the python int has more methods etc?

Basically what I was trying to state was that across programming languages the data type sizes were fairly conventional and that an int in C# would be the same size as an int in Python / Java. I think this might be wrong though.

Thanks!

Top answer
1 of 4
4

In Python, integers have arbitrary magnitude. You need not worry about how many bits are used in memory to store the integer; integers can be arbitrarily large (or large negative).

In this case, the 224 bits you are seeing are not all used to represent the integer; a lot of that is overhead of representing a Python object (as opposed to a native value in C-like languages).

EDIT: See also: https://docs.python.org/3/library/stdtypes.html#typesnumeric

2 of 4
3

is this because the python int has more methods etc?

Almost but not quite.

Internally, all python objects contain at least a reference count used by the garbage collector and a pointer to a [statically-allocated] type object (which is essentially an array of function pointers to the methods available on objects of that type). Objects with variable size (like lists, dicts, and python 3's variable-precision ints) also contain a size value.

So that's 3 values stored in the "header" of each python object. On a 32-bit platform these 3 values will most likely take 3*32 bits, and on a 64-bit platform they will take 3*64 bits. So any variable-size python object with zero size is going to take at least 24 bytes on a 64-bit machine.

Beyond that, the value of a python long integer is actually stored in an array of 15- or 30-bit chunks (depending on your build). On my 64-bit build, you can see the size of integers increase steadily as I increase their size by increments of 30 bits.

>>> [sys.getsizeof(2**(n*30) - 1) for n in range(100)]
[24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 
 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152, 156, 
 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204, 208, 212, 216, 
 220, 224, 228, 232, 236, 240, 244, 248, 252, 256, 260, 264, 268, 272, 276, 
 280, 284, 288, 292, 296, 300, 304, 308, 312, 316, 320, 324, 328, 332, 336, 
 340, 344, 348, 352, 356, 360, 364, 368, 372, 376, 380, 384, 388, 392, 396, 
 400, 404, 408, 412, 416, 420]

And there you have it, demystified I hope.

Top answer
1 of 1
162

The short answer

You're getting the size of the class, not of an instance of the class. Call int to get the size of an instance:

>>> sys.getsizeof(int())
28

If that size still seems a little bit large, remember that a Python int is very different from an int in (for example) C. In Python, an int is a fully-fledged object. This means there's extra overhead.

Every Python object contains at least a refcount and a reference to the object's type in addition to other storage; on a 64-bit machine, just those two things alone take up 16 bytes! The int internals (as determined by the standard CPython implementation) have also changed over time, so that the amount of additional storage taken depends on your version.

int objects in CPython 3.11

Integer objects are internally PyLongObject C types representing blocks of memory. The code that defines this type is spread across multiple files. Here are the relevant parts:

typedef struct _longobject PyLongObject;

struct _longobject {
    PyObject_VAR_HEAD
    digit ob_digit[1];
};

#define PyObject_VAR_HEAD      PyVarObject ob_base;

typedef struct {
    PyObject ob_base;
    Py_ssize_t ob_size; /* Number of items in variable part */
} PyVarObject;

typedef struct _object PyObject;

struct _object {
    _PyObject_HEAD_EXTRA
    union {
       Py_ssize_t ob_refcnt;
#if SIZEOF_VOID_P > 4
       PY_UINT32_T ob_refcnt_split[2];
#endif
    };
    PyTypeObject *ob_type;
};

/* _PyObject_HEAD_EXTRA is nothing on non-debug builds */
#  define _PyObject_HEAD_EXTRA

typedef uint32_t digit;

If we expand all the macros and replace all the typedef statements, this is the struct we end up with:

struct PyLongObject {
    Py_ssize_t ob_refcnt;
    PyTypeObject *ob_type;
    Py_ssize_t ob_size; /* Number of items in variable part */
    uint32_t ob_digit[1];
};

uint32_t means "unsigned 32-bit integer" and uint32_t ob_digit[1]; means an array of 32-bit integers is used to hold the (absolute) value of the integer. The "1" in "ob_digit[1]" means the array should be initialized with space for 1 element.

So we have the following bytes to store an integer object in Python (on a 64-bit system):

  • 8 bytes (64 bits, Py_ssize_t, signed) for ob_refcnt - the reference count
  • 8 bytes (64 bits, PyTypeObject*) for ob_type - the pointer to the int class itself
  • 8 bytes (64 bits, Py_ssize_t, signed) for ob_size - which stores how many 32-bit integers are used to store the integer

and finally a variable-length array (with at least 1 element) of

  • 4 bytes (32 bits) to store each part of the integer

The comment that accompanies this definition summarizes Python 3.11's representation of integers. Zero is represented not by an object with size (ob_size) zero (the actual size is always at least 1 though). Negative numbers are represented by objects with a negative size attribute! This comment further explains that only 30 bits of each uint32_t are used for storing the value.

>>> sys.getsizeof(0)
28
>>> sys.getsizeof(1)
28
>>> sys.getsizeof(2 ** 30 - 1)
28
>>> sys.getsizeof(2 ** 30)
32
>>> sys.getsizeof(2 ** 60 - 1)
32
>>> sys.getsizeof(2 ** 60)
36

On CPython 3.10 and older, sys.getsizeof(0) incorrectly returns 24 instead of 28, this was a bug that was fixed. Python 2 had a second, separate type of integer which worked a bit differently, but generally similar.

You will get slightly different results on a 32-bit system.

Discussions

What is the size of an int in Python? - Stack Overflow
Is an int in Python 4 bytes? Is there a possibility to have a 64-byte 'long long' like in C++? Trying to assign a variable: a = 1234567891011121314 Gives this error: error: can't allocate region More on stackoverflow.com
🌐 stackoverflow.com
integer - How does Python manage int and long? - Stack Overflow
Max size is an amount of bytes. On a 64-bit machine, your virtual memory has 64-bit wide addresses, but we'll leave 1 bit for sign and 1 value for null address, so that gives us 2**63 - 1 addressable bytes. That's 9223372036854775807. A python3 integer is not limited to the 64-bit hardware and can handle (in ... More on stackoverflow.com
🌐 stackoverflow.com
Get size in Bytes needed for an integer in Python - Stack Overflow
I guess the downvotes came from the question asking for an integer in hexadecimal format, not in decimal format. I have asked to edit the question now to cover both hexadecimal and decimal format, elsewise your answer here would not fit to the question. 2021-06-24T09:47:57.503Z+00:00 ... Unless you're dealing with an array.array or a numpy.array - the size always has object overhead. And since Python ... More on stackoverflow.com
🌐 stackoverflow.com
How much memory do int's take up in python?

In Python, integers have arbitrary magnitude. You need not worry about how many bits are used in memory to store the integer; integers can be arbitrarily large (or large negative).

In this case, the 224 bits you are seeing are not all used to represent the integer; a lot of that is overhead of representing a Python object (as opposed to a native value in C-like languages).

EDIT: See also: https://docs.python.org/3/library/stdtypes.html#typesnumeric

More on reddit.com
🌐 r/learnpython
10
4
December 6, 2014
🌐
Python
peps.python.org › pep-0237
PEP 237 – Unifying Long Integers and Integers | peps.python.org
February 1, 2025 - Python currently distinguishes between two kinds of integers (ints): regular or short ints, limited by the size of a C long (typically 32 or 64 bits), and long ints, which are limited only by available memory.
🌐
LinkedIn
linkedin.com › posts › reuven_how-big-is-a-python-integer-many-of-my-activity-7044578562690981888-FdWF
Reuven Lerner on LinkedIn: How big is a Python integer? Many of my students, especially those coming… | 15 comments
March 23, 2023 - But they also have a floor and ... right? Well, Python is there to help you: >>> print(x) ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase .....
🌐
Code-maven
python.code-maven.com › size-of-integer-in-python
Size of integer in Python
It also shows that even for a small integer such as 1, Python uses 28 bytes(!) very wastefull compared to other programming languages and to numpy. It also demonstrats how the used memory will grow as the number grows.
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.01-understanding-data-types.html
Understanding Data Types in Python | Python Data Science Handbook
For example, when we define an integer in Python, such as x = 10000, x is not just a "raw" integer. It's actually a pointer to a compound C structure, which contains several values. Looking through the Python 3.4 source code, we find that the integer (long) type definition effectively looks like this (once the C macros are expanded): struct _longobject { long ob_refcnt; PyTypeObject *ob_type; size_t ob_size; long ob_digit[1]; };
Find elsewhere
🌐
Quora
quora.com › How-many-bytes-does-an-integer-data-occupy-in-the-Python-language
How many bytes does an integer data occupy in the Python language? - Quora
But that’s not guaranteed by C, or C++; as of the latest draft standards, both only require int to be at least 16 bits. In Swift, Int is the same as either Int32 or Int64. In Python, int is an arbitrary-width integer, not any fixed size at all.
🌐
Better Programming
betterprogramming.pub › what-is-up-with-the-numbers-in-python-26d8d36e129b
What Is Up With The Numbers In Python? | by Jacob Toftgaard Rasmussen | Better Programming
February 28, 2022 - What Is Up With The Numbers In Python? Let’s uncover why the size of an integer in Python is at least 24 bytes If you are a seasoned programmer then feel free to jump straight to the part called …
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-length-of-integer-python
How to find length of integer in Python? - GeeksforGeeks
July 23, 2025 - Finding the length of an integer in Python is simply counting number of digits it contains. For example, integer 123 has a length of 3 because it consists of three digits (1, 2, and 3).
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › ints
int — Python Reference (The Right Way) 0.1 documentation
These represent numbers in the range -2147483648 through 2147483647. (The range may be larger on machines with a larger natural word size, but not smaller.) When the result of an operation would fall outside this range, the result is normally returned as a long integer (in some cases, the exception ...
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python integers
An Essential Guide to Python Integers
March 27, 2025 - counter = 10 print(type(counter))Code language: Python (python) ... The output indicates an integer is an instance of the class int. To get the size of an integer, you use the getsizeof() function of the sys module.
🌐
freeCodeCamp
freecodecamp.org › news › maximum-integer-size-in-python
Int Max in Python – Maximum Integer Size
April 3, 2023 - You can check the maximum integer size in Python using the maxsize property of the sys module. In this article, you'll learn about the maximum integer size in Python. You'll also see the differences in Python 2 and Python 3. The maximum value of an ...
🌐
DataCamp
datacamp.com › tutorial › everything-you-need-to-know-about-pythons-maximum-integer-value
What You Need to Know About Python's Maximum Integer Value | DataCamp
May 6, 2024 - Similar to the long data type in Python 2, the int data type in Python 3 is constrained only by the amount of memory available. In fact, because there is effectively no upper limit to the size of integers in Python 3, the long data type doesn't exist in Python 3.
🌐
TechBeamers
techbeamers.com › why-python-integer-size-bigger-than-c
Why is Python’s Integer Size Bigger than C’s int? - TechBeamers
November 30, 2025 - Unlike a big integer, small integers stay in memory as long as the Python interpreter is running. Observe this fact from the example given below. small_int1=-5 => None small_int2=-5 => None small_int1 is small_int2 => True big_int1=257 => None big_int2=257 => None big_int1 is big_int2 => False · The above tests would have given you a fair idea of why integer sizes in Python are bigger than ints in C.
🌐
Arpit Bhayani
arpitbhayani.me › blogs › long-integers-python
How Python Handles Gigantic Integers
January 10, 2020 - Since Python 3 all integers are represented as a bignum and these are limited only by the available memory of the host system. ob_size holds the count of elements in ob_digit.
🌐
Execute Program
executeprogram.com › courses › python-for-programmers › lessons › size-and-precision-of-numbers
Python for Programmers: Size and Precision of Numbers
Learn programming languages like TypeScript, Python, JavaScript, SQL, and regular expressions. Interactive with real code examples.
🌐
GeeksforGeeks
geeksforgeeks.org › what-is-the-maximum-possible-value-of-an-integer-in-python
What is the maximum possible value of an integer in Python ? - GeeksforGeeks
July 28, 2022 - there are two separate types "int" (which is 32 bit) and "long int" that is same as "int" of Python 3.x, i.e., can store arbitrarily large numbers. ... # A Python program to show that there are two types in # Python 2.7 : int and long int # ...
Top answer
1 of 7
38
def byte_length(i):
    return (i.bit_length() + 7) // 8

Of course, as Jon Clements points out, this isn't the size of the actual PyIntObject, which has a PyObject header, and stores the value as a bignum in whatever way is easiest to deal with rather than most compact, and which you have to have at least one pointer (4 or 8 bytes) to on top of the actual object, and so on.

But this is the byte length of the number itself. It's almost certainly the most efficient answer, and probably also the easiest to read.

Or is ceil(i.bit_length() / 8.0) more readable?

2 of 7
30

Unless you're dealing with an array.array or a numpy.array - the size always has object overhead. And since Python deals with BigInts naturally, it's really, really hard to tell...

>>> i = 5
>>> import sys
>>> sys.getsizeof(i)
24

So on a 64bit platform it requires 24 bytes to store what could be stored in 3 bits.

However, if you did,

>>> s = '\x05'
>>> sys.getsizeof(s)
38

So no, not really - you've got the memory-overhead of the definition of the object rather than raw storage...

If you then take:

>>> a = array.array('i', [3])
>>> a
array('i', [3])
>>> sys.getsizeof(a)
60L
>>> a = array.array('i', [3, 4, 5])
>>> sys.getsizeof(a)
68L

Then you get what would be called normal byte boundaries, etc.. etc... etc...

If you just want what "purely" should be stored - minus object overhead, then from 2.(6|7) you can use some_int.bit_length() (otherwise just bitshift it as other answers have shown) and then work from there