1

There is a C API function for Python dictionaries called PyDict_ClearFreeList. However the docstring is rather sparse:

int PyDict_ClearFreeList()

Clear the free list. Return the total number of freed items.

New in version 3.3.

This function has no parameters so it (probably) has nothing to do with any specific dictionary.

And it returns an int. This seems rather weird because that indicates it refers to some C state because python "classes" always have a Py_ssize_t size.

So what exactly is this "free list"?

Community
  • 1
  • 1
MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • 2
    Are you vaguely familiar with the dictobject.c source? https://github.com/python/cpython/blob/master/Objects/dictobject.c Don't worry if you aren't a C expert. You can learn quite a lot about how dicts work just from the comments. – PM 2Ring Feb 25 '17 at 13:14

1 Answers1

4

You are correct that the free list has nothing to do with any specific dictionary; in fact, it applies to numerous data types (e.g. PyFloat_ClearFreeList).

The free list is where Python keeps the list of the free places it could put things - i.e. memory containing objects it is no longer using, but which it hasn't released back to the global Python memory pool or the system.

Per this useful Theano tutorial:

To speed-up memory allocation (and reuse) Python uses a number of lists for small objects. Each list will contain objects of similar size: there will be a list for objects 1 to 8 bytes in size, one for 9 to 16, etc. When a small object needs to be created, either we reuse a free block in the list, or we allocate a new one.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437