Returns the approximate memory footprint an object and all of its contents. source: https://code.activestate.com/recipes/577504/
(o)
| 460 | |
| 461 | |
| 462 | def total_size(o): |
| 463 | class="st">"""Returns the approximate memory footprint an object and all of its |
| 464 | contents. |
| 465 | |
| 466 | source: https://code.activestate.com/recipes/577504/ |
| 467 | |
| 468 | |
| 469 | class="st">""" |
| 470 | |
| 471 | def dict_handler(d): |
| 472 | return chain.from_iterable(d.items()) |
| 473 | |
| 474 | all_handlers = { |
| 475 | tuple: iter, |
| 476 | list: iter, |
| 477 | deque: iter, |
| 478 | dict: dict_handler, |
| 479 | set: iter, |
| 480 | frozenset: iter, |
| 481 | } |
| 482 | seen = set() class="cm"># track which object id's have already been seen |
| 483 | default_size = getsizeof(0) class="cm"># estimate sizeof object without __sizeof__ |
| 484 | |
| 485 | def sizeof(o): |
| 486 | if id(o) in seen: class="cm"># do not double count the same object |
| 487 | return 0 |
| 488 | seen.add(id(o)) |
| 489 | s = getsizeof(o, default_size) |
| 490 | |
| 491 | for typ, handler in all_handlers.items(): |
| 492 | if isinstance(o, typ): |
| 493 | s += sum(map(sizeof, handler(o))) |
| 494 | break |
| 495 | return s |
| 496 | |
| 497 | return sizeof(o) |
| 498 | |
| 499 | |
| 500 | def count_cache_key_tuples(tup): |