Squall Leohart
Squall Leohart

Reputation: 677

Python "sys.getsizeof" reports same size after items removed from list/dict?

I notice that when using sys.getsizeof() to check the size of list and dictionary, something interesting happens.

i have:

a = [1,2,3,4,5]

with the size of 56 bytes (and empty list has size of 36, so it makes sense because 20/5 = 4)

however, after I remove all the items in the list (using .remove or del), the size is still 56. This is strange to me. Shouldn't the size be back to 36?

Any explanation?

Upvotes: 4

Views: 1081

Answers (3)

Ned Batchelder
Ned Batchelder

Reputation: 375624

The list doesn't promise to release memory when you remove elements. Lists are over-allocated, which is how they can have amortized O(1) performance for appending elements.

Details of the time performance of the data structures: http://wiki.python.org/moin/TimeComplexity

Upvotes: 12

kindall
kindall

Reputation: 184211

Increasing the size of a container can be an expensive operation, since it may require that a lot of things be moved around in memory. So Python almost always allocates more memory than is needed for the current contents of a list, allowing any individual addition to the list to have a very good chance of being performed without needing to move memory. For similar reasons, a list may not release the memory for deleted elements immediately, or ever.

However, if you delete all the elements at once using a slice assignment:

a[:] = []

that seems to reset it. This is an implementation detail, however.

Upvotes: 7

Charles Brunet
Charles Brunet

Reputation: 23120

When you append an item to a Python list, it allocates a given amount of memory if the already allocated memory for the list is full. When you remove an item from a list, it keeps memory allocated for the next time you would append items to the list. See this related post for an example.

Upvotes: 1

Related Questions