Weak refrences to an object can be collected if no strong refrences remain, but we can disable it!
Lists and dictionaries cannot be weak refrenced, but this can:

class Dict(dict):
    pass
class List(list):
    pass

If your holding a large dictionary, you might want to split up over diffrent variables, like this:

import weakref,gc
gc.disable() #no garbage collection!
d = weakref.WeakValueDictionary() #if your values are large(otherwise use WeakKeyDictionary)
#do what ever#

Want group things held together (no order) that also have regular value names?(Deleted when no strong refrences)
Just use weakref.WeakSet!

What do you say about this fine strategy? (not a question)

Also, try to wrap this around it:

try:
    #your code
except MemoryError:
    gc.collect()
    del gc.garbage[:]

So if you run out of memory, you could run a garbage collection (and delete the unreachable grabage)and hope for the best.

I'm not sure I understand which problem you are addressing with this code ...

None! I'm just showing you guys a cool feature!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.