| | 573 | |
| | 574 | |
| | 575 | def print_gc_stats(env): |
| | 576 | print '---------------- GC stats ----------------------' |
| | 577 | import gc |
| | 578 | live_objects = {} |
| | 579 | for obj in gc.get_objects(): |
| | 580 | live_objects[id(obj)] = type(obj) |
| | 581 | print "live objects: ", len(live_objects) |
| | 582 | first = True |
| | 583 | if hasattr(env, '_accumulated'): |
| | 584 | first = False |
| | 585 | accumulated = env._accumulated |
| | 586 | stats = env._stats |
| | 587 | print "accumulated objects: ", len(accumulated) |
| | 588 | else: |
| | 589 | accumulated = env._accumulated = {} |
| | 590 | stats = env._stats = {} |
| | 591 | # retrieving previous stats |
| | 592 | oldstats = {} |
| | 593 | for t,cnt in stats.iteritems(): |
| | 594 | oldstats[t] = [cnt[0]] |
| | 595 | # analysing new stuff |
| | 596 | for i,t in live_objects.iteritems(): |
| | 597 | if i not in accumulated: |
| | 598 | cnt = stats.setdefault(t, [0]) |
| | 599 | cnt[0] += 1 |
| | 600 | accumulated[i] = t |
| | 601 | if first: |
| | 602 | return [] |
| | 603 | # removing old stuff |
| | 604 | delete = [] |
| | 605 | for i,t in accumulated.iteritems(): |
| | 606 | if i not in live_objects: |
| | 607 | cnt = stats[t] |
| | 608 | cnt[0] -= 1 |
| | 609 | delete.append(i) |
| | 610 | for i in delete: |
| | 611 | del accumulated[i] |
| | 612 | print 'stats by type:' |
| | 613 | for t in sorted(stats.keys(), key=lambda x: str(x)): |
| | 614 | newval = stats[t] |
| | 615 | oldval = oldstats.get(t) |
| | 616 | if not oldval: |
| | 617 | print t, 'delta:', newval |
| | 618 | elif oldval[0] != newval[0]: |
| | 619 | print t, 'total:', newval[0], 'delta:', newval[0] - oldval[0] |
| | 620 | print '------------------------------------------------' |