-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add memory footprint query #47147
Comments
I propose a patch which allows to query the memory footprint of an The patch implements a generic function to compute the object One advantage with this approach is that the object size can be I am not completely happy with the name 'footprint', but think using Current test pass on linux32 and linux64, but the test suite is not This patch is part of my Google Summer of Code project on Python |
Can't you write this as a simple Python function using In any case, if this is added somewhere it should not be a builtin. This |
Yes, it would be possible and has been done, e.g.
What place would you consider to be appropriate? |
Such implementation-specific things usually went into the sys module. |
It's actually not possible, in general, to compute the memory Even for the things such as lists where the formula I don't really see a problem with calling it sizeof, so I would then |
Proposals like this have been rejected in the past. Memory consumption |
That issue is addressed in this patch.
but they allocate just an upper bound.
These issues are orthogonal to the memory consumption of a single
This is addressed in the current patch.
While true in general, this is not true in practice - in particular,
This is not true. It is meaningful to say that (and many that you The patch meets a real need, and is the minimum amount of code that |
Lists will need a custom tp_footprint then, too. Or, if we call it |
True.
It wouldn't be a type method, but a regular method on the specific type, I think that would work as well. |
Guido, recently you've been opposed to adding more slots. Any opinions |
I'm torn about the extra slot; I'd rather not add one, but I can't see It should definitely not be a built-in; the sys module is fine though |
I think adding a default __sizeof__ implementation into object Not adding the default into object would cause an exception to be Having to look __sizeof__ up in the class dictionary, and |
I tried to implement a magic method __sizeof__() for the type object >>> type.__sizeof__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor '__sizeof__' of 'type' object needs an argument Debugging it I found that type_getattro will (1) look for the I actually want it to perform (3), but since type is its own Is my understanding correct? How would it be possible to invoke My first approach did the same for object, that is a magic __sizeof__() >>> >>> isinstance(int, object)
True Any suggestions on that? thanks, |
You probably just need to make the method a class method -- see METH_CLASS. |
thanks, that did the trick. |
The attached patch implements the sizeof functionality as a sys module |
Nick Coghlan helped me to clear my 'metaclass confusion' so here is a |
The patch looks fine to me, please apply. Don't forget to add a |
Applied in r63856. |
Three questions on the sizeof.patch:
+ res = sizeof(PyDictObject) + sizeof(mp->ma_table); is the sizeof(mp->ma_table) counted twice?
....
static PyObject *
tuple_sizeof(PyTupleObject *v)
{
Py_ssize_t res;
res = _PyObject_SIZE(&PyTuple_Type) + Py_SIZE(v) *
sizeof(void*);
return PyInt_FromSsize_t(res);
} PyDoc_STRVAR(sizeof_doc, .... /Jean Brouwers |
Jean Brouwers wrote:
Yes, you are right. I'll fix this.
Isn't this addressed by taking PyStringObject.ob_sval into account? It PyObject_MALLOC(sizeof(PyStringObject) + size) which should mean that the space for the null terminating character is
No, that slipped the initial patch. I corrected in r64230.
Your implementation is like the applied changes from me, with one When a tuple's memory is allocated, the required space is computed (typeobj)->tp_basicsize + (nitems)*(typeobj)->tp_itemsize Thus, I understand the memory allocated by a tuple to be res = PyTuple_Type.tp_basicsize + Py_SIZE(v) * sizeof(PyObject *); |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: