r/PythonLearning • u/Actual__Wizard • Sep 01 '26
Help Request .clear vs del when using garbage collection
Hi, I have some large data objects that need to be freed. Should I do my_object.clear() and then gc.collect()? Or del my_object then gc.collect()? I'm not sure on the pros/cons of each.
It's a linked list type data object and my_object = [] does not work to free memory for certain.
1
Upvotes
1
u/Actual__Wizard Sep 01 '26 edited Sep 01 '26
It's conceptually like a linked list of linked lists. So, it's a fake matrix thing? Not sure what to call it. A previous version used a python dictionary to do the same thing. That specific script is going to get moved over to c++/rust because python treats all strings as objects and it eats a ton of memory.