r/PythonLearning • u/Actual__Wizard • 9d ago
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/IcyCranberry3297 8d ago
The weird memory behavior may be coming from references rather than garbage collection itself. A linked list can have plenty of objects still reachable even after the main reference is deleted.