memory management and -trees if this list were maintained as priority queue (in each algorithmthe requested amount of memory is subtracted from the chosen memory hole and the leftover part of that hole is returned to the free list although it might sound good at firstthe best-fit algorithm tends to produce the worst external fragmentationsince the leftover parts of the chosen holes tend to be small the first-fit algorithm is fastbut it tends to produce lot of external fragmentation at the front of the free listwhich slows down future searches the next-fit algorithm spreads fragmentation more evenly throughout the memory heapthus keeping search times low this spreading also makes it more difficult to allocate large blockshowever the worst-fit algorithm attempts to avoid this problem by keeping contiguous sections of free memory as large as possible garbage collection in some languageslike and ++the memory space for objects must be explicitly deallocated by the programmerwhich is duty often overlooked by beginning programmers and is the source of frustrating programming errors even for experienced programmers the designers of python instead placed the burden of memory management entirely on the interpreter the process of detecting "staleobjectsdeallocating the space devoted to those objectsand returning the reclaimed space to the free list is known as garbage collection to perform automated garbage collectionthere must first be way to detect those objects that are no longer necessary since the interpreter cannot feasibly analyze the semantics of an arbitrary python programit relies on the following conservative rule for reclaiming objects in order for program to access an objectit must have direct or indirect reference to that object we will define such objects to be live objects in defining live objecta direct reference to an object is in the form of an identifier in an active namespace ( the global namespaceor the local namespace for any active functionfor exampleimmediately after the command widgetis executedidentifier will be defined in the current namespace as reference to the new widget object we refer to all such objects with direct references as root objects an indirect reference to live object is reference that occurs within the state of some other live object for exampleif the widget instance in our earlier example maintains list as an attributethat list is also live object (as it can be reached indirectly through use of identifier wthe set of live objects are defined recursivelythusany objects that are referenced within the list that is referenced by the widget are also classified as live objects the python interpreter assumes that live objects are the active objects currently being used by the running programthese objects should not be deallocated other objects can be garbage collected python relies on the following two strategies for determining which objects are live