Instead of collecting objects after a fixed number of allocations (700) You could make it dynamic like this:

# initial values
min_allocated_memory = 0
max_allocated_memory = 0
next_gc_run = 1024 * 1024

def manage_memory():
    allocated_memory = get_allocated_memory()
    min_allocated_memory = min(min_allocated_memory, allocated_memory)
    max_allocated_memory = max(max_allocated_memory, allocated_memory)

    if max_allocated_memory - min_allocated_memory > next_gc_run:
        # run the gc
        memory_freed, allocated_memory = run_gc()
        next_gc_run = max(
                allocated_memory * 1.5 - memory_freed, 1024 * 1024
        )
        min_allocated_memory = allocated_memory
        max_allocated_memory = allocated_memory


manage_memory() should be called after every allocation and after a ref count of an object reaches 0 (memory is freed)


Expected behaviours:

=> As less objects contain cyclic references as less often the GC will run (memory_freed is small)

=> As more objects contain cyclic references as more often the GC will run (memory_freed is large)

=> If memory utiliaziation grows fast (burst allocations) GC will run less often: next_gc_run = allocated_memory * 1.5 - memory_freed

... Of course the constants: 1.5 and 1024 * 1024 are only suggestions...

- Ralf
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to