Python: does the class class set a "leak" when elements are deleted, for example, dict?

I know that a Python dict will β€œleak” when the elements are deleted (because the element slot will be overwritten with the magic β€œdeleted” value) ... But will the set class behave the same? Is it safe to keep set around, add and remove things from it over time?

Edit : Ok, I tried, and here is what I found:

  >>> import gc
 >>> gc.collect ()
 0
 >>> nums = range (1000000)
 >>> gc.collect ()
 0
 ### rsize: 20 megs
 ### A baseline measurement
 >>> s = set (nums)
 >>> gc.collect ()
 0
 ### rsize: 36 megs
 >>> for n in nums: s.remove (n)
 >>> gc.collect ()
 0
 ### rsize: 36 megs
 ### Memory usage doesn't drop after removing every item from the set ...
 >>> s = None
 >>> gc.collect ()
 0
 ### rsize: 20 megs
 ### ... but nulling the reference to the set * does * free the memory.
 >>> s = set (nums)
 >>> for n in nums: s.remove (n)
 >>> for n in nums: s.add (n)
 >>> gc.collect ()
 0
 ### rsize: 36 megs
 ### Removing then re-adding keys uses a constant amount of memory ...
 >>> for n in nums: s.remove (n)
 >>> for n in nums: s.add (n + 1,000,000)
 >>> gc.collect ()
 0
 ### rsize: 47 megs
 ### ... but adding new keys uses more memory.
+4
source share
2 answers

Yes, set is basically a hash table, like dict - differences in the interface do not imply many differences below. From time to time you should copy the set - myset = set(myset) - just like you should for the dict, according to which many additions and deletions are regularly performed over time.

+7
source

For such questions, it is often better to conduct a quick experiment like this and see what happens:

 s = set() for a in range(1000): for b in range(10000000): s.add(b) for b in range(10000000): s.remove(b) 

What documents and people say and what kind of behavior is actually happening often diverge. If this is important to you, check it out. Do not rely on others.

-1
source

Source: https://habr.com/ru/post/1302640/


All Articles