Returns the approximate memory footprint an object and all of its contents. source: https://code.activestate.com/recipes/577504/
(o)
| 501 | |
| 502 | |
| 503 | def total_size(o): |
| 504 | """Returns the approximate memory footprint an object and all of its |
| 505 | contents. |
| 506 | |
| 507 | source: https://code.activestate.com/recipes/577504/ |
| 508 | |
| 509 | |
| 510 | """ |
| 511 | |
| 512 | def dict_handler(d): |
| 513 | return chain.from_iterable(d.items()) |
| 514 | |
| 515 | all_handlers = { |
| 516 | tuple: iter, |
| 517 | list: iter, |
| 518 | deque: iter, |
| 519 | dict: dict_handler, |
| 520 | set: iter, |
| 521 | frozenset: iter, |
| 522 | } |
| 523 | seen = set() # track which object id's have already been seen |
| 524 | default_size = getsizeof(0) # estimate sizeof object without __sizeof__ |
| 525 | |
| 526 | def sizeof(o): |
| 527 | if id(o) in seen: # do not double count the same object |
| 528 | return 0 |
| 529 | seen.add(id(o)) |
| 530 | s = getsizeof(o, default_size) |
| 531 | |
| 532 | for typ, handler in all_handlers.items(): |
| 533 | if isinstance(o, typ): |
| 534 | s += sum(map(sizeof, handler(o))) |
| 535 | break |
| 536 | return s |
| 537 | |
| 538 | return sizeof(o) |
| 539 | |
| 540 | |
| 541 | def count_cache_key_tuples(tup): |