Search code examples
pythonpython-3.xnumpypython-internalsinternals

Why does a numpy array have 96 bytes of overhead?


If I take a simply and empty numpy array i can see it has 96 bytes of overhead,

>>> sys.getsizeof( np.array([]) )
96

What is that 96 bytes storing? Where in the C source for numpy or Python 3 (cpython) is this set up?


Solution

  • Array is present in C sources in numpy/core/include/numpy/ndarraytypes.h

    See: https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/ndarraytypes.h

    Looks like it has several pointers, number of dimensions and PyObject_HEAD, which all may in total count to number of bytes you see.

    /*                                                                                                                                                                                                                                            
     * The main array object structure.                                                                                                                                                                                                           
     */
    /* This struct will be moved to a private header in a future release */
    typedef struct tagPyArrayObject_fields {
        PyObject_HEAD
        /* Pointer to the raw data buffer */
        char *data;
        /* The number of dimensions, also called 'ndim' */
        int nd;
        /* The size in each dimension, also called 'shape' */
        npy_intp *dimensions;
        /*                                                                                                                                                                                                                                        
         * Number of bytes to jump to get to the                                                                                                                                                                                                  
         * next element in each dimension                                                                                                                                                                                                         
         */
        npy_intp *strides;
    
        PyObject *base;
        /* Pointer to type structure */
        PyArray_Descr *descr;
        /* Flags describing array -- see below */
        int flags;
        /* For weak references */
        PyObject *weakreflist;
    } PyArrayObject_fields;